1,定义配置文件mypro.properties
#自定义配置 base.redis.url=localhost:434342,在启动类上加上配置注解:
@PropertySource("classpath:mypro.properties")
public class Application {
}
这是定义要读取的properties文件的位置,另外,假如只读取该文件中的部分配置,可以在启动类上使用
@ConfigurationProperties(prefix = "base")注解来指定读取的前缀。建议不用设置前缀而是读取所有。
3,在需要使用的类中使用@Value(${base.redis.url})注解注入配置信息
//获取配置信息 @Value("${spring.datasource.url}") private String url;