java spring context:property-placeholder手动加载

时间:2012-12-05 19:08:40

标签: java spring properties loading

Spring java- quartz schedular application

我想加载.property文件动态传递throw程序参数而不是上下文:属性占位符在spring上下文中,我该如何实现这个任务,任何帮助都赞赏...

我从主java文件手动加载和刷新spring上下文,如下面的代码所示。

SpringUtil_1.loadSpringConfig();
rootContext = new ClassPathXmlApplicationContext();
rootContext.setConfigLocation("abc-configuration.xml");
rootContext.refresh();

在spring配置中,我想从代码中获得如下所示的上下文属性占位符。

<context:property-placeholder location="classpath:lnRuntime.properties"/>

我在spring上下文和使用spring EL的java文件中使用占位符如下

<bean id="dataSource" 
class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
        <property name="driverClassName" value="net.sourceforge.jtds.jdbcx.JtdsDataSource"/>
        <property name="url" value="${dataSource.url}"/>
    </bean>

在java中我正在访问如下

private @Value("${dz.host}") String dzHost;

1 个答案:

答案 0 :(得分:5)

找到答案

@Bean
public static PropertySourcesPlaceholderConfigurer properties(){
  PropertySourcesPlaceholderConfigurer pspc =
   new PropertySourcesPlaceholderConfigurer();
  Resource[] resources = new ClassPathResource[ ]
   { new ClassPathResource( "foo.properties" ) };
  pspc.setLocations( resources );
  //pspc.setIgnoreUnresolvablePlaceholders( true );
  return pspc;
}

重新计算http://www.baeldung.com/2012/02/06/properties-with-spring/#byhandnew

相关问题