CXF& Spring - 在jax-rs服务bean

时间:2018-03-27 15:26:46

标签: java spring spring-boot cxf

简介

我已经使用CXF Frameworks和Spring启动了一个新的基本应用程序,并使用一个简单的实验控制器连接在一起。

如果我没有使用注入(在属性上),并且如果我在构造函数上设置注入(没有默认构造函数),则实验控制器可以运行。

问题

Spring和CXF是如何协同工作的,以及我应该如何成功注入(如果可能的话,在构造函数上)?

来源

的beans.xml

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

  <import resource="classpath:META-INF/cxf/cxf.xml"/>

  <jaxrs:server id="services" address="/">
    <jaxrs:serviceBeans>
      <bean class="tp.rest.ExperimentalController"/>
    </jaxrs:serviceBeans>
    <jaxrs:providers>
      <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
    </jaxrs:providers>
  </jaxrs:server>

  <!-- enable CXF JMX -->
  <bean id="org.apache.cxf.management.InstrumentationManager"
        class="org.apache.cxf.management.jmx.InstrumentationManagerImpl">
    <property name="enabled" value="true"/>
    <property name="bus" ref="cxf"/>
    <property name="usePlatformMBeanServer" value="true"/>
    <property name="createMBServerConnectorFactory" value="false"/>
  </bean>
</beans>

的web.xml

<web-app
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
  <display-name>Experimental</display-name>
  <description>Experimental</description>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/beans.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

ExperimentalController

@Path("/expe")
@Produces(MediaType.APPLICATION_XML_VALUE)
public class ExperimentalController {

    @Autowired
    private Test test;

    @GET
    public Test test() {
        return test;
    }
}

测试

@Component
@XmlRootElement
public class Test {
    private String value = "test";

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

如果需要,我还可以提供pom.xml。

0 个答案:

没有答案