Bruno Melloni wrote:
> The problem was the classpath - it includes WEB-INF/classes but not
> WEB-INF. I ended up reading the bean from the CXF context file with
> the following code:
> 
> ApplicationContext ctx = new ClassPathXmlApplicationContext(
> "../applicationContext-ws.xml" ); myBean =
> (MyBeanClass)ctx.getBean("myBeanID");
> 
> The key was the "../".
> 
> Still, even though this works, it doesn't feel right.  There must be
> a cleaner way of reading context files that reside in WEB-INF, or
> even more specifically - to read (or even autowire) beans from the
> CXF context file - just like you do in Spring MVC.

This isn't right - what you're doing there is creating a whole new
ApplicationContext that just happens to be based on the same bean
definition files as the main context (created for you by the
ContextLoaderListener).  You already have a context including all the
bean definitions in WEB-INF/applicationContext-ws.xml plus those in
WEB-INF/classes/applicationContext-app.xml, which was loaded at startup
by the ContextLoaderListener.

The correct way to proceed depends on what you want to do.  I presume
your applicationContext-ws.xml contains something like

<jaxws:endpoint implementor="my.example.WebServiceImpl" ... />

and you want to get at your Spring beans from inside your WebServiceImpl
class?  If that is the case then the easiest thing to do is to change it
to say

<jaxws:endpoint implementor="#webServiceImpl" ... />

<bean id="webServiceImpl" class="my.example.WebServiceImpl">
</bean>

This makes your web service object a Spring bean in its own right rather
than having CXF create it by reflection.  So now if you want a reference
to myBean within your service class, you can do it in the normal Spring
style - add a public void setSomething(MyBean b) method to the
WebServiceImpl class and add <property name="something" ref="myBean" />
inside the webServiceImpl <bean> definition.  You don't need to call
getBean directly.

Does this help?

Ian

-- 
Ian Roberts               | Department of Computer Science
[email protected]  | University of Sheffield, UK

Reply via email to