Spring bean not injected into CXF web service, Why?

后端 未结 3 454
一整个雨季
一整个雨季 2021-01-02 09:22

I am writing a RESTful service (using CXF on JBoss) in which I have inject another class using Spring (Autowired). But the class is not getting injected and is null.

3条回答
  •  鱼传尺愫
    2021-01-02 09:48

    If you want to use Spring Beans in CXF Web Service class, then declare WebService as following in the XML configuration file of the CXF (e.g. spring-cxf.xml)

    
    
    

    Declare separated bean for the WebService class and then put it in the endpoint with an ID. Like this you will have spring managed bean, where you can use AutoWired annotations as well.

    Your beans never won't be injected automatically if you will declare your web service as following.

    
    
    
    
    

    In this case you will need either:

    • Inject spring beans manually

      SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

    • Or retrieve the beans one by one from the spring context

      ApplicationContext context = ...; // your Spring ApplicationContext HelloBean helloBean = (HelloBean) context.getBean("bean");

      I haven't tried this for JAX-RS, but the approach in my opinion should be the same.

      From CXF official documentation.

提交回复
热议问题