Deploy DAO to CXF Service

I am trying to inject a DAO into a CXF service. For this, I use the xml configuration.

In my app-servlet.xml, I added the following entry:

<bean id="blogService" class="blog.BlogEntriesImpl"> <property name="blogDao" ref="blogDao" /> </bean> 

blogDao bean is also defined in this file.

The service is configured in another XML file:

  <import resource="classpath:META-INF/cxf/cxf.xml" /> <jaxws:endpoint id="blogService" implementor="blog.BlogEntriesImpl" address="/Blog1" /> 

BlogEntriesImpl implements the service interface. It has a dao attribute and a setter method.

I debugged the application and found out that one instance of BlogEntriesImpl started from the very beginning and had the dao attribute. I would say that this is done using the bean configuration from app-servlet.xml.

However, when I call the service, a NullPointerException is thrown. Here is another example of using BlogEntriesImpl.

To solve the problem, I declared the dao attribute in the service implementation class (BlogEntriesImpl) static. The variable is set at the beginning of the application. But I do not like it.

Is there a better way to embed dao in a CXF service?

Thank you in advance!

+4
source share
1 answer

You are right, in fact there are two instances of your BlogEntriesImpl class, one of which was created by Spring and one by Apache CXF. You should explicitly ask Apache CXF to use the Spring bean instead of providing the class. Check the Service Writing with Spring , it looks like you should replace:

 <jaxws:endpoint id="blogService" implementor="blog.BlogEntriesImpl" address="/Blog1" /> 

with:

 <jaxws:endpoint id="blogService" implementor="#blogService" address="/Blog1" /> 

If Apache CXF cannot find a bean named blogService , move it to the main context from the Spring MVC context ( app-servlet.xml ).

+4
source

All Articles