Apache CXF + Spring Java config +替换beans.xml

时间:2015-08-03 06:31:52

标签: tomcat jax-ws spring-java-config

我尝试用JavaConfig(Spring)替换我的beans.xml。在beans.xml中,我有以下配置:

<bean id="testWebService" class="at.test.TestWebService" />
<jaxws:endpoint id="Test" address="/TestWebService_V100"
    implementor="#testWebService" />

<jaxrs:server id="TestRestService" address="/rest/test"
    name="TestRestService">
    <jaxrs:serviceBeans>
        <ref bean="testWebService" />
    </jaxrs:serviceBeans>
    <jaxrs:providers>
        <ref bean="jsonProvider" />
        <ref bean="DateHandler" />
    </jaxrs:providers>
</jaxrs:server>

目前我有一个config.java类,其中包含beans.xml中的所有bean。

config.java:

@Configuration
public class Config {

@Bean
public TestWebService testWebService() {

    return new TestWebService();
}

所有bean都配置了@Controller,所有资源都标有@Autowired。

@WebService(endpointInterface = "at.test.interfaces.ITestWebService")
@Transactional
@Controller
public class TestWebService extends AbstractSessionWebservice implements
ITestWebService {

它工作正常,没有错误,tomcat启动正常。但是我如何配置jaxws:endpoint和jaxrs:server?我在beans文件中有多个条目。那么如何配置多个jaxws:endpoint和jaxrs:服务器条目?

1 个答案:

答案 0 :(得分:0)

请试试这个

@Autowired
ApplicationContext ctx;


@Bean
public ServletRegistrationBean dispatcherServlet() {
    CXFServlet cxfServlet = new CXFServlet();
    return new ServletRegistrationBean(cxfServlet, "/rest/*");
}

@Bean(name="cxf")
public SpringBus springBus() {
    return new SpringBus();
}

@Bean
    public Server jaxRsServer() {
        List providers = new LinkedList();
    providers.add(new JsonProvider());
    providers.add(new DateHandler());

    LinkedList<ResourceProvider> resourceProviders = new LinkedList<>();
    for (String beanName : ctx.getBeanDefinitionNames()) {
        if (ctx.findAnnotationOnBean(beanName, Path.class) != null) {
            SpringResourceFactory factory = new SpringResourceFactory(beanName);
            factory.setApplicationContext(ctx);
            resourceProviders.add(factory);
        }
    }


    JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
    factory.setBus(springBus());
    factory.setProviders(providers);
factory.setResourceProviders(resourceProviders);
    return factory.create();

    }


} 

在TestRestService端点中,

@Endpoint
public class TestRestServiceEndpoint implements TestRestService {

    @PayloadRoot(localPart=TestWebService_V100, namespace=NAMESPACE)
    public <ReturnType> <MethodName>(<RequestType> request) {
    // WS BL
        return;     
    }

}