Friday, August 1, 2008

Referring spring beans in struts action

While defining beans in spring's applicationContext.xml, if we need a bean's reference(ClassB) in another bean(ClassA), we will do the following.

1) Define a variable of ClassB in ClassA
2) Setters for the variable.

3) following beans in applicationContext.xml
<bean id="beanB" class="ClassB" > </bean>
<bean class="beanA">
<property name="varname" ref="beanB" />
</bean>

But in struts action class you can avoid <property name="varname" ref="beanB" /> by simply setting struts.objectFactory = spring in struts.properties, though you should decrale a variable with the name beanB (defined in applicationContext.xml) and should have setter for the same variable.

If your application doesn't have struts.properties, create a new struts.properties and keep it in the classpath

No comments: