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

时间:2014-06-01 18:27:22

标签: spring-mvc

我使用单个.property文件作为

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

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

我如何使用多个属性文件。我看到THIS但未使用@PropertySource

2 个答案:

答案 0 :(得分:0)

如果您使用的是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>  

您可以在这些文件中使用属性,如下所示

@Controller
public class BasicController {

    @Value
    String name;   
 .....
...
}

如果名称在属性文件中可用作键,则会在此处注入..

答案 1 :(得分:0)

您可以使用@PropertySources之类的

@PropertySources(value = {@PropertySource("classpath:jdbc.properties"),@PropertySource("classpath:paypalConfig.properties")})

或者您可以使用@PropertySource之类的

@PropertySource(value{"classpath:jdbc.properties","classpath:paypalConfig.properties"})

执行此操作后,您可以使用Environment变量来访问与属性文件中的键相对应的值

environment.getProperty("YOUR_KEY_IN_PROPERTY_FILE");