我将用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注释?