Inject DAO into CXF Service
cxf, dependency-injection, java, spring, web-services
Solution
You are right, there are actually two instances of your `BlogEntriesImpl` class, one created by Spring and one by Apache CXF. You must explicitly ask Apache CXF to use Spring bean rather than providing a class. Check out Writing a service with Spring, looks like you have to replace:
<jaxws:endpoint id="blogService" implementor="blog.BlogEntriesImpl" address="/Blog1" />
with:
<jaxws:endpoint id="blogService" implementor="#blogService" address="/Blog1" />
If Apache CXF can't find bean with name `blogService`, consider moving it to main context from Spring MVC context (`app-servlet.xml`).
Problem
I am trying to inject a DAO into an CXF Service. For this purpose I am using xml configuration. In my app-servlet.xml I've added 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 is instanciated on the start and it has the dao attribute. I would say it is done by the bean configuration from the app-servlet.xml. However, when I call the service, a NullPointerException is thrown. Here is another instance of BlogEntriesImpl being used. To solve the problem I declared the dao attribute in the service implementation class (BlogEntriesImpl) as static. The variable is set on the start of the application. But I don't like this solution. Is there a better way to inject a dao into the CXF service? Thank you in advance!