3

Spring java-quartz 调度应用

我想加载 .property 文件动态传递的抛出程序参数而不是 context:property-placeholder 在 spring 上下文中,我怎样才能完成这个任务,任何帮助表示赞赏......

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

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

在弹簧配置中,我有如下的上下文属性占位符,我想从代码中获取。

<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;
4

1 回答 1

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

于 2012-12-05T19:23:48.263 回答