cxf-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Daniel Kulp <dk...@apache.org>
Subject Re: Reading bean from cxf/Spring context file
Date Wed, 15 Jul 2009 18:26:18 GMT
As Ian suggested, having spring inject your bean in as a property or similar 
is normally the way to go.

The other option is to make your impl bean or similar implement the Spring 
ApplicationContextAware interface.    Then Spring will pass the 
ApplicationContext into your bean where you can query anything you want.

Dan


On Tue July 14 2009 5:17:12 pm Bruno Melloni wrote:
> No, sorry.  I guess I did not communicate it clearly.
>
> <jaxws:endpoint implementor="#webServiceImpl" ... /> works great.
>
> The problem happens when I add a vanilla bean to the context:
>
> <bean id="myBean".... />
>
> I try to read the bean fail miserably.
>
> - I already tried adding "private MyBeanClass myBean" and the corresponding
> getter/setter in my code.  No luck. - I also tried to add @Autowire to that
> attribute.  Still no luck.
>
> Clearly what I'm missing is 'how to grab' a bean from that context that was
> already loaded.  My classes don't seem to be aware of the CXF context - not
> even my service Impl class.
>
> I agree that reloading the context like I'm doing is insane (even though it
> is the only thing I could do to make it work - so I was punting).  What
> code would *you* use to get a reference to myBean?
>
> thanks
>
> -----Original Message-----
> From: Ian Roberts [mailto:i.roberts@dcs.shef.ac.uk]
> Sent: Tuesday, July 14, 2009 2:19 PM
> To: users@cxf.apache.org
> Subject: Re: Reading bean from cxf/Spring context file
>
> 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

-- 
Daniel Kulp
dkulp@apache.org
http://www.dankulp.com/blog

Mime
View raw message