After switching to Spring 3.1 Eclipse started to complain about our current implementation of TokenBasedRememberMeServices. The class extending it has a parameterless constructor which has been deprecated. The new constructor accepts two params.
An extract from applicationContext-security.xml :
<bean id="rememberMeServices" class="MyRememberMeServices"
p:key="${rememberMeServices.key}">
<property name="userDetailsService" ref="userDetailsService"/>
</bean>
What's the easiest way of moving to the new API?
Use constructor injection:
<bean id="rememberMeServices" class="MyRememberMeServices">
<constructor-arg value="${rememberMeServices.key}" />
<constructor-arg ref="userDetailsService" />
</bean>
Obviously you'd add a corresponding constructor in your MyRememberMeServices which calls the parent class.
Related
While migrating from EJB with spring to POJO , I read every where that just changing this configuration will work :
<bean id="sapFeedBean" class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean" lazy-init="true">
<property name="jndiName" value="ejb/sapBean" />
<property name="resourceRef" value="false" />
<property name="businessInterface" value="com.aa.inflightsales.sap.SapBusiness" />
<property name="lookupHomeOnStartup" value="false" />
</bean>
but how to do this , I am trying to create bean of the POJO class , but how can I define the business interface , as interface injection is not supported by spring.
Just define the business methods in this interface which should be implemented by the business class and when you need it just use a reference to the interface with the convenient annotation and the framework will inject the implementation.
I want to populate a value of my spring bean with the return value of a method. Is there any way I can do this?
<bean id="JmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="defaultDestinationName"
value="#Value#returned#by#method" />
I already have a bean of the class which has the method in my application context.
<bean id=xyz class=path.to.xyz>
</bean>
Please note that the value that I want to inject is not a variable, but return value of a method.
You can use factory-bean and factory-method:
<bean id="JmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="defaultDestinationName">
<bean factory-bean="xyz" factory-method="methodName" />
</property>
</bean>
If the method you want to call takes parameters you can pass them in using constructor-arg
<bean factory-bean="xyz" factory-method="methodName">
<constructor-arg index="0" value="firstParameter" />
<constructor-arg index="1" ref="someOtherBean" />
</bean>
This can be achieved with Spring expression language
<bean id="b1" class="B1">
</bean>
<bean id="b2" class="B2">
<property name=xxx" value="#{b1.xxx}" />
</bean>
you can just do
<bean id="JmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="defaultDestinationName"
value="#{className.methodName()}" />
The spring container will call the geter method for that property
Note: you woudl have to autowire path.to.xyzConfig using #autowire
References 1 : Look for this : 6.4 Expression support for defining bean definitions
This question could help
In short, you could use a factory method to return the value you want.
I am trying to set up HostConfiguration bean. One of the property it has is called proxyHost. However, the apache HostConfiguration class does not follow the java beans convention. The setter for the proxyHost accepts an argument of type ProxyHost whereas the getter returns a String.
I have the following snippet in my applicationContext.xml.
<bean id="proxyHost" class="org.apache.commons.httpclient.ProxyHost">
<constructor-arg index="0" type="java.lang.String" value="myproxy.com" />
<constructor-arg index="1" type="int" value="8087" />
</bean>
<bean id="hostConfiguration" class="org.apache.commons.httpclient.HostConfiguration">
<property name="proxyHost" ref="proxyHost" />
</bean>
When I try to load the applicationContext for the app I get the following error since HostConfigurationClass does not have a getProxyHost that returns a ProxyHost or a setter that takes a String:-
org.springframework.beans.NotWritablePropertyExcep tion: Invalid property 'proxyHost' of bean class [org.apache.commons.httpclient.HostConfiguration]: Bean property 'proxyHost' is not writable or has an invalid setter method: Does the parameter type of the setter match the return type of the getter?
While searching on the springsource forum I came across this thread where it was recommended to use MethodInvokingFactoryBean to solve this.
I am not exactly sure how using MethodInvokingFactoryBean would help since I would need a return type of ProxyHost from the method getProxyHost() to fix this, right? And I am not also sure about how to use it in this context. I am not clear on the internals of MethodInvokingFactoryBean. Therefore, if someone could please give me an example in the above context how to use MethodInvokingFactoryBean that would be of immense help.
Also is this generally the accepted way to set up beans like HostConfiguration that do not follow convention in spring?
Thanks!
First , instantiate the ProxyHost
(i.e. ProxyHost proxyHost = new ProxyHost("myproxy1.com",8080);
<bean id="proxyHost" class="org.apache.commons.httpclient.ProxyHost">
<constructor-arg index="0" type="java.lang.String" value="myproxy1.com" />
<constructor-arg index="1" type="int" value="8088" />
</bean>
Then instantiate the HostConfiguration object
(i.e. HostConfiguration hostConfiguration = new HostConfiguration();
<bean id="hostConfiguration" class="org.apache.commons.httpclient.HostConfiguration" />
After that , use the MethodInvokingFactoryBean to call setProxyHost() on the HostConfiguration and pass the proxyHost as argument.
(i.e. hostConfiguration.setProxyHost(proxyHost);)
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<ref local="hostConfiguration"/>
</property>
<property name="targetMethod">
<value>setProxyHost</value>
</property>
<property name="arguments">
<ref local="proxyHost"/>
</property>
</bean>
As mentioned in the other answer you can implement a FactoryBean. If you are using spring 3.0 you could also take a look at the Java Configuration - #Configuration / #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" />
Is it possible to set the property of one bean by reading the property of another bean? For instance, suppose I had:
class A {
void setList(List list);
}
class B {
List getList();
}
I would like Spring to instantiate both classes, and call A's setList method, passing in the result of calling B's getList method. The Spring configuration might look something like:
<bean id="b" class="B"/>
<bean id"a" class="A">
<property name="list" ref="b" ref-property="list"/>
</bean>
Alas, this made-up XML does not work.
Why not just inject B into A? Because I do not want to introduce the extra dependency. A is only dependent List, not on B.
in addition to #Kevin's answer if you are using spring 3.0 it is possible to do this with the new spring expression language
<bean id="a" class="A">
<property name="list"
value="#{b.list}"/>
</bean>
spring 3.0 documentation
There are a couple of ways. Here is one:
<bean id="b" class="B"/>
<bean id="a" class="A">
<property name="list">
<bean class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetObject" ref="b"/>
<property name="propertyPath" value="list"/>
</bean>
</property>
</bean>
Also see the <util:property-path/> element
If you are trying to do the same for a constructor then do this.
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg type="javax.sql.DataSource" value="#{jdbc.dataSource}">
</constructor-arg>
</bean>
Here "jdbc" is as mentioned below that has property "dataSource" with getter and setter and initilized as:
<bean id="jdbc" class="com.la.activator.DataSourceProvider">
<property name="myDataSourcePool" ref="dsPoolService"/>
</bean>