You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by virajn <ma...@gmail.com> on 2014/12/23 06:08:56 UTC

How to inject spring bean to CXFServlet extented class

I have a rest web service. In that at webservice start i have to start few
other services. I decided to extend the
org.apache.cxf.transport.servlet.CXFServlet class and in that initiate those
services. So far i have extended the class and it's working now.

How can i inject spring bean to this extended class

my servlet

public class RestUIServlet extends CXFServlet {

   @Autowired
   private Util util; // Problem is to inject this.

   public RestUIServlet() {
        super();
       //TODO : Initialize service here
   }
}

My web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/beans.xml</param-value>
</context-param>
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
<servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>
        test.RestUIServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

My beans.xml
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:spring/springContext.xml"/>
<context:property-placeholder/>
<context:annotation-config/>
<bean
class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"/>
<bean
class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"/>

<jaxrs:server id="services" address="/">
    <jaxrs:serviceBeans>
        <ref bean="uiServiceImpl"/>
    </jaxrs:serviceBeans>
    <jaxrs:providers>
        <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
    </jaxrs:providers>
</jaxrs:server>

<bean id="uiServiceImpl" class="test.UIServiceImpl"/>

I'm using cxf 3.0.0



--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-inject-spring-bean-to-CXFServlet-extented-class-tp5752665.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: How to inject spring bean to CXFServlet extented class

Posted by Kaustuv Maji <ka...@gmail.com>.
Hello,

can you try following two steps i hope it will resolve problem.
add <context:component-scan base-package="<package name>" /> in
beans.xml this entry will scan/read all the stareo type af annotations from
the java classes .

add @Component at class level.

Thanks and regards
Kaustuv Maji

On Tue Dec 23 2014 at 10:40:26 AM virajn <ma...@gmail.com> wrote:

> I have a rest web service. In that at webservice start i have to start few
> other services. I decided to extend the
> org.apache.cxf.transport.servlet.CXFServlet class and in that initiate
> those
> services. So far i have extended the class and it's working now.
>
> How can i inject spring bean to this extended class
>
> my servlet
>
> public class RestUIServlet extends CXFServlet {
>
>    @Autowired
>    private Util util; // Problem is to inject this.
>
>    public RestUIServlet() {
>         super();
>        //TODO : Initialize service here
>    }
> }
>
> My web.xml
>
> <context-param>
>     <param-name>contextConfigLocation</param-name>
>     <param-value>WEB-INF/beans.xml</param-value>
> </context-param>
> <listener>
>     <listener-class>
>         org.springframework.web.context.ContextLoaderListener
>     </listener-class>
> </listener>
> <servlet>
>     <servlet-name>CXFServlet</servlet-name>
>     <servlet-class>
>         test.RestUIServlet
>     </servlet-class>
>     <load-on-startup>1</load-on-startup>
> </servlet>
> <servlet-mapping>
>     <servlet-name>CXFServlet</servlet-name>
>     <url-pattern>/*</url-pattern>
> </servlet-mapping>
>
> My beans.xml
> <import resource="classpath:META-INF/cxf/cxf.xml"/>
> <import resource="classpath:spring/springContext.xml"/>
> <context:property-placeholder/>
> <context:annotation-config/>
> <bean
> class="org.springframework.context.support.PropertySourcesPlaceholderConf
> igurer"/>
> <bean
> class="org.springframework.beans.factory.config.
> PreferencesPlaceholderConfigurer"/>
>
> <jaxrs:server id="services" address="/">
>     <jaxrs:serviceBeans>
>         <ref bean="uiServiceImpl"/>
>     </jaxrs:serviceBeans>
>     <jaxrs:providers>
>         <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
>     </jaxrs:providers>
> </jaxrs:server>
>
> <bean id="uiServiceImpl" class="test.UIServiceImpl"/>
>
> I'm using cxf 3.0.0
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.
> com/How-to-inject-spring-bean-to-CXFServlet-extented-class-tp5752665.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>