簡體   English   中英

java spring context:property-placeholder手動加載

[英]java spring context:property-placeholder manually loading

Spring java- quartz schedular應用程序

我想加載.property文件動態傳遞throw程序參數而不是context:property-placeholder在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;

找到答案

@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;
}

Resoruces http://www.baeldung.com/2012/02/06/properties-with-spring/#byhandnew

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM