I have the following need and I want to know if can be accomplished using Spring.
I have a generic bean been used by other beans.
<bean id="genericCommand" class="myCommand" abstract="true">
<property name="command" value="start service #{#this.serviceName}" />
</bean>
This is a specific bean using the generic bean
<bean id="someTasks" class="myTask">
<property name="commands">
<array>
<bean parent="genericCommand">
<property name="serviceName" value="server1" />
</bean>
</array>
</property>
</bean>
It is possible to do it?
Yes it possible but you need to use <list> instead of <array>
<list>
<ref bean="googleGeocodingService"/>
</list>
Related
Consider I have something like this in beans.xml:
<bean id="emails" class="org.some.package.SomeClass">
<property name="emailList">
<list>
<value>pechorin#hero.org</value>
<value>raskolnikov#slums.org</value>
<value>stavrogin#gov.org</value>
<value>porfiry#gov.org</value>
</list>
</property>
</bean>
But I need to add emailList property into multiple beans. How can I do that without writing property to each bean? Can externalize property and inject it into each bean?
I expect something like:
<property name="commonProp">
<list>
<value>pechorin#hero.org</value>
<value>raskolnikov#slums.org</value>
<value>stavrogin#gov.org</value>
<value>porfiry#gov.org</value>
</list>
</property>
<bean id="emailsOne" class="org.some.package.ClassOne">
<property name="emailList" ref="commonProp" />
</bean>
<bean id="emailsTwo" class="org.some.package.ClassTwo">
<property name="emailList" ref="commonProp" />
</bean>
You can do it using: util:list
<util:list id="myList" value-type="java.lang.String">
<value>foo</value>
<value>bar</value>
</util:list>
Then use this myList reference in other beans.
I have the following beans in my Spring config file:
<bean id="myList" class="java.util.ArrayList">
<constructor-arg>
<list>
<ref bean="elem1"/>
<ref bean="elem2"/>
<ref bean="elem3"/>
<ref bean="elem4"/>
</list>
</constructor-arg>
</bean>
<bean id="elem4" class="myClass">
<property name="title" value="random4"/>
</bean>
<bean id="elem1" class="myClass">
<property name="title" value="random1"/>
</bean>
<bean id="elem3" class="myClass">
<property name="title" value="random3"/>
</bean>
<bean id="elem2" class="myClass">
<property name="title" value="random2"/>
</bean>
I have noticed that in my application the elements in myList are in the following order: elem4, elem1, elem3, elem2. I was expecting that the elements in my list will be in the order I set when I declared the ref beans ( elem1, elem2, elem3, elem4).
Is there an order in which Spring initializes beans?
Is there a way I can specify an order for the elements in my list?
The spring does respect the order you gave in the list. The elements in the list would exactly be the [elem1,elem2,elem3,elem4] as you specified. Otherwise, you are doing something wrong, can you show the code which prints you the different order?
The order of bean initialization however may be different and depends on bean dependencies, so, for example if you have two beans
<bean id="holder" class="my.HolderBean" lazy-init="false">
<property name="inner" ref="inner"/>
</bean>
<bean id="inner" class="my.InnerBean" lazy-init="false"/>
Then regardless of the xml definition order, the InnerBean will be initialized first, then during HolderBean initialization it will be injected into HolderBean.
I have this mapper-config.xml.There is the DAO and the BLM fot the Game class:
<bean id="DAOGame" class="it.certimeter.nagima.dao.game.DAOGame">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<bean id="BLMGameTarget" class="it.certimeter.nagima.blm.game.BLMGame">
<property name="daoGame" ref="DAOGame" />
</bean>
And the bean for the transaction:
<bean class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" id="BLMGame">
<property name="proxyInterfaces">
<list>
<value>it.certimeter.nagima.blm.game.IBLMGame</value>
</list>
</property>
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="target">
<ref bean="BLMGameTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="saveGame">PROPAGATION_REQUIRED, -it.fondsai.jeffs.core.exception.service.appl.JeffsServiceApplException</prop>
</props>
</property>
<!-- <property name="anonymousAccess" value="true"/> -->
</bean>
But I have this error:
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [it.certimeter.nagima.blm.game.IBLMGame] is defined: expected single matching bean but found 2: BLMGameTarget,BLMGame
Where am I wrong??
Yeah, the issue with what Ben commented on. I just ran into this myself the other day.
This issue brings a bad code smell with it, but if you want a workaround, I would suggest the following:
Go to where you #Autowire your IBLMGame instance. In addition to the #Autowire annotation, you can provide a #Qualifier annotation and give a String value that represents the bean name you actually want wired in.
So for your case, it could look something like this:
#Autowired
#Qualifier("BLMGameTarget") // you can substitute in "BLMGame" if that's the bean you want
IBLMGame iblmGame;
I am working on Spring based application. The XML is simple but contains several almost identical fragments. For example I have 5 different DAO objects, 2 queues etc. Configuration of each DAO looks like:
<bean id="deviceDaoTarget" class="com.mycompany.dao.hibernate.DeviceDAOHibernateImpl"
autowire="byName" />
<bean id="deviceDAO" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.mycompany.dao.DeviceDAO</value>
</property>
<property name="interceptorNames">
<list>
<value>hibernateInterceptor</value>
<value>deviceDaoTarget</value>
</list>
</property>
</bean>
I'd be happy to use some kind of import with parameters. For example I'd like to create parametrized configuration of DAO like this:
<bean id="${dao.target}" class="${dao.class}"
autowire="byName" />
<bean id="deviceDAO" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>${dao.interface}</value>
</property>
<property name="interceptorNames">
<list>
<value>hibernateInterceptor</value>
<value>${dao.target}</value>
</list>
</property>
</bean>
and then call it several times with different parameters, e.g.:
<import resource="spring-dao.xml">
<param name="dao.interface">com.mycompany.dao.hibernate.DeviceDAO</param>
<param name="dao.class">com.mycompany.dao.hibernate.DeviceDAOHibernateImpl</param>
<param name="dao.target">deviceDaoTarget</param>
</import>
Is something like this possible?
You can define a <bean id="parentBean" abstract="true" ...>...</bean> with the common configuration and then have <bean id="childBean" parent="parentBean" ...>...</bean> with just specific configuration for that bean.
Short question: If I have class that impelemnts FactoryBean interface, how can I get from FactoryBean object itself instead of FactoryBean.getObject()?
Long question: I have to use 3-rd party Spring based library which is hardly use FactoryBean interface. Right now I always must configure 2 beans:
<!-- Case 1-->
<bean id="XYZ" class="FactoryBean1" scope="prototype">
<property name="steps">
<bean class="FactoryBean2">
<property name="itemReader" ref="aName"/>
</bean>
</property>
</bean>
<bean id="aName" class="com.package.ClassName1" scope="prototype">
<property name="objectContext">
<bean class="com.package.ABC"/>
</property>
</bean>
<!-- Case 2-->
<bean id="XYZ2" class="FactoryBean1" scope="prototype">
<property name="steps">
<bean class="FactoryBean2">
<property name="itemReader" ref="aName2"/>
</bean>
</property>
</bean>
<bean id="aName2" class="com.package.ClassName1" scope="prototype">
<property name="objectContext">
<bean class="com.package.QWE"/>
</property>
</bean>
Actyually defintion of a bean with name "XYZ" (compare with "XYZ2") never will be changed, but because of factory nature I must copy the code for each configuration.
Definition of a bean with name "aName" always will be new (i.e. each configuration will have own objectContext value).
I would like to simplify the configuration have a single factory bean (remove "XYZ2" and rid of link to "aName"):
<bean id="XYZ" class="FactoryBean1" scope="prototype">
<property name="steps">
<bean class="FactoryBean2"/>
</property>
</bean>
<bean id="aName" class="com.package.ClassName1" scope="prototype">
<property name="objectContext">
<bean class="com.package.ABC"/>
</property>
</bean>
<bean id="aName2" class="com.package.ClassName1" scope="prototype">
<property name="objectContext">
<bean class="com.package.QWE"/>
</property>
</bean>
Unfortunately, it's not as simple as I expect. I suppose to glue factory (i.e. XYZ bean from the example) with necessary objects (i.e. "aName", "aName2") at runtime.
The approach doesn't work because when I ask Spring for FactoryBean object it returns to me FactoryBean.getObject() which impossible to instanciate at that time because of missing itemReader value.
I hope that SpringSource foresee my case I can somehome "hook" FactoryBean.getObject() call to provide all necessary properties at runtime.
Another complexity that disturb me a bit it's chains of Factories (Factory1 get an object from Factory2 that I have to "hook" at runtime).
Any ideas will be appreciated.
It's the & (ampersand), not the At-symbol, see Spring Framework documentation: Customizing instantiation logic using FactoryBeans
<property name="factoryBean" ref="&theFactoryBean" />
You can get the factory bean itself using the & syntax in the spring config:
<property name="factoryBean" ref="&theFactoryBean" />
as opposed to:
<property name="createdBean" ref="theFactoryBean" />