应用上下文未能找到Servlet上下文的控制器。

22
我有一个Spring Web应用程序,其中包含applicationContext.xml和dispatcher-servlet.xml配置。我在applicationContext.xml中定义了<context:component-scan />,但是当我运行我的应用程序时,控制器无法找到,除非我还将<context:component-scan />添加到dispatcher-servlet.xml中。我在两个文件中都使用相同的base-package,因此问题不在于此。
我感到困惑,因为我认为applicationContext.xml是dispatcher-servlet.xml的父级。难道在applicationContext.xml中放置<context:component-scan /> 就不够吗?
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>

编辑:我还在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>

调度器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>

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

EDIT 3: 好的,这很有意思。我的服务和DAO类在一个不同的项目(JAR)中,我从 Web 项目中引用它们。我正在使用基于 Java 的配置,并从 applicationContext.xml 进行引用:

<bean class="bar.foo.config.ServicesConfig" />
所以,这意味着我的Web项目(其中包含applicationContext.xml)中只有Controller注释。回顾一下,从我的applicationContext.xml中删除context:component-scan不应该有任何影响,因为除了@Controller之外,没有任何注释(修复为编辑:还有一些@Autowired注释)。但是,当我从applicationContext.xml中删除context:component-scan时,它会说找不到我的Service类的Controllers(从dispatcher servlet scan中找到)。引用ServicesConfig不应该足够吗?这是ServicesConfig类的参考资料-它有自己的组件扫描用于Services,这些Services与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 //
}

解决方案:

当我从根上下文中删除context:component-scan时,控制器无法获取自动装配的服务bean。这是因为根上下文引用了我的服务基于Java的配置Bean,但我没有设置根上下文来扫描组件。因此,当我将组件扫描添加到根上下文(applicationContext.xml)中时,一切都可以正常工作。这是我现在拥有的内容:

applicationContext.xml:

<bean class="bar.foo.config.ServicesConfig" />
<context:component-scan base-package="bar.foo.config" />

调度器servlet.xml:

<context:component-scan base-package="bar.foo.controller" />

我已经设置了Web上下文来捕获控制器(Controller)、自动装配(Autowired)和控制器包中的任何其他注释 - 我不确定这是否是最佳实践。


也许你应该将你的Spring配置文件添加到问题中,这很可能是一个配置问题。 - Boris Treukhov
请在applicationContext.xml中添加<context:component-scan base-package="bar.foo"/>以扫描bar.foo包中的组件。 - Priyank Doshi
显然,在我的设置中这是必要的,因为如果不包括它,Bean(除控制器外)将无法找到。 - acvcu
3个回答

36
你是正确的,有两个不同的应用程序上下文,一个由ContextLoaderListener加载的根应用程序上下文(在ServletContext初始化时),另一个由DispatcherServlet加载的Web上下文,根应用程序上下文是Web上下文的父级。
现在,由于这些是两个不同的应用程序上下文,它们会以不同的方式进行操作-如果您在应用程序上下文中为服务定义了组件扫描,则所有服务的bean都将在此处创建。
当您的Dispatcher servlet加载时,它将开始创建Web上下文,在某个时刻(通过驱动),它将为您的URI的映射到处理程序方法创建一个映射,它将获取应用程序上下文中的bean列表(将是Web应用程序上下文,而不是根应用程序上下文),并且由于您没有在此处定义component-scan,因此不会找到与控制器相关的bean,并且不会创建映射,这就是为什么您必须在dispatcher servlet的上下文中定义component-scan的原因。
一个好的实践是在根应用程序上下文中排除与Controller相关的beans。
<context:component-scan base-package="package">
    <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>

在Web应用程序上下文中,只涉及控制器的内容:

<context:component-scan base-package="package" use-default-filters="false">
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan>

"use-default-filters" 的目的是什么? - acvcu
1
在Web上下文或根上下文中拥有控制器是最佳实践吗?这重要吗? - acvcu
1
是的,这很重要,它应该在Web上下文中,否则当处理程序映射被创建时,它们将无法被检测到。我已经在Web上下文中将使用默认过滤器设置为关闭,以便只有与控制器相关的bean定义会被加载到Web上下文(通过包含过滤器),而服务等则期望从Root上下文中获取。 - Biju Kunjummen
1
如果我的控制器中有自动装配的服务,我应该让Web上下文扫描这些服务还是根上下文?这方面是否有最佳实践?我的困惑部分源于在控制器中有自动装配的bean。 - acvcu
这帮助我更好地理解不同上下文如何协同工作,并最好地帮助解决问题。谢谢。 - acvcu

1
在我们的应用程序中,我们在dispatcher-servlet.xml文件中定义。
我认为这是应该放置的位置,而不是在applicationContext.xml文件中。
Spring文档的这一部分应该提供更多信息:

http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html

正如您在第16.2节的其中一张图表中所看到的那样,dispatcher-servlet位于上下文层次结构中的applicationContext之上。


如果我从applicationContext.xml中删除context:component-scan(并在dispatcher-servlet.xml中将base-package设置为更高级别的包),那么启动Tomcat时会出现NoSuchBeanDefinitionExceptions。 - acvcu

0

我曾经遇到过同样的问题,通过与this tutorial比较我的web.xml代码,我进行了更改并解决了问题。这是我的web.xml文件:

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xsi:schemaLocation="
    http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     version="3.0">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/business-config.xml</param-value>
</context-param>

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

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

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

context-paramlistener是我错过的。

希望能对你有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接