H

On Wed, Jan 5, 2011 at 5:38 AM, Andrew <[email protected]> wrote:

> Note: I originally posted this via Nabble, and for some reason it appears
> to
> have been treated as spam, so here it goes again.
>
> Hi,
>
> I'm looking for some guidance on how to inject a Spring bean into a JAX-RS
> service (via Annotation or otherwise).  I have a CXF JAX-RS service which
> is
> working just fine. Problem is when I
> try to inject my DAO using an annotation:
>
> @Resource
> private MyDAO myDAO;
>

This should work, unless the service bean is proxified by Spring. It might
work if CGLIB is used only but I'm not sure.
You can make sure it works by adding a method such as

@Resource
public void setMyDAO(MyDAO dao) {
 myDAO = dao;
}

to the service bean class. If it still does not work then create an
interface with  a single setMyDAO method and have the service class
implementing it.

>
> It's not getting injected. My JAX-RS service is Spring configured like so:
>
> <beans xmlns="http://www.springframework.org/schema/beans";
>     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
>
>     xmlns:jaxws="http://cxf.apache.org/jaxws";
>     xmlns:jaxrs="http://cxf.apache.org/jaxrs";
>     xmlns:cxf="http://cxf.apache.org/core";
>
>     xsi:schemaLocation="
> http://www.springframework.org/schema/beans
> http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
>
> http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
> http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
>
> http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd";>
>
>
>  <import resource="classpath:META-INF/
> cxf/cxf.xml"/>
>
>  <import
> resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>
>  <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
>
>  <jaxrs:server id="message" serviceClass="com.foo.MessageResourceImpl"
> address="/">
>
>   <jaxrs:features>
>        <cxf:logging/>
>   </jaxrs:features>
>  </jaxrs:server>
> </beans>
>
> The DAO is getting initialized by Spring and I have verified the bean works
> in any other POJO, just not the CXF service. Furthermore, I don't see any
> errors in the logs.
>
> I'm using version 2.2.11, in case that helps.
>
>
Having  jaxrs:server/@serviceClass works and is simpler to using
jaxrs:server/jaxrs:serviceBeans but it bypasses the Spring injection
mechanism as the serviceClass is instantiated by CXF. So use
jaxrs:server/jaxrs:serviceBeans and refer to a separate Spring service bean
declaring "com.foo.MessageResourceImpl".

Hope it helps, Sergey

Thanks,
>
> Andrew
>

Reply via email to