【问题标题】:Create Spring Bean where class is a string?在类是字符串的情况下创建 Spring Bean?
【发布时间】:2011-12-09 17:59:58
【问题描述】:

是否可以在 Spring 配置文件中使用某种 bean 工厂或静态类来创建 Java 类的实例并设置其属性?像这样的:

<bean id="myThingy" class="org.whatever.specialBeanMaker">
     <property class="${maven.filtered.property.value}" />
     <properties>
        <property name="a" value="...." />
        <property name="b" value="...." />
        <property name="c" value="...." />
     </properties>
</bean>

换句话说,我需要从一个直到运行时才知道的类创建一个 bean(因为过滤、JNDI、propertyPlaceholderConfigurer 等。

【问题讨论】:

    标签: java spring configuration javabeans factory


    【解决方案1】:

    不需要任何特别的东西。就像你期望的那样做:

    <bean class="${whateverClass}"/>
    

    【讨论】:

    • 我之前尝试过期望它可以工作,但 Eclipse/Spring-Tools 抛出了构建错误。然后我刚才试了一下,果然奏效了!错误一定是来自其他东西。无论如何...谢谢。
    猜你喜欢
    • 2014-06-17
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 2017-12-07
    • 2014-11-28
    • 2022-01-11
    相关资源
    最近更新 更多