Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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
Spring mvc Spring MVC如何使用多属性文件_Spring Mvc - Fatal编程技术网

Spring mvc Spring MVC如何使用多属性文件

Spring mvc Spring MVC如何使用多属性文件,spring-mvc,Spring Mvc,我使用单个.property文件作为 @PropertySource("classpath:messages.properties") public class BasicController { @Autowired Environment env; ..... ... } 如何使用多个属性文件。我看到了,但它没有使用@PropertySource如果您使用的是spring 4.0,那么可以使用此注释 @PropertySources(value = {@Prope

我使用单个.property文件作为

@PropertySource("classpath:messages.properties")
public class BasicController {

    @Autowired
    Environment env;   
 .....
...
}

如何使用多个属性文件。我看到了,但它没有使用
@PropertySource

如果您使用的是spring 4.0,那么可以使用此注释

@PropertySources(value = {@PropertySource("classpath:/app.properties")})  
或者,如果您使用的是spring 3.0+

您可以使用下面的配置文件中的配置,也可以直接在类中调用setLocations方法

在后一种情况下,您将不需要使用PropertySource注释

<bean id="propertiesPlacholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >  
     <property name="locations">  
       <list>  
           <value>classpath:/app1.properties</value>  
           <value>classpath:/app2.properties</value>  
       </list>  
    </property>  
 </bean>  

如果名称作为属性文件中的键可用..它将被注入此处..

您可以使用
@PropertySources

@PropertySources(value = {@PropertySource("classpath:jdbc.properties"),@PropertySource("classpath:paypalConfig.properties")})
@PropertySource(value{"classpath:jdbc.properties","classpath:paypalConfig.properties"})
或者您也可以使用
@PropertySource

@PropertySources(value = {@PropertySource("classpath:jdbc.properties"),@PropertySource("classpath:paypalConfig.properties")})
@PropertySource(value{"classpath:jdbc.properties","classpath:paypalConfig.properties"})
执行此操作后,您可以使用
环境
变量访问属性文件中键对应的值,如下所示

environment.getProperty("YOUR_KEY_IN_PROPERTY_FILE");

如何使用第二种方法调用控制器中的
app1.properties
。您不必调用..只需使用@value example@value String name直接使用该文件中属性的值;不,我要问的是,我如何才能从特定文件中获取值就像我的问题中一样,我可以使用
env.getProperty('X')
来获取属性,这很容易用动态键访问。因此,我希望以这种方式访问。我不确定您的业务需要什么,才能仅使用getProperty()方法获取属性值。我们最终需要从属性文件中获取正确的值。我使用@value only alwas获取。这非常简单。。