在 Spring 版本 4 中,您可以找到属性文件:
1)xml模式
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<!-- default resources folder (default package maven project) -->
<value>classpath:mongodb.remote.properties</value>
<!-- Or in /WEB-INF/ folder -->
<value>/WEB-INF/mongodb.remote.properties</value>
</list>
</property>
</bean>
----------------------------------------------------------------------------------------
2)程序化模式:
If you have for example this package : com.profile.config, com.profile.controller, ecc..
it's not problem if you put only com.profile, it's ok !!! Now
@Configuration
@ComponentScan(basePackages = "com.profile")
/** resources folder & default package maven project*/
@PropertySource(value = { "classpath:mongodb.remote.properties" })
public class MyPropertySourcesPlaceholderConfigurer {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
---------------------------------------------------------------------------------
Your property file
label.test.val=this is the property file value!!!!!
---------------------------------------------------------------------------------
@Controller
public class LabelsAndValuesController {
@Value("${label.test.val}")
String test;
}
输出:
---------------------------------------------------------------------------------
this is the property file value!!!!!
---------------------------------------------------------------------------------