Question

I have a two beans with the same id as thats the Neo4j Class it maps to internally so i can't have the id changed for both. Now for a clustered environment I need this bean:

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

And for a non-clustered one I need this :

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

Right now I comment either one of them based on environment as not all environments will have cluster setup. Is there a way where as based on environment value(a property may be) it picks one bean among these.

This is how it is used in java class.

@Autowired
private GraphDatabaseService graphDB;

Thanks,

Was it helpful?

Solution

You can use spring profile feature (since spring 3.1.X) see link

e.g

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

and active the profile in your application in this way

-Dspring.profiles.active="cluster"

Will be loaded only the beans without profile and all those with the profile activated. I hope this solution can help to solve your problem.

OTHER TIPS

You dont need to define with different Id´s just in different xml configuration files. Like

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">

And then use Spring profile to load one file or another depending of the profile

        <beans profile="cluster">
    <import resource="spring/cluster.xml" />
</beans>
<beans profile="no_cluster">
   <import resource="spring/no_cluster.xml" />
</beans>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top