How would I specify a bean reference as an argument-resolver, in Spring 3-1? - spring

This is a syntax question about a Spring configuration (refer to spring-beans-3.1.xsd and spring-mvc-3.1.xsd).
I have a specific bean definition (id="SecurityRequestParametersArgumentResolver") that I want to register as a custom argument resolver. The xml fragment is:
<bean id="SecurityRequestParametersArgumentResolver"
class="xxx.security.web.SecurityRequestParametersArgumentResolver">
<property name="credentialsManager" ref="CredentialsManager" />
<property name="tokenService" ref="TokenService" />
</bean>
... AND I would like to use a bean reference. The following three lines don't obey the xsd-grammar ( what should be the correct tag declaration here? )
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean ref="SecurityRequestParametersArgumentResolver"/>
</mvc:argument-resolvers>
</mvc:annotation-driven>
... All of the examples I have seen look like THIS, and thus are going after the default no-argument constructor
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="class="xxx.security.web.SecurityRequestParametersArgumentResolver"/>
</mvc:argument-resolvers>
</mvc:annotation-driven>
Repeating the question, what should the Spring syntax be in order to use a bean reference as a custom argument resolver?
Thanks!

At least the constructor argument problem can be solved easily
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="class="xxx.security.web.SecurityRequestParametersArgumentResolver">
<constructor-arg value="123"/>
</bean>
</mvc:argument-resolvers>
</mvc:annotation-driven>
But I do not know any way to use a reference instead of a bean within <mvc:argument-resolvers>. But depending on what you want to do, there are several workarrounds.
Use a Bean Factory
Create a proxy class that implements the HandlerMethodArgumentResolver and forward every call to an other instance, then you can have your own SecurityRequestParametersArgumentResolver like a normal bean and register the Proxy in the <mvc:argument-resolvers> section.
Proxy:
class HandlerMethodArgumentResolverProxy
implements HandlerMethodArgumentResolver{
HandlerMethodArgumentResolver delegate;
publicHandlerMethodArgumentResolverProxy
(HandlerMethodArgumentResolver delegate){
this.delegate=delegate;
}
public boolean supportsParameter(MethodParameter parameter) {
this.delegate.supportsParameter(parameter);
}
//delegate for resolveArgument
}
config:
<bean id="securityRequestParametersArgumentResolver"
class="xxx.security.web.SecurityRequestParametersArgumentResolver">
...
</bean>
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class "HandlerMethodArgumentResolverProxy">
<constructor-arg
ref="securityRequestParametersArgumentResolver"/>
</bean>
</mvc:argument-resolvers>
</mvc:annotation-driven>

Related

Spring: Setting a property annotated with #Resource

This following does not behave like I expect, using Spring 3.2.1.RELEASE, groovy 1.8.8. I've elided quite a bit of code. If this isn't sufficient, I'll put together a (not) working full example.
groovy bean:
package foo
import javax.annotation.Resource
class SomeBean {
#Resource
String someProp
}
spring xml:
<context:annotation-config />
<context:component-scan base-package="other.packages.not.foo" />
<bean id="someBean" class="foo.SomeBean">
<property name="someProp" value="bar" />
</bean>
This fails with an error about not being able to find a bean to satisfy the String property. To work around this, I ended up doing this:
groovy bean:
#Resource( name = 'someProp' )
String someProp
spring xml:
<bean id="someProp" class="java.lang.String">
<constructor-arg value="bar" />
</bean>
<bean id="someBean" class="foo.SomeBean" />
I don't like this solution, because it couples the bean source code to the spring config, and that's coupling in the wrong direction. And it creates an unnecessary bean for a simple String. Am I missing an obvious solution?
#Resource is not meant for property injection.
For this to work:
<bean id="someBean" class="foo.SomeBean">
<property name="someProp" value="bar" />
</bean>
you need to implement a setter method
public void setSomeBean(String value) {
someValue = value;
}
#Resource could be used if you want to inject a different bean from your context.
#Resource(name="someBean")
SomeBean someBean

How to add a param to a factory-method of a factory-bean in Spring?

let's say I have a factory bean:
<bean id="myFactory" class="com.company.MyFactory" lazy-init="true">
<property name="myProperty" ref="propA">
</bean>
Let's say propA is a bean injected by IOC used in the factory method. And I have 2 beans generated from this factory:
<bean id="bean1" factory-bean="myFactory" factory-method="instance"/>
<bean id="bean2" factory-bean="myFactory" factory-method="instance"/>
How can I make bean2 to use a different myProperty than bean1 without using a different factory method? Or, how can I pass propA as a parameter to the factory-method from the bean1 or bean2 configuration?
This can be achieved in a slightly different way:
class MyFactory {
public Bean instance(MyProperty myProperty) {
return //...
}
}
Now you can use counterintuitive syntax like following:
<bean id="bean1" factory-bean="myFactory" factory-method="instance">
<constructor-arg ref="propA"/>
</bean>
<bean id="bean2" factory-bean="myFactory" factory-method="instance">
<constructor-arg ref="propB"/>
</bean>
Believe it or not but propA and propB will be used as instance() method arguments.

Is there a better way to Configure Bean?

Can the following Spring DI xml be improved? Below the xml is the programmatic approach of configuring the target bean.
<bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />
<bean id="jacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig"
factory-bean="jacksonObjectMapper" factory-method="getSerializationConfig" />
<bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="jacksonSerializationConfig" />
<property name="targetMethod" value="setSerializationInclusion" />
<property name="arguments">
<list>
<value type="org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion">NON_NULL</value>
</list>
</property>
</bean>
ObjectMapper mapper = new
ObjectMapper();
mapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
XML is a really bad way of doing this. Yes, you can do this, but it's much easier to write a FactoryBean which configures your ObjectMapper:
public class MyObjectMapperFactoryBean extends AbstractFactoryBean<ObjectMapper> {
public Class<ObjectMapper> getObjectType() {
return ObjectMapper.class;
}
public ObjectMapper createInstance() {
// create and return ObjectMapper
}
}
and then in your XML:
<bean id="jacksonObjectMapper" class="x.y.MyObjectMapperFactoryBean" />
Still not totally ideal, but a little cleaner:
<bean id="objectMapperBuilder1" class="org.codehaus.jackson.map.ObjectMapper"/>
<bean id="objectMapperBuilder2" factory-bean="objectMapperBuilder1" factory-method="setSerializationInclusion">
<constructor-arg value="NON_NULL"/>
</bean>
<bean id="jsonWriter" factory-bean="objectMapperBuilder2" factory-method="writerWithDefaultPrettyPrinter" />
<!-- etc, etc -->
One downside is that you'll have unnecessary bean instances in memory. (I'm using this method, and I'll live with it until Spring decides to handle these). There are many threads here and on the Spring forums asking for support with fluent setters like the builder pattern used by Jackson, but until then you have to choose the lesser evil for you.
I agree with #skaffman's general approach of using a FactoryBean in place of the unavoidably convoluted Spring XML bean configuration to configure a Jackson ObjectMapper. Spring 3.2+ now provides such a FactoryBean out of the box. See JacksonObjectMapperFactoryBean / Jackson2ObjectMapperFactoryBean. Here's an example of the Spring XML to configure the ObjectMapper via the Spring FactoryBean -
<bean id="jacksonObjectMapper" factory-bean="&jacksonObjectMapperFactoryBean" factory-method="getObject"/>
<bean id="jacksonObjectMapperFactoryBean" class="org.springframework.http.converter.json.JacksonObjectMapperFactoryBean">
<property name="featuresToDisable">
<array>
<util:constant static-field="org.codehaus.jackson.map.SerializationConfig$Feature.WRITE_NULL_PROPERTIES"/>
</array>
</property>
</bean>
(Note the need to use &amp in the 'factory-bean' attribute to instruct Spring to use the factory method on the FactoryBean itself, rather than the bean it creates).

Inject spring dependency in abstract super class

I have requirement to inject dependency in abstract superclass using spring framework.
class A extends AbstractClassB{
private Xdao daox ;
...
public setXdao() { ... }
}
class AbstractClassB{
..
private yDao daoy;
public seyYdao() { ... }
}
I need to pass superclass dependency everytime i instantiate Abstract class B (which can be subclassed in 100's of ways in my project)
entry in application.xml (spring context file)
<bean id="aClass" class="com.mypro.A"
<property name="daox" ref="SomeXDaoClassRef" />
<property name="daoy" ref="SomeYDaoClassRef"/>
</bean>
How can i just create bean reference of super class AbstractClassB in application.xml so that i can use it in all subclass bean creation?
You can create an abstract bean definition, and then "subtype" that definition, e.g.
<bean id="b" abstract="true" class="com.mypro.AbstractClassB">
<property name="daox" ref="SomeXDaoClassRef" />
</bean>
<bean id="a" parent="b" class="com.mypro.A">
<property name="daoy" ref="SomeYDaoClassRef" />
</bean>
Strictly speaking, the definition for b doesn't even require you to specify the class, you can leave that out:
<bean id="b" abstract="true">
<property name="daox" ref="SomeXDaoClassRef" />
</bean>
<bean id="a" parent="b" class="com.mypro.A">
<property name="daoy" ref="SomeYDaoClassRef" />
</bean>
However, for clarity, and to give your tools a better chance of helping you out, it's often best to leave it in.
Section 3.7 of the Spring Manual discusses bean definition inheritance.
You can use the abstract flag of Spring to tell Spring that a class is abstract. Then all concrete implementations can simply mark this bean as their parent bean.
<bean id="abstractClassB" class="AbstractClassB" abstract="true">
<property name="yDao" ref="yDao" />
</bean>
<bean id="classA" class="A" parent="abstractClassB">
<property name="xDao" ref="xDao" />
</bean>
Have an abstract parent bean:
http://forum.springsource.org/showthread.php?t=55811

How do you acess a property of a bean for reading in a spring xml config file?

I want to do something like the following in spring:
<beans>
...
<bean id="bean1" ... />
<bean id="bean2">
<property name="propName" value="bean1.foo" />
...
I would think that this would access the getFoo() method of bean1 and call the setPropName() method of bean2, but this doesn't seem to work.
What I understood:
You have a bean (bean1) with a
property called "foo"
You have another bean (bean2) with a
property named "propName", wich also
has to have the same "foo" that in
bean1.
why not doing this:
<beans>
...
<bean id="foo" class="foopackage.foo"/>
<bean id="bean1" class="foopackage.bean1">
<property name="foo" ref="foo"/>
</bean>
<bean id="bean2" class="foopackage.bean2">
<property name="propName" ref="foo"/>
</bean>
....
</beans>
Doing this, your bean2 is not coupled to bean1 like in your example. You can change bean1 and bean2 without affecting each other.
If you REALLY need to do the injection you proposed, you can use:
<util:property-path id="propName" path="bean1.foo"/>
You need to use PropertyPathFactoryBean:
<bean id="bean2" depends-on="bean1">
<property name="propName">
<bean class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetBeanName" value="bean1"/>
<property name="propertyPath" value="foo"/>
</bean>
</property>
</bean>
I think you have to inject bean1, then get foo manually because of a timing issue. When does the framework resolve the value of the target bean?
You could create a pointer bean and configure that.
class SpringRef {
private String targetProperty;
private Object targetBean;
//getters/setters
public Object getValue() {
//resolve the value of the targetProperty on targetBean.
}
}
Common-BeanUtils should be helpful.

Resources