Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
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 - Fatal编程技术网

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()。很好,谢谢!