假如要添加一个配置文件 example.properties 。里面放键值对:
myconfig.example.hosts=192.168.1.1
想在项目启动的时候读取它,并用于创建bean。
首先要创建配置模型
@Component
// 这边会把配置文件中的myconfig.exmaple前缀的全带出来,自动组装成bean,使用前提是,这个类的属性和配置的名称能刚好对应上
@ConfigurationProperties("myconfig.example")
public class ExampleProperties {
private List<String> hosts = new ArrayList<>();
public List<String> getHosts() {
return hosts;
}
public void setHosts(List<String> hosts) {
this.hosts = hosts;
}
}
也可以这么写:
@ConfigurationProperties(locations = "classpath:example.properties", ignoreUnknownFields = false, prefix = "myconfig.example")
这样也可以直接指定文件 就不要下面再定义PropertySources来指定配置文件了。
如果配置中的前缀不一致,可以自己定义映射关系:
@Component
public class JdbcConfig {
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${dricverClassName}")
private String dricverClassName;
}
使用 @PropertySources()注解,添加配置文件解析。
@Configuration
// 这边spring会自己把配置读取到Environment类中
@PropertySource("classpath:example.properties")
public class ExampleAutoConfiguration {
}
多个可以使用 @PropertySources({@PropertySource("classpath:jdbc.properties"),@PropertySource("classpath:user.properties")})
*如果想自己控制bean创建,不要把config声明为bean,自己new出来,例如:
public class ExampleConfig {
private List<String> hosts = new ArrayList<>();
public List<String> getHosts() {
return hosts;
}
public void setHosts(List<String> hosts) {
this.hosts = hosts;
}
}
// 通过方法创建bean
@Configuration
@PropertySource("classpath:example.properties")
public class ExampleAutoConfiguration {
@Autowired
private Environment env;
@Bean(name="exampleConfig")
public ExampleConfig ExampleConfig(){
ExampleConfig exampleConfig = new ExampleConfig();
exampleConfig.setHosts(env.getProperty("myconfig.example.hosts"));
return exampleConfig;
}
}
如果想手动实现配置文件的读取逻辑,可以这么写:
// 如果需要自己实现配置文件读取逻辑
@Component
public class ExampleEnvironmentPostProcessor implements EnvironmentPostProcessor{
private String[] propertyFileList = {
"exmaple1.properties",
"exmaple2.properties"
};
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
// 从类路径加载
for (String propertyFile : propertyFileList) {
Resource resource = new ClassPathResource(propertyFile);
if (!resource.exists()) {
throw new IllegalArgumentException("propertyFile" + resource + "not exist");
}
try {
Properties properties = new Properties();
properties.load(resource.getInputStream());
PropertiesPropertySource propertySource = new PropertiesPropertySource(resource.getFilename(), properties);
environment.getPropertySources().addLast(propertySource);
} catch (IOException ex) {
throw new IllegalStateException("load resource exception" + resource, ex);
}
}
// 从外部加载
try(InputStream input = new FileInputStream("E:\\example.properties")) {
Properties properties = new Properties();
properties.load(input);
PropertiesPropertySource propertySource = new PropertiesPropertySource("example", properties);
environment.getPropertySources().addLast(propertySource);
} catch (Exception e) {
e.printStackTrace();
}
}
}
需要/在classpath META-INF文件夹中,创建spring.factories文件并添加
org.springframework.boot.env.EnvironmentPostProcessor=package.name.ExampleEnvironmentPostProcessor
使用时,从environment取
context.getEnvironment().getProperty("myconfig.example.hosts");