Java @物业资源-如何正确使用不同的资源? 让我们考虑我们有两个属性文件,并希望读取其中的一些属性。从Spring4.0开始,可以使用注解@PropertySources来实现。我发现可以这样做: @PropertySources({ @PropertySource(name = "first", value = "classpath:first.properties"), @PropertySource(name = "second", value = "classpath:second.properties") }) public class AppConfig { @Autowired Environment environment; @Bean public SomeBean someBean { //reading from first properties file org.springframework.core.env.PropertySource<?> first = ((StandardEnvironment) environment).getPropertySources().get("first")); first.getProperty("someProperty"); //reading from second properties file org.springframework.core.env.PropertySource<?> second = ((StandardEnvironment) environment).getPropertySources().get("second")); second.getProperty("someProperty"); } } @PropertySources({ @PropertySource(name=“first”,value=“classpath:first.properties”), @PropertySource(name=“second”,value=“classpath:second.properties”) }) 公共类AppConfig{ @自动连线 环境; @豆子 公共菜豆{ //从第一个属性文件读取 org.springframework.core.env.PropertySource优先= ((StandardEnvironment)environment.getPropertySources().get(“first”); 第一,getProperty(“someProperty”); //从第二个属性文件读取 org.springframework.core.env.PropertySource第二= ((StandardEnvironment)environment.getPropertySources().get(“第二”); 第二,getProperty(“someProperty”); } }
这样合适吗?你知道更好的吗?这是可以做得更多的:Java @物业资源-如何正确使用不同的资源? 让我们考虑我们有两个属性文件,并希望读取其中的一些属性。从Spring4.0开始,可以使用注解@PropertySources来实现。我发现可以这样做: @PropertySources({ @PropertySource(name = "first", value = "classpath:first.properties"), @PropertySource(name = "second", value = "classpath:second.properties") }) public class AppConfig { @Autowired Environment environment; @Bean public SomeBean someBean { //reading from first properties file org.springframework.core.env.PropertySource<?> first = ((StandardEnvironment) environment).getPropertySources().get("first")); first.getProperty("someProperty"); //reading from second properties file org.springframework.core.env.PropertySource<?> second = ((StandardEnvironment) environment).getPropertySources().get("second")); second.getProperty("someProperty"); } } @PropertySources({ @PropertySource(name=“first”,value=“classpath:first.properties”), @PropertySource(name=“second”,value=“classpath:second.properties”) }) 公共类AppConfig{ @自动连线 环境; @豆子 公共菜豆{ //从第一个属性文件读取 org.springframework.core.env.PropertySource优先= ((StandardEnvironment)environment.getPropertySources().get(“first”); 第一,getProperty(“someProperty”); //从第二个属性文件读取 org.springframework.core.env.PropertySource第二= ((StandardEnvironment)environment.getPropertySources().get(“第二”); 第二,getProperty(“someProperty”); } },java,spring,Java,Spring,这样合适吗?你知道更好的吗?这是可以做得更多的: @PropertySources({ @PropertySource(name = AppConfig.FIRST, value = "classpath:first.properties"), @PropertySource(name = AppConfig.SECOND, value = "classpath:second.properties") }) public class AppConfig { static
@PropertySources({
@PropertySource(name = AppConfig.FIRST, value = "classpath:first.properties"),
@PropertySource(name = AppConfig.SECOND, value = "classpath:second.properties")
})
public class AppConfig {
static final String FIRST = "first";
static final String SECOND = "second";
@Autowired
private AbstractEnvironment defaultConfiguration;
@Bean
public SomeBean someBean {
//reading from first properties file
firstConfiguration().getProperty("someProperty");
//reading from second properties file
secondConfiguration().getProperty("someProperty");
}
@Bean
public org.springframework.core.env.PropertySource<?> firstConfiguraiton() {
return defaultConfiguration.getPropertySources().get(FIRST);
}
@Bean
public org.springframework.core.env.PropertySource<?> secondConfiguration() {
return defaultConfiguration.getPropertySources().get(SECOND);
}
}
@PropertySources({
@PropertySource(name=AppConfig.FIRST,value=“classpath:FIRST.properties”),
@PropertySource(name=AppConfig.SECOND,value=“classpath:SECOND.properties”)
})
公共类AppConfig{
静态最终字符串FIRST=“FIRST”;
静态最终字符串SECOND=“SECOND”;
@自动连线
私有抽象环境配置;
@豆子
公共菜豆{
//从第一个属性文件读取
firstConfiguration().getProperty(“someProperty”);
//从第二个属性文件读取
secondConfiguration().getProperty(“someProperty”);
}
@豆子
public org.springframework.core.env.PropertySource firstconfiguration()文件{
返回defaultConfiguration.getPropertySources().get(第一个);
}
@豆子
public org.springframework.core.env.PropertySource secondConfiguration(){
返回defaultConfiguration.getPropertySources().get(秒);
}
}
您获得房产的方式似乎不错。您可以通过对属性源名称使用常量来改进它。而不是强制转换,你可以自动连接一个抽象环境或类似的东西。如果愿意,也可以使用.getPropertySources().iterator()。很好,谢谢!