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.
Related
How do I use PropertyPlaceHolderConfigurer in the below when the property to be injected is a list. I know how it works with annotations. Need to understand its working with xml.
<bean id="attributes" class="com.test.SamlAttributes">
<property name="groups">
<list>
<value>group1</value>
</list>
</property>
<property name="attribute">
<list>
<value>value3</value>
</list>
</property>
</bean>
I am currently using database parameters in hibernate(Version 4.0.1) file through properties file.
I want to use some database parameters from environment variable. How can I fetch values from java files and set in to xml files before that loads in context.
<bean id="propertyConfigurer"
class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer">
<constructor-arg ref="configurationEncryptor" />
<property name="locations">
<list>
<value>classpath:/test/demo/prop/DataParam.properties</value>
</list>
</property>
</bean>
<bean id="data" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass">
<value>${driverClass}</value>
</property>
<property name="jdbcUrl">
<value>${dbconnecturl}</value>
</property>
.
.
.
</beans>
I got some idea to make an object of Configuration class but I don't know where to write that code and how it would be implemented.
You need to use spring expression language to configure the properties from OS environemnt variables as shown below:
<bean id="propertyConfigurer"
class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer">
<constructor-arg ref="configurationEncryptor" />
<property name="locations">
<list>
<value>classpath:/test/demo/prop/DataParam.properties</value>
</list>
</property>
</bean>
<bean id="data" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass">
<value>#{ systemProperties['driverClass']}</value>
</property>
<property name="jdbcUrl">
<value>#{ systemProperties['dbconnecturl']}</value>
</property>
.
.
.
</beans>
You can use as <property name="username" value="#{systemProperties['dbUsername']}"/> for instance.
The variable systemProperties is predefined, you can see Xml Based Configuration for more details.
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>
I have a prototype bean 'client' for which I want to call a method 'addHandler' every time it is instantiated. I am using 'MethodInvokingFactoryBean' for this. I the docs, I can see that by default MethodInvokingFactoryBean operates in a singleton fashion and caches the result of 'getObject' to return in subsequent calls.
To circumvent this, I want to call 'setSingleton' with 'false'. I am not sure about how to do this.
<bean id="provider" class="com.example.credProvider">
<constructor-arg value="key1"/>
</bean>
<bean id="handler" class="com.example.Handler"/>
<bean id="client" class="com.example.DBClient" scope="prototype">
<constructor-arg><ref local="provider"/></constructor-arg>
<property name="endpoint" value="http://xyz:8080/" />
</bean>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject"><ref local="client"/></property>
<property name="targetMethod"><value>addHandler</value></property>
<property name="arguments">
<ref local="handler" />
</property>
</bean>
Will adding a scope="prototype" like this help? -
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" scope="prototype">
...
</bean>
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.