从数据库或Java对象动态加载Springbean属性)

从数据库或Java对象动态加载Springbean属性),spring,web-services,properties,Spring,Web Services,Properties,我有一个场景,需要将属性从数据库或java对象加载到bean中 举个例子: <bean id="ConfigAsstDemoBeanParent" class="gps.springconfig.DemoClass" lazy-init="true"> <property name="demoValueFromBean" value="demoValue"></property> <property name="demoValueForKeyFr

我有一个场景,需要将属性从数据库或java对象加载到bean中

举个例子:

<bean id="ConfigAsstDemoBeanParent" class="gps.springconfig.DemoClass" lazy-init="true">
  <property name="demoValueFromBean" value="demoValue"></property>
  <property name="demoValueForKeyFromProperties" value="${DEMO_KEY}"></property>
</bean>

与从属性文件加载值的
${DEMO_KEY}
属性占位符不同,我需要从数据库加载值,我使用存储过程从Java类中检索该值

请为我推荐一种机制,我可以利用它来实现上述场景。目前,我正在研究扩展SpringMain和/或PropertyPlaceHolderConfigure类,并编写自己的自定义引导程序


另外,请建议为上述场景编写引导程序的提示。

Java配置似乎是一个很好的选择:

@Configuration
public class Config {

    @Resource
    private DataSource dataSource;

    @Bean
    @Lazy
    public DemoClass configAsstDemoBeanParent() {
        DemoClass demo = new DemoClass();
        demo.setDemoValueFromBean("demoValue");
        demo.demoValueForKeyFromProperties( /* query the database here */);
        return demo;
    }

}

请注意,您可以将
DataSource
(或
JdbcTemplate
)注入到
@Configuration
类中,前提是该类是在其他地方定义的。

我使用PropertyPlaceHolderConfigure处理类似的内容。@RockyTriton但据我所知,PropertyPlaceHolderConfigure只能用于解析.properties文件中的属性。我真的不知道如何使用它从DB中获取值。如果我错了,请纠正我。+1此外,您不必编写任何自定义引导。你可以按照这里的描述加载这个类->这太棒了。。很好用。。不过,我知道这是一件小事。我有很多类似上面定义的bean,还有更多的属性占位符。。你能不能给我一些建议,让它成为泛型的,这样我就可以使用一个泛型类来获取键的所有值,这个泛型类可以执行DB调用…@kanap008:如果你已经用XML定义了这些bean,并带有属性占位符,那么你最好还是使用XML。您还可以尝试自动生成
@Configuration
类。