applicationContext没有找到Servlet上下文的控制器

acv*_*vcu 22 spring spring-mvc spring-annotations

我有一个带有applicationContext.xml和dispatcher-servlet.xml配置的Spring Web应用程序.我<context:component-scan />在applicationContext.xml中定义了,但是当我运行我的应用程序时,除非我也添加<context:component-scan />到dispatcher-servlet.xml,否则找不到控制器.我在两者中使用相同的基础包,所以这不是问题.

我很困惑,因为我认为 applicationContext.xml是dispatcher-servlet.xml的父级.不会把<context:component-scan />applicationContext.xml放进去吗?

web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 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">


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

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

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

</web-app>
Run Code Online (Sandbox Code Playgroud)

编辑:我也在dispatcher-servlet.xml中使用mvc:annotation-driven,它应该选择控制器(我想?).

编辑2:这是配置文件.我从applicationContext.xml中删除了一堆Spring Security和OAuth设置(出于安全原因,他们可能无论如何都不相关).

applicationContext.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:sec="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context" xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
      http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
      http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<context:component-scan base-package="bar.foo"/>
<context:property-placeholder location="classpath:my.properties" />
<bean class="bar.foo.ServicesConfig" />

</beans>
Run Code Online (Sandbox Code Playgroud)

调度员servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
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-3.1.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<context:component-scan base-package="bar.foo.controller" />
<mvc:annotation-driven/>
<mvc:default-servlet-handler />

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

<bean id="contentViewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json" />
        </map>
    </property>
    <property name="defaultViews">
        <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
    </property>
    <property name="order" value="1" />
</bean>

</beans>
Run Code Online (Sandbox Code Playgroud)

编辑3:好的,这很有趣.我的服务和dao类位于我从Web项目引用的不同项目(JAR)中.我正在使用基于java的配置并从applicationContext.xml引用它:

<bean class="bar.foo.config.ServicesConfig" />
Run Code Online (Sandbox Code Playgroud)

因此,这意味着我的Web项目中只有Controller注释(applicationContext.xml所在的位置).回想起来,删除上下文:我的applicationContext.xml中的组件扫描应该没有任何影响,因为除了@Controller之外没有注释(FIX到EDIT:有一些@Autowired注释).但是,当我从applicationContext.xml中删除上下文:component-scan时,它表示控制器(从调度程序servlet扫描中找到)无法找到我的Service类.不应该提供ServicesConfig就足够了吗?这是用于引用的ServicesConfig类 - 它有自己的服务组件扫描,它与applicationContext.xml扫描的包不同.

@Configuration
@ComponentScan({ "some.other.package", "another.package" })
@ImportResource({ "classpath:commonBeans.xml" })
@PropertySource({ "classpath:services.properties",
"classpath:misc.properties" })
public class ServicesConfig {
  // Bean definitions //
}
Run Code Online (Sandbox Code Playgroud)

解:

当我从根上下文中删除上下文:组件扫描时,控制器没有获取自动连接的服务bean.这是因为根上下文引用了我的服务基于java的配置Bean,但我没有根上下文设置来扫描组件.因此,当我将组件扫描添加到根上下文(applicationContext.xml)时,一切正常.这就是我现在拥有的:

applicationContext.xml中:

<bean class="bar.foo.config.ServicesConfig" />
<context:component-scan base-package="bar.foo.config" />
Run Code Online (Sandbox Code Playgroud)

调度员servlet.xml中:

<context:component-scan base-package="bar.foo.controller" />
Run Code Online (Sandbox Code Playgroud)

我有Web上下文设置来拾取Controller,Autowired和控制器包中的任何其他注释 - 我不确定这是否是最佳实践.

Bij*_*men 35

你是对的 - 有两个不同的应用程序上下文,ContextLoaderListener加载的根应用程序上下文(在初始化ServletContext时)和Web上下文(由DispatcherServlet加载),根应用程序上下文是Web的父类上下文.

现在,由于这些是两个不同的应用程序上下文,因此它们的行为方式不同 - 如果您component-scan在应用程序上下文中定义服务,则会在此处创建服务的所有bean.

当您的Dispatcher servlet加载时,它将开始创建Web上下文,在某些时候(由<mvc:annotation-driven/>它驱动将为您的uri创建一个映射到处理程序方法,它将获取应用程序上下文中的bean列表(这将是Web应用程序)上下文,而不是Root应用程序上下文),因为你没有在component-scan这里定义控制器相关的bean将无法找到并且不会创建映射,这就是你必须在调度程序servlet上下文中定义组件扫描的原因也.

一个好的做法是在根应用程序上下文中排除与Controller相关的bean:

<context:component-scan base-package="package">
    <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
Run Code Online (Sandbox Code Playgroud)

并且只有与Web应用程序上下文中的控制器相关:

<context:component-scan base-package="package" use-default-filters="false">
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan>
Run Code Online (Sandbox Code Playgroud)