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.
-----Original Message-----
From: Ian Roberts [mailto:[email protected]]
Sent: Tuesday, July 14, 2009 11:50 AM
To: [email protected]
Subject: Re: Reading bean from cxf/Spring context file
What do you mean you would like to "read" a custom bean? If you need
access to one bean from another bean in the context then just use the
normal dependency injection mechanism (declare a setFoo method and use
<property name="foo" ref="..." />). If you need the ApplicationContext
within one of your Spring beans (e.g. to look up prototype beans
repeatedly) then you can have your bean implement BeanFactoryAware or
any of the other *Aware interfaces.
If you want access to a Spring bean from a non-Spring class you don't
need to create your own ClassPathApplicationContext; you can use
WebApplicationContextUtils.getWebApplicationContext(ServletContext) to
get a reference to the context that was loaded by the ContextLoaderListener.
If you want more details I'll need a better idea of what exactly you're
trying to do...
Ian
Bruno Melloni wrote:
> I think I am missing the point on something basic. Would somebody enlighten
> me?
>
> I would like to read a custom bean that I added to the cxf/spring context
> file. What is the correct syntax for reading it?
>
> My configuration:
>
> a) In web.xml:
>
> <context-param>
> <param-name>contextConfigLocation</param-name>
>
> <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
> </context-param>
> <listener>
>
> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
> </listener>
>
> b) Context files:
>
> WEB-INF/applicationContext-ws.xml (for the web service)
> WEB-INF/classes/applicationContext-app.xml (for other beans)
>
> c) Loading beans from applicationContext-app.xml (works OK):
>
> ApplicationContext ctx = new ClassPathXmlApplicationContext(
> "applicationContext-app.xml" );
> myBean = (MyBeanClass)ctx.getBean("myBean");
>
> d) Loading beans from applicationContext-ws.xml does not work. I tried:
>
> - similar code to that in (c)
> - autowiring
> - using the ServletContextAware interface (crazy idea of course - CXF is
> not Spring MVC)
>
> Any help will be greatly welcome... I know this must be terribly basic and I
> am feeling really stupid.
--
Ian Roberts | Department of Computer Science
[email protected] | University of Sheffield, UK