方式1

  1. <bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  2. </bean>
  3. <!-- spring上下文(XML和注解)中的属性占位符替换 -->
  4. <context:property-placeholder properties-ref="properties" trim-values="true"/>

方式2:

  1. <context:property-placeholder location=""xx />

方式3:

  1. <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
  2. <property name ="location" value="xxx">
  3. </bean>

方式4:

  1. Any ${...} placeholders present in a @PropertySource resource location will be resolved against the set of property sources already registered against the environment. For example:
  2. @Configuration
  3. @PropertySource("classpath:/com/${my.placeholder:default/path}/app.properties")
  4. public class AppConfig {
  5. @Autowired
  6. Environment env;
  7. @Bean
  8. public TestBean testBean() {
  9. TestBean testBean = new TestBean();
  10. testBean.setName(env.getProperty("testbean.name"));
  11. return testBean;
  12. }
  13. }