java - Spring MVC 4 targetBeanName NoSuchBeanDefinitionException

标签 java spring spring-mvc web.xml wro4j

我在 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 监听器和/或 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>

您可以查看以下example project将 spring 与 wro4j 结合使用。

关于java - Spring MVC 4 targetBeanName NoSuchBeanDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31736160/

相关文章:

java - SSLSocket 也适用于不安全的 http?

java - 渲染 3D 第三人称 View

java - 如何使用 Spring Controller 从 URL 读取文件?

java - Spring LDAP 和 Spring Boot 配置

java - 使用 spring-boot gradle-plugin 构建的可执行 jar 缺少依赖项

java - 如何在两个不同的页面中使用单个命令对象而不使其成为单例

java - Spring MVC : How to set body parameter in request browser and how to get this body parameters in controller in Spring MVC?

java - 使用 ForkJoin 和 Streams 构建自适应网格细化

java - 从 Java 程序将数据插入数据库时​​出错

java - Spring xml ...有条件加载变量