Inject spring dependency in abstract super class - spring

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

Related

Can we use Autowiring byName and byType on same bean? if yes then how?

I have tried autowiring byName and byType for different beans and it's working fine. But I want to use byName and byType autowiring on the same bean, can it be done? I have tried several way but it doesn't work.
<beans>
<bean id="car" class="com.Car" autowire="byName">
<property name="carName" value="BMW" />
</bean>
<bean id="wheel" class="com.Wheel">
<property name="wheelType" value="with Tube" />
</bean>
<bean id="wheel1" class="com.Wheel">
<property name="wheelType" value="tubeless" />
</bean>
<bean id="Byke" class="com.Byke" autowire="byType">
<property name="bykeName" value="Honda" />
</bean>
<bean id="Wheel" class="com.Wheel">
<property name="wheelType" value="No Information" />
</bean>
</beans>
In short answer is NO. For explanation see the relevant API doc which clearly takes only one argument for type of autowiring you need viz. autowireMode For quick reference pasting signature - java.lang.Object createBean(java.lang.Class<?> beanClass, int autowireMode ,boolean dependencyCheck) throws BeansException

Spring DI : session vise object creation using aop proxy for inherited class

In my project i have used struts2 and spring. Spring is used just for DI. I am having all my actions created by sessions and so as model beans via spring DI. Now I want to use inheritance for my class which will be generated through aop based proxy and it will be per session. Coding goes as below.
<bean id="common.EshopDefaultAction" class="com.common.actions.EshopDefaultAction" scope="session" >
<property name="site" ref="master.site" />
<property name="menu" ref="master.menu" />
<property name="indexDAO" ref="common.indexDAO" />
<property name="categoryDAO" ref="master.categoryDAO" />
<property name="productDAO" ref="master.productDAO" />
</bean>
<bean id="common.IndexAction" parent="common.EshopDefaultAction" class="com.common.actions.IndexAction" scope="session">
<property name="indexInfo" ref="common.indexInfo" />
<aop:scoped-proxy />
</bean>
both actions are having pairs of setters and getters.
I want to have site,menu, indexDAO, categoryDAO, productDAO objects created by session for all of its child like IndexAction just shown above. Right now it creates different objects for EshopDefaultAction and IndexAction.
Adding <aop:scoped-proxy /> to EshopDefaultAction bean definition gives error like
Invalid property 'targetBeanName' of bean class [com.common.actions.IndexAction]:
Bean property 'targetBeanName' is not writable or has an invalid setter method.
Does the parameter type of the setter match the return type of the getter?

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

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>

spring bean initializing instances differently via property wiring

I have the following properties in a property file:
context1.property1=value1
context1.property2=value2
context1.property3=value3
context2.property1=value4
context2.property2=value5
context2.property3=value6
I have a bean with the following structure:
class Bean {
private property1;
private property2;
private property3;
}
Is there any way better to initialize 2 instances of Bean without writing something like:
<bean id="bean1" class="com.test.Bean">
<property name="property1" value="${context1.value1}" />
<property name="property2" value="${context1.value2}" />
<property name="property3" value="${context1.value3}" />
</bean>
<bean id="bean2" class="com.test.Bean">
<property name="property1" value="${context2.value1}" />
<property name="property2" value="${context2.value2}" />
<property name="property3" value="${context2.value3}" />
</bean>
Thanks!
Have a look at PropertyOverrideConfigurer:
Property resource configurer that overrides bean property values in an application context definition. It pushes values from a properties file into bean definitions.
Configuration lines are expected to be of the following form:
beanName.property=value
Example properties file:
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql:mydb
See also
Using PropertyOverrideConfigurer with Annotated Classes in Spring 3

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