Typically, you would not inject your DAO bean into a service class method that
is not specifically a DAO setter method. There are at least two ways to do
this. I tend to use constructor injection, which would look like this:
CONSTRUCTOR INJECTION
<bean id="mDao" class="com.dao.MDaoImpl"/>
<bean id="aService" class="com.service.AServiceImpl">
<constructor-arg index="0" ref="mDao" />
</bean>
@WebService(endpointInterface = "com.service.AService", serviceName="AService")
public class AServiceImpl implements AService
{
private MDao mDao;
public AServiceImple(MDao mDao)
{
super();
this.mDao = mDao;
}
public String aM(String aKey)
{
mDao.someMethod();
}
}
SETTER INJECTION
<bean id="mDao" class="com.dao.MDaoImpl"/>
<bean id="aService" class="com.service.AServiceImpl">
<property name="mDao" ref="mDao" />
</bean>
@WebService(endpointInterface = "com.service.AService", serviceName="AService")
public class AServiceImpl implements AService
{
private MDao mDao;
public String aM(String aKey)
{
mDao.someMethod();
}
public void setMDao(MDao mDao)
{
this.mDao = mDao;
}
}
I prefer constructor injection just because, if you have a few DAO beans to
inject, then the setter methods just add length to the service class (much more
than simple constructor injection)
Ron Grimes
________________________________________
From: Nishant Chandra [[email protected]]
Sent: Sunday, September 20, 2009 5:42 AM
To: [email protected]
Subject: DI in an Impl class
Hi,
I have a WS impl like this:
@WebService(endpointInterface = "com.service.AService", serviceName="AService")
public class AServiceImpl implements AService {
public String aM(String aKey) {
...
}
}
I have a bean definition:
<bean id="mKDao" class="com.dao.MDao"/>
How can I inject this "mKDao" in the aM method above? Will @Resource
work or there is another way?
In the servlet, I could use the statement below and get the bean.
WebApplicationContext ctx =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
Nishant