【问题标题】:Spring MVC 4 targetBeanName NoSuchBeanDefinitionExceptionSpring MVC 4 targetBeanName NoSuchBeanDefinitionException
【发布时间】:2015-07-31 00:17:07
【问题描述】:

我在 web.xml 中定义了一个过滤器

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/root-context.xml
        /WEB-INF/spring/security-context.xml
    </param-value>
</context-param>
<filter>
    <filter-name>WebResourceOptimizer</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
        <param-name>targetBeanName</param-name>
        <param-value>wroFilter</param-value>
    </init-param>
    <init-param>
        <param-name>targetFilterLifecycle</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>WebResourceOptimizer</filter-name>
    <url-pattern>/wro/*</url-pattern>
</filter-mapping>
<filter>
    <display-name>DomainFilter</display-name>
    <filter-name>DomainFilter</filter-name>
    <filter-class>com.cieloh.filter.DomainFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>DomainFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter>
    <display-name>springSecurityFilterChain</display-name>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<resource-ref>
    <description>MySQL Datasource</description>
    <res-ref-name>jdbc/MySQL</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

我在 servlet-context.xml 中定义了 bean

<bean id="wroFilter" class="ro.isdc.wro.http.ConfigurableWroFilter">
    <property name="properties" ref="wroProperties" />
</bean>

<bean id="wroProperties"
    class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="location" value="classpath:wro.properties" />
</bean>

我得到一个 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为“wroFilter”的 bean。我不确定我在这里缺少什么。

【问题讨论】:

  • 异常中有没有引起或其他细节?根本原因可能与您所看到的不同,并且在 spring 初始化错误中很常见

标签: java spring spring-mvc web.xml wro4j


【解决方案1】:

显然您缺少 spring 侦听器和/或 context-param 配置。尝试将以下配置添加到 web.xml:

   <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/servlet-context.xml</param-value>
    <!-- classpath:app-context.xml -->
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

您可以使用带有 wro4j 的 spring 查看以下 example project

【讨论】:

  • 嗨,亚历克斯。谢谢回复。我在 web.xml 中有监听器。我最初没有包含它。我编辑了我的帖子以显示完整的 web.xml。
  • 其实我镜像了你的web.xml,现在一切正常。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2017-09-02
  • 2015-01-14
  • 2016-10-15
  • 2016-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-24
相关资源
最近更新 更多