【问题标题】:Spring Annotations in Apache cxfApache cxf 中的 Spring 注解
【发布时间】:2015-04-16 04:27:56
【问题描述】:
我需要将基于 apache cxf 的注释移动到 spring 类中是否有任何简单的方法来支持相同的
例如。移出下面 xml 中提到的 jaxrs:server 和“导入资源”以移至任何 spring 配置类
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<jaxrs:server id="restContainer" address="/">
<jaxrs:serviceBeans>
<!-- This is where we tell which beans CXF should expose as Web-Services -->
</jaxrs:serviceBeans>
</jaxrs:server>
</beans>
【问题讨论】:
标签:
spring
cxf
jax-rs
spring-annotations
【解决方案1】:
要导入资源,您可以简单地使用 @ImportResouce 注释。
你的例子是
@ImportResource(locations = {"classpath:META-INF/cxf/cxf.xml", "classpath:META-INF/cxf/cxf-servlet.xml"})
对于 JAX-RS servlet,您可以这样做:
@Configuration
@ImportResource({"classpath:META-INF/cxf/cxf.xml"})
public class JaxRsProvidedByApacheCXFConfiguration {
@Value("${cxf.path:/*}")
private String cxfPath;
@Bean
public ServletRegistrationBean cxfServletRegistrationBean() {
return new ServletRegistrationBean(new CXFServlet(), cxfPath);
}
@Bean
public Server jaxRsServer() {
return null; // todo create the server with JAXRSServerFactoryBean
}
【解决方案2】:
完成@gmalowski的配置类:
去掉 jaxRsServer() 方法,使类实现 ApplicationContextAware,并添加
@Bean
public SpringJAXRSServerFactoryBean jaxRsServer() {
SpringJAXRSServerFactoryBean bean = new SpringJAXRSServerFactoryBean();
bean.setAddress("/");
bean.setServiceBeans(new ArrayList<Object>(context.getBeansOfType(FooBar.class).values()));
//bean.setProviders(Collections.singletonList(jacksonProvider()));
bean.setApplicationContext(context);
bean.create();
return bean;
}
private ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}