Spring MVC double初始化

时间:2017-03-27 09:15:28

标签: java spring spring-mvc

我在Java spring应用程序中遇到了问题,当我调用我的其余控制器时,它会执行2次。

这是我的控制器

@Controller
public class CrashReportController extends AbstractBaseController {

private final Logger logger = LoggerFactory.getLogger(CrashReportController.class);

private CrashReportRequest crashReportRequest;


@Override
protected Logger getLogger() {
    return logger;
}

@RequestMapping(value = "/req/{token}", method = RequestMethod.POST, consumes = "application/json", produces = "application/pdf")
public @ResponseBody ResponseEntity<byte[]> req(@RequestBody CrashReportRequest request, @PathVariable ("token") String token)
        throws IOException {...}

}

这是我的web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>    

<display-name>rmp</display-name>


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>resources/app-config.xml</param-value>
</context-param>

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

<servlet>
    <servlet-name>my-dispatcher-servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>resources/web-config.xml</param-value>
    </init-param>      
</servlet>

<servlet-mapping>
    <servlet-name>my-dispatcher-servlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

应用-config.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans.xsd
                       http://www.springframework.org/schema/context
                       http://www.springframework.org/schema/context/spring-context.xsd ">


<context:component-scan base-package="rmp.rest.controller">
    <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

网络配置

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans.xsd
                       http://www.springframework.org/schema/mvc
                       http://www.springframework.org/schema/mvc/spring-mvc.xsd
                       http://www.springframework.org/schema/context
                       http://www.springframework.org/schema/context/spring-context.xsd ">

<context:component-scan base-package="rmp.rest.controller">
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
</bean>

为什么我有双重启动(我想这是问题)? 我很难解决这个问题,请帮助!

1 个答案:

答案 0 :(得分:0)

您的配置使包&#34; rmp.rest.controller&#34;要扫描两次(因此它上面的所有bean都要初始化两次)。

这是由以下元素引起的。在App-config.xml中:

<context:component-scan base-package="rmp.rest.controller">
    <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

在Web-config.xml中:

<context:component-scan base-package="rmp.rest.controller">
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

从App-config.xml中删除前一个元素似乎是要遵循的路径。