Spring parent and child resources - spring

I am using spring+struts2 and I have a large number of action classes that required a single service (mailService). Instead of injecting service into these action classes, I am wondering is there a way to create a parent class and inject the resources and then have all these classes extending the parent. So I do no need to repeat the injection in each class.
Thanks in advance

try defining a baseClass with a setter method for the mailService. your action classes can all extend this baseClass.
ie
<!-- parent class with the mailService injected -->
<bean name="baseAction" class="..." abstract="true">
<property name="mailService" ref="mailService"/>
</bean>
<bean name="someAction" class="..." parent="baseAction" >
</bean>

Related

Spring injecting one reference bean into another reference bean

In the below spring configuration snippet, injecting reference of "SomeManager" into bean "SomeWorker" and "SomeLocal". The reference of "SomeLocal" is also injected into "SomeWorker".
My doubt is is it possible to inject the same reference of "SomeManager" injected into "SomeWorker" into another bean, like here in "SomeLocal".
Problem is if i inject it separately into "SomeWorker" and "SomeLocal" than unnecessarily there will be two instances of "SomeManager" which is basically not required in this scenario as "SomeLocal" is referred only within the "SomeWorker"
<bean id="SomeWorker" class="com.test.worker.SomeWorker">
<property name="someManager" ref="SomeManager" />
<property name="someLocal" ref="SomeLocal"/>
<bean id="SomeLocal" class="com.test.local.SomeLocal">
<property name="someManager" ref="SomeManager" />
</bean>
<bean id="SomeManager" class="com.test.manager.SomeManager"/>

overriding bean configuration in spring

Let's say I have two modules. One is core and another is core dependent implementation module.
Core is a jar file for that dependent implementation module war.
In the core I have a bean defined like
<bean id="x" class="com.pokuri.X">
<property name="y" ref="y"/>
<property name="z" ref="z"/>
</bean>
And that class has a method as follows
public class X{
public void doSomeJob(){
.......
}
}
this method is being called from some core classes. Now I need to alter the logic in that doSomeJob() method of X as per my core dependent implementation. So, I create a class like this
public class ExtX extends X{
#override
public void doSomeJob(){
// changed logic
}
}
and defined the bean with same id in another application context xml file like this.
<bean id="x" class="com.pokuri.ExtX">
<property name="y" ref="y"/>
<property name="z" ref="z"/>
</bean>
and we are building application context using contextConfigLocation context parameter in web.xml specifying value as classpath:springfolder.
But in the core logic I am getting core bean instance only(i.e X instance) not ExtX. How can we override that bean definition and let system start using new extend bean definition?
And I heard that with same ID in different application context files will override first loaded bean definition with later loaded bean definition. Is there any priority kind of attribute on bean definition to let ApplicationContext use highest priority one to consider over low priority one when beans with same ID were found.
One way of overriding the bean definition is what you have indicated - to define it with the same id multiple times and the last bean definition with the same id is the one which takes effect. So if you ensure that ExtX is the last one loaded up, it should just work, and to ensure this you can do this in your war file, instead of loading up by saying classpath:springfolder, you can explicitly import the core configuration in your war's Spring config file and then override the bean this way:
<import resource="core-resource.xml"/>
<bean id="x" class="com.pokuri.ExtX">
<property name="y" ref="y"/>
<property name="z" ref="z"/>
</bean>
This will ensure that your overridden bean is the one which takes effect.
There is no priority/order field that you can make use of here though - if you want you can load up all bean definitions of a type by providing Map<String,X> as a parameter, and sort it by expecting an order property and use it that way, but there is lot more work to it.
A second approach is described here: Overriding the bean defined in parent context in a child context

I still don't get how to avoid the use of getBean()

I'm new to Spring and I've read many guides on how to inject beans. Curiously, in every example I see, they use getBean in a class main method (not what I need). Also I've read many forums and questions related to how not to use getBean but I still can't figure out the best approach for my app.
I'm refactoring a web app that is highly coupled and without design patterns. Every business class has a corresponding DAO class, every DAO class extends a super DAO which handles the connection and other stuff. The problem here is that every DAO needs, in the constructor, some database config parameters that are being passed from the business class. What I'm trying to do is to put these parameters in a DBConfig bean and inject them into every DAO allowing me to create the DAO object from every business class simply, for example: dao = new myDAO().
How can I inject the DBConfig bean into every DAO "automatically"? Should I use getBean in the super DAO?
Your config could look like this:
<bean id="daoConfig1" class="com.foo.dao.DAOConfig">
<property name="dbUrl" value="jdbc://urlForDao1" />
...
</bean>
<bean id="dao1" class="com.foo.dao.DAO1">
<constructor-arg ref="daoConfig1" />
</bean>
<bean id="business1" class="com.foo.service.Business1">
<property name="dao" ref="dao1" />
</bean>
<bean id="daoConfig2" class="com.foo.dao.DAOConfig">
<property name="dbUrl" value="jdbc://urlForDao2" />
...
</bean>
<bean id="dao2" class="com.foo.dao.DAO2">
<constructor-arg ref="daoConfig2" />
</bean>
<bean id="business2" class="com.foo.service.Business2">
<property name="dao" ref="dao2" />
</bean>
Or share a single daoConfig instance between all daoX beans, if that's what you want.
You can then use the folowing to handle the business logic:
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"beans.xml"});
Business1 b1 = (Business1) context.getBean("business1");
b1.doStuff();
Or better still, use something like Spring MVC that can wire the business beans into your controllers without needing to call getBean().

Spring - List references a DOA method to get its values

I am brand new to spring and I am trying to write my first spring application.
I have set up a DOA class that accesses the DB and pulls a list of values. I would like to reference those values in a bean definition.
For Example:
I have DAO class called "JdbcDataDAO" that contains a method getValues() - I would like to reference the values in a standalone list in my bean definitions
Here is what I have:
<bean id="dataDAO" class="dao.impl.JdbcDataDAO">
<property name="dataSource" ref="dataSource"/>
</bean>
<util:list id="myList" list-class="java.util.List">
<value>#{dataDAO.values}</value>
</util:list>
But when I retrieve the bean "myList", it contains "#{dataDAO.values}" and not the values
Any help would be appreciated - Thanks
Note sure if you can do this with SpEL. And it doesn't look good anyway - you are mixing infrastructure/configuration with business logic.
You can have a factory-bean or a #Configuration class with #Bean methods where you can inject the DAO and programatically populate the list.
You can also have a BeanPostProcessor that takes all List beans an fills them with whatever you want.

Create bean of type Set<Class<?>>

How can I create a bean of type Class?
I found a way using getClass() but that requires an instance and cannot be used via factory-method since it is not static. It also requires an extraneous bean be created for this express purpose:
<bean id="foo" class="Foo" />
<bean id="fooClass" factory-bean="foo" factory-method="getClass" />
This isn't so bad if the Foo class is easy to construct, but what if the constructor has required parameters?
I then need to create a Set of Class to wire into another bean via a property. I would create the Set such as:
<util:set id="classSet">
<ref local="fooClass"/>
</util:set>
If you really wanted to do what you describe, then you can do it like this:
<bean id="myClass" class="java.lang.Class" factory-method="forName">
<constructor-arg value="com.MyClass"/>
</bean>
But as #ChssPly76 said, if you want to inject it into another bean, you only need inject the class name, and Spring will auto-convert it into a class instance for you.
Why would you? Can you provide an example where that's actually needed?
If you only need this as a dependency (e.g. some other bean has a property of type Class), Spring's built-in ClassEditor property editor would convert a regular string into a Class instance with that name for you:
<property name="someClass" value="java.lang.String"/>
The above would result in setSomeClass(Class clazz) setter being called on the bean whose property that is.

Resources