You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Nishant Chandra <ni...@gmail.com> on 2009/09/20 13:42:06 UTC
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
Re: DI in an Impl class
Posted by Mick Knutson <mi...@gmail.com>.
In your applicationContext you inject the dao like:
<!-- Services Bean... -->
<bean id="messageService"
class="com.baselogic.service.impl.MessageServiceImpl">
<property name="myDataAccessService" ref="myDataAccessService" />
</bean>
<!-- DAO Bean... -->
<bean id="myDataAccessService"
class="com.baselogic.service.impl.MyDataAccessServiceImpl">
</bean>
---
Thank You…
Mick Knutson, President
BASE Logic, Inc.
Enterprise Architecture, Design, Mentoring & Agile Consulting
p. (866) BLiNC-411: (254-6241-1)
f. (415) 685-4233
Website: http://baselogic.com
Linked IN: http://linkedin.com/in/mickknutson
Vacation Rental: http://tahoe.baselogic.com
---
On Sun, Sep 20, 2009 at 4:42 AM, Nishant Chandra
<ni...@gmail.com>wrote:
> 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
>
Re: DI in an Impl class
Posted by Nishant Chandra <ni...@gmail.com>.
Thanks. It worked.
On Mon, Sep 21, 2009 at 1:13 AM, Ron Grimes <rg...@sinclairoil.com> wrote:
> 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 [nishant.chandra@gmail.com]
> Sent: Sunday, September 20, 2009 5:42 AM
> To: users@cxf.apache.org
> 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
--
Nishant Chandra
Hyderabad, India
Cell : +91 9949828480
RE: DI in an Impl class
Posted by Ron Grimes <rg...@sinclairoil.com>.
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 [nishant.chandra@gmail.com]
Sent: Sunday, September 20, 2009 5:42 AM
To: users@cxf.apache.org
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