• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Broke_薪雪
博客园    首页    新随笔    联系   管理    订阅  订阅

SpringBoot读取自定义配置文件

自定义配置文件

my-config.properties

1 bfxy.title=bfxy
2 bfxy.name=hello spring boot!
3 bfxy.attr=12345

 

 

配置文件类

appconfig.java

 1 import org.springframework.beans.factory.annotation.Autowired;
 2 import org.springframework.beans.factory.annotation.Value;
 3 import org.springframework.context.annotation.ComponentScan;
 4 import org.springframework.context.annotation.Configuration;
 5 import org.springframework.context.annotation.PropertySource;
 6 import org.springframework.core.env.Environment;
 7 
 8 @Configuration
 9 @ComponentScan({"com.bfxy.springboot.*"})            //你要进行扫描的包路径
10 @PropertySource("classpath:my-config.properties")    //加载指定的配置
11 public class appConfig {
12     
13     @Value("${custom.group}")
14     private String customGroup;
15     
16     @Autowired
17     private Environment environment;
18     
19     @Value("${bfxy.name}")
20     private String name;
21     
22     @Value("${bfxy.title}")
23     private String title;
24     
25     @Value("${bfxy.attr}")
26     private String attr;
27     
28     
29     public void output(){
30         System.err.println("通过@Value注解读取配置: " + this.customGroup);
31         System.err.println("通过Environment对象读取配置: " + environment.getProperty("custom.team"));
32         
33         System.err.println("加载自己的配置文件内容"+this.name+","+this.title+","+this.attr);
34     }
35     
36 
37 }

 

posted @ 2019-01-18 12:38  Broke、薪雪  阅读(476)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3