【问题标题】:Spring multiple beans with same id in a factory pattern?在工厂模式中弹出多个具有相同 ID 的 bean?
【发布时间】:2014-07-31 07:21:07
【问题描述】:

我有两个 bean,它们的 id 与它在内部映射到的 Neo4j 类的 id 相同,因此我不能同时更改两者的 id。现在对于集群环境,我需要这个 bean:

<bean id="graphDatabaseService" factory-bean="graphDbBuilderFinal"
        factory-method="newGraphDatabase" destroy-method="shutdown" /> 

对于非集群的,我需要这个:

<bean id="graphDatabaseService" class="org.springframework.data.neo4j.support.GraphDatabaseServiceFactoryBean"
        destroy-method="shutdown" scope="singleton">
<constructor-arg value="${neo4j.database.path}" />
</bean>

现在我根据环境对其中任何一个进行评论,因为并非所有环境都会设置集群。有没有一种方法可以根据环境值(可能是属性)在其中选择一个 bean。

这就是它在java类中的使用方式。

@Autowired
private GraphDatabaseService graphDB;

谢谢,

【问题讨论】:

    标签: spring factory spring-data-neo4j


    【解决方案1】:

    您可以使用弹簧轮廓功能(自 spring 3.1.X 起)see link

    例如

    <beans profile="cluster">
    <bean id="graphDatabaseService" factory-bean="graphDbBuilderFinal"
            factory-method="newGraphDatabase" destroy-method="shutdown" /> 
    </beans>
    
    <beans profile="no_cluster">
    <bean id="graphDatabaseService" class="org.springframework.data.neo4j.support.GraphDatabaseServiceFactoryBean"
            destroy-method="shutdown" scope="singleton">
    <constructor-arg value="${neo4j.database.path}" />
    </bean>
    </beans>
    

    并以这种方式在您的应用程序中激活配置文件

    -Dspring.profiles.active="cluster"
    

    将只加载没有配置文件的 bean 和所有激活了配置文件的 bean。 我希望这个解决方案可以帮助您解决问题。

    【讨论】:

    • 将这些移动到单独的 xmls 后,我在启动时收到错误原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为“graphDatabaseService”的 bean。可能无法导入这些?我改变了tomcat启动来设置这样的配置文件..我错过了什么? -Dspring.profiles.active="no_cluster" call "%EXECUTABLE%" start %CMD_LINE_ARGS% :end
    • 在这个link有一个用于spring profile的tomcat配置
    • 如果一切正常,请告诉我们。如果不行,我会帮你找到问题。
    • 感谢@Xstian!这有帮助,尽管 setenv 是一个更清洁的解决方案但没有工作,可能是因为我在 windows m/c 上,而我有 setenv.bat。但是在 catalina.bat 中设置它是有效的。
    【解决方案2】:

    您不需要在不同的 xml 配置文件中定义不同的 ID。喜欢

    cluster.xml --->

      <bean id="graphDatabaseService" factory-bean="graphDbBuilderFinal"
        factory-method="newGraphDatabase" destroy-method="shutdown" /> 
    

    no_cluster.xml

      <bean id="graphDatabaseService" class="org.springframework.data.neo4j.support.GraphDatabaseServiceFactoryBean"
        destroy-method="shutdown" scope="singleton">
    

    然后根据配置文件使用 Spring 配置文件加载一个或另一个文件

            <beans profile="cluster">
        <import resource="spring/cluster.xml" />
    </beans>
    <beans profile="no_cluster">
       <import resource="spring/no_cluster.xml" />
    </beans>
    

    【讨论】:

    • 将这些移动到单独的 xmls 后,我在启动时收到错误原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为“graphDatabaseService”的 bean。可能无法导入这些?我更改了tomcat启动以设置这样的配置文件..我错过了什么? -Dspring.profiles.active="no_cluster" call "%EXECUTABLE%" start %CMD_LINE_ARGS% :end
    • 你在使用 Maven 吗?通常我通过 Maven 传递配置文件。
    • 很高兴听到这个消息。如果您认为有用的回复并解决了您的问题,请不要忘记投票并将答案标记为完成
    猜你喜欢
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-21
    相关资源
    最近更新 更多