I want to know how to inject attribute of an Object to another Spring bean in Spring XML configurations? I have declared a bean in configuration file and I want to inject attribute of that bean into another bean.
Update:
Here is the configurations I tried in the spring.xml file.
<bean id="cFactoryBean" class="org.springframework.data.cassandra.config.CassandraClusterFactoryBean>
<property name="contactPoints" value="${cassandra.contactpoints}" />
<property name="port" value="${cassandra.port}" />
</bean>
<bean id="sFactoryBean" class="org.springframework.data.cassandra.config.CassandraSessionFactoryBean>
<property name="cluster" value="${cFactoryBean.object}" />
<property name="keyspaceName" value="${cassandra.keyspace}" />
<property name="converter" ref="cConverterBean" />
</bean>
I have created cFactoryBean bean and I want to inject object property of cFactoryBean to the 'cluster' property in the sFactoryBean.
In the following example the attribute connectionManager of the class Dao
refers to the spring bean dbConnectionManager.
<bean id="feedbackDao" class="com.foo.Dao"
<property name="connectionManager" ref="dbConnectionManager" />
</bean>
<bean id="dbConnectionManager" class="com.foo.dao.ConnectionManager" />
Related
I have handler bean with a url property which needs different value for different class injections .How can I do this without creating multiple beans
<bean id="handler" class="com.Handler">
<property name="url" value="/"/>
</bean>
<bean id="a" class="com.A">
<property name="handler" ref="handler"> for this I need url to be /aaa
</bean>
<bean id="b" class="com.B">
<property name="handler" ref="handler"> for this I need url to be /bbb
</bean>
.....
I do not want to create multiple handler beans
I using spring session HttpSession, how can I custom cookie key, I tried this solution: Custom cookie name when using Spring Session. but it does not work, the name is SESSION still.
my config like below:
<context:annotation-config/>
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>
<context:property-placeholder location="classpath:/env/env_test.properties"/>
<bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:port="${spring.redis.port}" p:hostName="${spring.redis.host}"/>
<bean id="mapSessionRepository" class="org.springframework.session.MapSessionRepository" />
<bean id="sessionRepositoryFilter"
class="org.springframework.session.web.http.SessionRepositoryFilter">
<constructor-arg ref="sessionRepository"/>
<property name="httpSessionStrategy">
<bean class="org.springframework.session.web.http.CookieHttpSessionStrategy">
<property name="cookieName" value="_session_id" />
</bean>
</property>
</bean>
You just need to add below bean to create custom cookie.
<bean class ="org.springframework.session.web.http.DefaultCookieSerializer">
<property name="cookieName" value="JSESIONID"></property>
</bean>
JESSIONID - Custom Cookie Name
Please remove below configuration fr`enter code here`om xml file.
<bean id="sessionRepositoryFilter"
class="org.springframework.session.web.http.SessionRepositoryFilter">
<constructor-arg ref="sessionRepository"/>
<property name="httpSessionStrategy">
<bean class="org.springframework.session.web.http.CookieHttpSessionStrategy">
<property name="cookieName" value="_session_id" />
</bean>
</property>
</bean>
You have to create a bean with class
org.springframework.session.web.http.DefaultCookieSerializer
So inside of that bean, you define your custom properties, after you declare that bean as a property of the following:
org.springframework.session.web.http.CookieHttpSessionStrategy
By example:
<bean id="yourCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer">
<property name="cookieName" value="yourCustomName"/>
<property name="cookiePath" value="yourCustomPath"/>
...
</bean>
<bean id="cookieHttpSessionStrategy" class="org.springframework.session.web.http.CookieHttpSessionStrategy">
<property name="cookieSerializer" ref="yourCookieSerializer"></property>
</bean>
Is it possible to have property resolution on the factory-bean field of a Spring bean declaration?
Example:
<bean factory-bean="$APP{some.factory.bean}" factory-method="...">
Spring version: 3.2.4
Adding a PropertyPlaceholderConfigurer to your spring context should work:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:yourfile.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="false" />
<property name="ignoreUnresolvablePlaceholders" value="false" />
</bean>
You can declare your propertyName in the file and then use it like:
<bean factory-bean="${propertyName}" factory-method="...">
Just encountered the same issue (using Spring 4.2.0). It seems that property resolution for factory beans does not work despite what they say in SPR-12638. I've ended up using ProxyFactoryBean as a workaround.
The desired configuration, which does not work:
<!-- The bean to be created: factory type determined in runtime -->
<bean id="..." factory-bean="factory-${factory.type}" factory-method="..." />
<!-- Possible factories -->
<bean id="factory-A" class="..." />
<bean id="factory-B" class="..." />
<bean id="factory-C" class="..." />
The workaround that I've found:
<!-- The bean to be created: use factory proxy -->
<bean id="..." factory-bean="factory-proxy" factory-method="..." />
<!-- The factory proxy: real factory type determined in runtime -->
<bean id="factory-proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="factory-${factory-type}" />
</bean>
<!-- Possible factories -->
<bean id="factory-A" class="..." />
<bean id="factory-B" class="..." />
<bean id="factory-C" class="..." />
Enjoy :)
I would like to inject one bean into another using an addProperty method. For example, if I have bean A and bean B and bean B has a method addA() but not setter method, how do I inject bean A into B without resorting to annotations.
You could do it like this:
<bean id="a" class="my.A" />
<bean id="b" class="my.B" />
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="b" />
<property name="targetMethod" value="addA" />
<property name="arguments" ref="a" />
</bean>
Of course, this approach doesn't scale well if you've got a lot of bean-wiring to do. If you own the code, you should use Spring annotations and auto-wiring, or define setters and use the 'property' Spring XML tags.
hi i am using spring config.xml to config my embedded database hsqldb. my spring config is as below:
<jdbc:embedded-database id="dataSource" type="HSQL">
<jdbc:script location="classpath:schema.sql" />
</jdbc:embedded-database>
<bean id="adapterDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="datasource" ref="dataSource"></property>
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:file:data/db/lmexadapter_db" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
when run run my appication which is using above spring config.xml it give me a exception as below:
org.springframework.beans.NotWritablePropertyException: Invalid property 'datasource' of bean class [org.apache.commons.dbcp.BasicDataSource]: Bean property 'datasource' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
please help to resolve this as soon as possible
Thank you
The issue is that org.apache.commons.dbcp.BasicDataSource doesn't have a "setDatasource" method. It does have a protected field "datasource", however, which you could expose by subclassing and providing a setter.