当前位置: 首页 > 面试题库 >

applicationContext找不到Servlet上下文的控制器

华懿轩
2023-03-14
问题内容

我有一个带有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>

编辑:我也在调度程序servlet.xml中使用mvc:annotation驱动,它应该用来拾取Controllers(我认为呢?)。

编辑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>

dispatcher-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类位于从Web项目引用的其他项目(JAR)中。我正在使用基于Java的配置,并从applicationContext.xml引用它:

<bean class="bar.foo.config.ServicesConfig" />

因此,这意味着我的Web项目(位于applicationContext.xml所在的位置)中仅存在Controller批注。回顾一下,从我的applicationContext.xml中删除context:component-scan应该不会有任何影响,因为除了@Controller以外,没有其他注释(FIX到EDIT:有一些@Autowired注释)。但是,当我从applicationContext.xml中删除context: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 //
}

解:

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

applicationContext.xml:

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

dispatcher-servlet.xml:

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

我已在Web上下文中设置了Controller,Autowired以及Controller程序包中的所有其他注释-我不确定这是否是最佳实践。


问题答案:

没错-有两个不同的应用程序上下文,由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>

在Web应用程序上下文中,只有与控制器相关的一个:

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


 类似资料:
  • 我有一个带有applicationcontext.xml和dispatcher-servlet.xml配置的Spring web应用程序。我已经在applicationcontext.xml中定义了,但是当我运行应用程序时,没有找到控制器,除非我还将添加到dispatcher-servlet.xml。我在两个软件包中使用了相同的基包,所以这不是问题所在。 我感到困惑,因为我以为applicatio

  • 问题内容: 我有以下代码: 我正在尝试在命令行“ $ ant build”上使用ant对其进行编译,但我不断收到以下错误: 有什么建议吗?谢谢! 问题答案: 简短答案:添加此

  • 对于一个项目,我试图实现JSCSSMergeServlet servlet。 在完全按照说明并通过Maven启动Jetty之后 我得到一个错误的说法: 是否有一些其他文件我需要修改,以便使这个启动和运行?

  • 我使用SpringBoot 1.5.9并创建了: 我的简单控制器正在处理“/”并转发到index.html: 根据SpringBoot文档,应该呈现下的静态内容。如果创建文件夹并将我的放在那里,并在pom.xml中包含spring-boot-starter-thymeleaf依赖项,那么一切都很好。但是,我很困惑为什么src/main/resources/static/index.html的基本呈

  • 问题内容: 可能有人给我解释一下什么是获得这种方式的区别的? 性能或上下文本身是否有差异?如果是这样,那是最好的方法?还有其他检索上下文的方法吗? 问题答案: 还有一个。 从技术上讲,性能没有区别,只有会隐式创建HTTP会话对象(如果尚未创建)。因此,如果尚未完成,那么如果尚未创建会话,则通过会话获取servlet上下文可能要花费几纳秒的时间。 返回的上下文也没有区别。这些方法只是为了方便起见,获

  • 我试图找出一种最简单的方法来控制基本Spring Boot RESTful服务的404 Not Found处理程序,例如Spring提供的示例: https://spring.io/guides/gs/rest-service/ 而不是让它返回默认的Json输出: 或者有比抛出并处理NoHandlerFoundException更好的方法吗?