代码之家  ›  专栏  ›  技术社区  ›  Joschi

Spring@RestController注释,带有来自bean的根上下文。xml

  •  1
  • Joschi  · 技术社区  · 8 年前

    我将用Spring 4 MVC替换我的Jersey Rest控制器。所以我把所有的控制器都转换成了spring mvc RestController。问题是我无法让控制器工作。这是我的控制器:

    @RestController
    @RequestMapping("/person")
    public class PersonController { 
    
        // .... //
    
        // Spring xml injections
        private PersonService personService;
        public void setPersonService(PersonService personService) {
            this.personService = personService;
        }
    }
    

    bean配置为xml配置-> 人豆。xml:

    <bean id="personController" class="de.some.package.PersonController">
        <property name="personService" ref="personService"/>
    </bean>
    

    完整的应用程序一起构建在一个名为beans的文件中。xml-> 豆。xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
    
        <import resource="classpath:spring/person-beans.xml"/>
        <!-- and some more.. -->
    
    </beans>
    

    这是我的网站。xml:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/spring/beans.xml</param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <servlet>
        <servlet-name>RESTServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>RESTServlet</servlet-name>
        <url-pattern>/ws/*</url-pattern> 
    </servlet-mapping>
    

    我想我需要补充一点:

    <context:component-scan base-package="de.some.package" />
    <mvc:annotation-driven />
    

    为了让@RestController注释起作用,但是注入到我的PersonController类中的是null(这里是personService)。

    有没有办法从bean中使用spring配置。xml以及@RestController注释?

    1 回复  |  直到 8 年前
        1
  •  1
  •   DamCx    8 年前

    正如你所说,如果你加上 <mvc:annotation-driven /> 在你的 beans.xml 文件,您将能够同时拥有注释和Spring xml配置文件