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;
}
标签:PropertySource,String,redis,properties,public,注解,class,SpringBoot From: https://www.cnblogs.com/enhance/p/16989047.html