posts - 101,comments - 5,views - 14万

1. @PropertySource

SpringBoot读取配置信息的方式有以下几种。但实际开发中一个配置文件是不够用的,比如项目中集成mongo redis kafka等需要多个配置文件,这样有利于开发以及维护管理。Springboot通过PropertySource或者PropertySources来实现多配置文件

a)application.properties

b)application-test.properties

c)System.getProperties()

d)操作系统环境变量,如下图示

 

 如下例中,在resources中添加redis.properties mongo.properties文件。只有添加@PropertySource或者@PropertySources指定这两个properties文件才会读取其中内容

 

 

复制代码
@SpringBootApplication(
exclude = {
MongoAutoConfiguration.class,
MongoReactiveAutoConfiguration.class,
MongoReactiveDataAutoConfiguration.class,
MongoDataAutoConfiguration.class
})
@EnableTransactionManagement
//@PropertySource(value={"classpath:mongo.properties","classpath:redis.properties"})
@PropertySources({@PropertySource("classpath:mongo.properties"),
@PropertySource("classpath:redis.properties")})
public class Application {

public static void main(String[] args) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
SpringApplication.run(Application.class, args);
}
}
@RestController
@RequestMapping("/test")
public class QueryController {
  @Value("${mongo.property}")
  private String mongoProperty;
  @Value(("${redis.property}"))
  private String redisProperty; 
  @RequestMapping(value="hello10")
  public String say10(){
  System.out.println("sourceProperties:"+mongoProperty);
  return mongoProperty;
  }
  @RequestMapping(value="hello11")
  public String say11(){
   System.out.println("sourceProperties:"+redisProperty);
  return redisProperty;
  }
复制代码

 

posted on   colorfulworld  阅读(929)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示