contextLoaderListener配置,找不到控制器

时间:2015-06-03 04:50:56

标签: spring spring-mvc

当我使用DispatcherServlet运行我的示例spring mvc webapplication时,它工作正常,但是当我包含ContextLoaderlListener时,它还尝试从applicationContext.xml中找到控制器类,该控件类实际定义为在dispatcher-servlet.xml中扫描

这是我的web.xml

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>


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


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

这是我的application-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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="com.startcompany.startapp">
        <context:exclude-filter type="annotation" expression="com.startcompany.startapp.controller"/>
    </context:component-scan>
</beans>

这是myapp-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:component-scan base-package="com.startcompany.startapp.controller"/>

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

在tomcat上部署时出现此异常。

SEVERE: Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Type filter class not found: com.startcompany.startapp.controller; nested exception is java.lang.ClassNotFoundException: com.startcompany.startapp.controller
Offending resource: ServletContext resource [/WEB-INF/application-context.xml]; nested exception is java.lang.ClassNotFoundException: com.startcompany.startapp.controller
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70) 

2 个答案:

答案 0 :(得分:0)

我认为这个问题与diff文件中的两个矛盾的陈述有关。在一个地方你试图include controller包用于扫描,而在其他地方你是excluding

应用context.xml中

   <context:exclude-filter type="annotation" expression="com.startcompany.startapp.controller"/>

<强> MyApp的-servlet.xml中

<context:component-scan base-package="com.startcompany.startapp.controller"/>

答案 1 :(得分:0)

在exclude-filter中,类型为“ annotation”的您应该使用注释来标记排除的类

expression="MyExcludingAnnotation"