使用spring boot和JAXRSServerFactoryBean时无法注入spring服务类

时间:2014-09-24 19:16:40

标签: java spring cxf jax-rs spring-boot

我有一个独立的应用程序与Spring Boot一起运行。它在应用程序的根目录(/)注册Soap端点的CXF servlet。我还通过

以编程方式添加了CXF jaxrs
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

    /**
     * register cxf SOAP web services with spring boot
     * @return
     */
    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        return new ServletRegistrationBean(new CXFServlet(), "/*");                 
    }

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        // register cxf SOAP REST services with spring boot
        JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
        factory.setProviders(Arrays.asList(new JacksonJsonProvider()));     
        factory.setResourceClasses(ConfigureSoapResponseServiceImpl.class);
        factory.setAddress("/rest");
        factory.create();
    }
}

到目前为止,这适用于注册我的所有jaxws和jaxrs服务。但是,我无法将服务类注入休息端点。我假设这是因为我使用JAXRSServerFactoryBean以编程方式设置它。

@Consumes("application/json")
@Produces("application/json")
@Path("/config")
public interface ConfigureSoapResponseService {


    @GET
    @Path("/")  
    public List<String> getAllOperations(@QueryParam("wsdlLocation") String wsdlLocation);


}

@Service
public class ConfigureSoapResponseServiceImpl implements ConfigureSoapResponseService {


    @Autowired
    public WsdlInspectionService wsdlInspectionService;


    @Override
    public List<String> getAllOperations(String wsdlLocation) {     
        return wsdlInspectionService.getWsdlOperations(wsdlLocation);
    }

    @PostConstruct
    public void init() {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }
}


@Component("wsdlInspectionService")
public class WsdlInspectionServiceImpl implements WsdlInspectionService {


    public List<String> getWsdlOperations(String wsdlLocation) {
        // ...
    }
}

我尝试在上面的代码中使用@PostConstruct注释并添加

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>

但两者都没有效果。

在我的cxf-servlet.xml中。由于我使用的是Spring Boot,因此我没有applicationContext.xml。我的cxf-servlet.xml的其余部分包括:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:jaxws="http://cxf.apache.org/jaxws" 
xmlns:jaxrs="http://cxf.apache.org/jaxrs"  
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation=" http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd 
http://cxf.apache.org/bindings/soap 
http://cxf.apache.org/schemas/configuration/soap.xsd 
http://cxf.apache.org/jaxws 
http://cxf.apache.org/schemas/jaxws.xsd 
http://cxf.apache.org/jaxrs 
http://cxf.apache.org/schemas/jaxrs.xsd">


<jaxws:endpoint 
    xmlns:tns="https://someService/"
    id="someService"
    address="/someService" 
    serviceName="tns:someService"
    endpointName="tns:someService" 
    implementor="net.livetv.ws.SomeServicePortImpl">
</jaxws:endpoint>

// etc...

为了完整性,这是我的web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="2.5"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <servlet>
        <servlet-name>cxf</servlet-name>
        <display-name>cxf</display-name>
        <description>Apache CXF Endpoint</description>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>


    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>

</web-app>

我的错误是这行代码的NullPointerException:

return wsdlInspectionService.getWsdlOperations(wsdlLocation);

它没有注入我的服务。如何将其设置为正确注入?

0 个答案:

没有答案