【问题标题】:Spring Filter components in auto scanning自动扫描中的 Spring Filter 组件
【发布时间】:2021-01-22 05:48:38
【问题描述】:

我有一个 Spring MVC 应用程序。 (Java 平台的应用程序框架和控制容器反转。该框架的核心功能可以被任何 Java 应用程序使用,但是有一些扩展可以在 Java EE(企业版)平台之上构建 Web 应用程序)使用这个 Spring 过滤, 现在使用弹簧过滤器

 <context:component-scan base-package="com.pastis"  >
    <context:exclude-filter type="assignable" expression="com.pastis.security.controller.SecurityManagerController"/>
    <context:exclude-filter type="assignable" expression="com.pastis.pq.workflow.web.controller.WorkflowController"/>
    <context:exclude-filter type="regex"      expression="com.pastis.pq.workflow.web.*" />
    <context:exclude-filter type="regex"      expression="com.pastis.security.*" />
    <context:exclude-filter type="regex"      expression="com\.pastis\.security\..*" />
</context:component-scan>

<jpa:repositories base-package="com.pastis.repositories"/>

也试过了:

<context:annotation-config />
    <context:component-scan base-package="com.pastis" />
    <context:component-scan base-package="com.pastis.security">
        <context:exclude-filter type="assignable" expression="com.pastis.security.controller.SecurityManagerController"/>
    </context:component-scan>
    <context:component-scan base-package=" com.pastis.pq">
        <context:exclude-filter type="assignable" expression="com.pastis.pq.workflow.web.controller.WorkflowController"/>
        <context:exclude-filter type="assignable" expression="com.pastis.pq.workflow.web.controller.WorkflowAdminController"/>
    </context:component-scan>

但我对WorkflowController 有疑问

还有这个控制器:

com.pastis.security.controller.SecurityManagerController

不过,当我启动应用程序时。我收到了这个错误:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityManagerController':

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:jpa="http://www.springframework.org/schema/data/jpa"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/data/jpa
   http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- Use annotations to inject stuff -->
    <context:annotation-config />
    <context:component-scan base-package="com.pastis.pq" use-default-filters="false" >
        <context:include-filter type="aspectj" expression="com.pastis.pq.web.endpoint.*" />
        <!--context:include-filter type="annotation" expression="com.pastis.pq.web."-/-->
    </context:component-scan>

    <jpa:repositories base-package="com.pastis.pq.repositories"/>

    <!-- main datasource -->
    <bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.h2.Driver" />
        <property name="url" value="jdbc:h2:mem:~/test2;DB_CLOSE_DELAY=-1 />
        <property name="username" value="sa" />
        <property name="password" value="" />
    </bean>

    <!-- transaction management -->
    <tx:annotation-driven/>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <!-- spring data repositories -->
    <jpa:repositories base-package="com.pastis.pq.repositories"/>

    <bean id="jpaVendorAdapter"
              class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">

        <property name="database" value="H2" />
        <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
    </bean>

    <bean id="entityManagerFactory"
              class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="pastis-entities" />
        <property name="packagesToScan">
            <array>
              <value>com.pastis.pq.model</value>
            </array>
        </property>
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <property name="jpaProperties">
            <props>
                <prop key="eclipselink.target-database">org.eclipse.persistence.platform.database.OraclePlatform</prop>
                <prop key="eclipselink.target-server">WebLogic</prop>
            </props>
        </property>
    </bean>

    <!-- customizable database configuration -->
    <bean id="dataConfig" class="com.pastis.commons.services.SystemConfig">
        <constructor-arg index="0" value="test-config.properties"/>
        <constructor-arg index="1" ref="dataSource"/>
    </bean>

 
       
</beans>

我运行测试时的控制台:

2020-10-08 10:46:57,315 [DEBUG] org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'securityManagerController'
2020-10-08 10:46:57,315 [DEBUG] org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'securityManagerController'
2020-10-08 10:46:57,315 [DEBUG] org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'securityManagerController'
2020-10-08 10:46:57,315 [DEBUG] org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'securityManagerController'
2020-10-08 10:46:57,317 [DEBUG] org.springframework.beans.factory.annotation.InjectionMetadata - Registered injected element on class [com.pastis.security.controller.SecurityManagerController]: 

【问题讨论】:

  • 首先,您的排除过滤器已过时,请查看包路径。其次,您需要提供更多信息。完整的堆栈跟踪或至少完整的错误消息。它可能会告诉您缺少哪个依赖项。您还必须显示您的 WebApplicationContext 配置。

标签: java spring spring-mvc inversion-of-control component-scan


【解决方案1】:

您的初始代码 sn-p 是正确的,但您没有为组件扫描定义正确的包。

如果您要排除的类是这样的:

com.pastis.security.controller.SecurityManagerController

而不是这个 XML 片段:

<context:annotation-config />
<context:component-scan base-package="com.pastis.pq"  >
    <context:exclude-filter type="assignable" expression="com.pastis.security.controller.SecurityManagerController"/>
</context:component-scan>

<jpa:repositories base-package="com.pastis.pq.repositories"/>

你需要这样的东西:

<context:annotation-config />
<context:component-scan base-package="com.pastis">
    <context:exclude-filter type="assignable" expression="com.pastis.security.controller.SecurityManagerController"/>
</context:component-scan>

<jpa:repositories base-package="com.pastis.pq.repositories"/>

请注意包名称从com.pastis.pq 更改为com.pastis

或者更好的是,为此目的定义另一个 component-scan 元素:

<context:annotation-config />
<context:component-scan base-package="com.pastis.pq" />
<context:component-scan base-package="com.pastis.security">
    <context:exclude-filter type="assignable" expression="com.pastis.security.controller.SecurityManagerController"/>
</context:component-scan>

<jpa:repositories base-package="com.pastis.pq.repositories"/>

请不要忘记从&lt;context:component-scan&gt;元素中删除属性use-default-filters="false"。如documentation所示:

您还可以通过在注释上设置useDefaultFilters=false 或提供use-default-filters="false" 作为&lt;component-scan/&gt; 元素的属性来禁用默认过滤器。这实际上将禁用对带有 @Component@Repository@Service@Controller@Configuration 注释的类的自动检测。

这很可能是在您尝试的其中一种方法中 Spring 无法识别您的 com.pastis.pq.web.endpoint.* 包中的 bean 的原因。

只要你使用&lt;context:component-scan&gt;,也许你可以摆脱&lt;context:annotation-config&gt;元素。请在 stackoverflow 中查看this great answer

更新

经过问题的不同变化,我认为这个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:jpa="http://www.springframework.org/schema/data/jpa"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/data/jpa
   http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:component-scan base-package="com.pastis.pq">
        <context:exclude-filter type="assignable" expression="com.pastis.pq.workflow.web.controller.WorkflowController"/>
        <context:exclude-filter type="assignable" expression="com.pastis.pq.workflow.web.controller.WorkflowAdminController"/>
    </context:component-scan>

    <context:component-scan base-package="com.pastis.security">
        <context:exclude-filter type="assignable" expression="com.pastis.security.controller.SecurityManagerController"/>
    </context:component-scan>

    <jpa:repositories base-package="com.pastis.pq.repositories"/>

    <!-- main datasource -->
    <bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.h2.Driver" />
        <property name="url" value="jdbc:h2:mem:~/test2;DB_CLOSE_DELAY=-1 />
        <property name="username" value="sa" />
        <property name="password" value="" />
    </bean>

    <!-- transaction management -->
    <tx:annotation-driven/>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <!-- spring data repositories -->
    <jpa:repositories base-package="com.pastis.pq.repositories"/>

    <bean id="jpaVendorAdapter"
              class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">

        <property name="database" value="H2" />
        <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
    </bean>

    <bean id="entityManagerFactory"
              class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="pastis-entities" />
        <property name="packagesToScan">
            <array>
              <value>com.pastis.pq.model</value>
            </array>
        </property>
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <property name="jpaProperties">
            <props>
                <prop key="eclipselink.target-database">org.eclipse.persistence.platform.database.OraclePlatform</prop>
                <prop key="eclipselink.target-server">WebLogic</prop>
            </props>
        </property>
    </bean>

    <!-- customizable database configuration -->
    <bean id="dataConfig" class="com.pastis.commons.services.SystemConfig">
        <constructor-arg index="0" value="test-config.properties"/>
        <constructor-arg index="1" ref="dataSource"/>
    </bean>

</beans>

【讨论】:

  • 我更新了问题,但还是有同样的问题
  • 请问,你能更新你的servlet-xml 配置吗?如果您的第一个 sn-p 是正确的,您需要使用类似 &lt;context:exclude-filter type="regex" expression="com\.pastis\.security\..*" /&gt; 的正则表达式修改排除过滤器。您需要正确转义正则表达式特殊字符。请问,你可以试试吗?
  • 我也试过这个...我不知道我做错了什么
  • 很遗憾听到这个消息。我再次更新了我的答案。拜托,你能试试这个确切的 XML 配置,看看它是否有效吗?您是否使用了任何其他配置?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-13
  • 1970-01-01
  • 1970-01-01
  • 2013-12-03
  • 2019-02-16
  • 1970-01-01
相关资源
最近更新 更多