【问题标题】:Exclude subpackages from Spring autowiring?从 Spring 自动装配中排除子包?
【发布时间】:2012-05-23 17:46:05
【问题描述】:

有没有一种简单的方法可以在 Spring 3.1 中从自动装配中排除包/子包?

例如,如果我想包含一个基本包为com.example 的组件扫描,有没有一种简单的方法可以排除com.example.ignore?

(为什么?我想从我的集成测试中排除一些组件)

【问题讨论】:

    标签: spring autowired


    【解决方案1】:

    对于 Spring 4,我使用以下
    (我发布它是因为问题是 4 岁,使用 Spring 4 的人多于 Spring 3.1):

    @Configuration
    @ComponentScan(basePackages = "com.example", 
      excludeFilters = @Filter(type=FilterType.REGEX,pattern="com\\.example\\.ignore\\..*")) 
    public class RootConfig {
        // ...
    }
    

    【讨论】:

      【解决方案2】:

      我不确定您是否可以使用 显式排除软件包,但我敢打赌,使用正则表达式过滤器会有效地帮助您:

       <context:component-scan base-package="com.example">
          <context:exclude-filter type="regex" expression="com\.example\.ignore\..*"/>
       </context:component-scan>
      

      要使其基于注释,您可以使用 @com.example.annotation.ExcludedFromITests 之类的内容来注释您希望排除在集成测试中的每个类。然后组件扫描将如下所示:

       <context:component-scan base-package="com.example">
          <context:exclude-filter type="annotation" expression="com.example.annotation.ExcludedFromITests"/>
       </context:component-scan>
      

      这更清楚了,因为现在您已经在源代码本身中记录了该类不打算包含在集成测试的应用程序上下文中。

      【讨论】:

      • 使用排除过滤器有效,但我不必转义“。”
      • @Amin 这可能是因为“。”占任何字符,包括文字“。”
      【解决方案3】:

      我在相同的用例中使用@ComponentScan,如下所示。这与BenSchro10's XML 答案相同,但它使用注释。两者都使用带有type=AspectJ的过滤器

      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
      import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration;
      import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration;
      import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
      import org.springframework.context.annotation.ComponentScan;
      import org.springframework.context.annotation.FilterType;
      import org.springframework.context.annotation.ImportResource;
      
      @SpringBootApplication
      @EnableAutoConfiguration
      @ComponentScan(basePackages = { "com.example" },
          excludeFilters = @ComponentScan.Filter(type = FilterType.ASPECTJ, pattern = "com.example.ignore.*"))
      public class Application {
          public static void main(String[] args) {
              SpringApplication.run(Application.class, args);
          }
      }
      

      【讨论】:

      • 也许纯属怀旧,但其实我还是更喜欢 XML
      • 可能是我,但在 Spring 4.2 中我必须使用 FilterType.REGEX 来实现这种方法
      • 这是我想要的,但我也必须使用FilterType.REGEX。也许可以更新答案以反映这一点?
      • 欢迎提出新的答案
      • 只是对子包的说明。如果要排除子包"com.example.ignore" 应该是pattern = "com.example.ignore..*"(*前多了一个点)
      【解决方案4】:

      您似乎已通过 XML 完成此操作,但如果您使用新的 Spring 最佳实践,您的配置将使用 Java,您可以将它们排除在外:

      @Configuration
      @EnableWebMvc
      @ComponentScan(basePackages = "net.example.tool",
        excludeFilters = {@ComponentScan.Filter(
          type = FilterType.ASSIGNABLE_TYPE,
          value = {JPAConfiguration.class, SecurityConfig.class})
        })
      

      【讨论】:

      • @JonathanW 但是,对于通过谷歌搜索访问此页面的人来说,这是一个非常有用的答案
      【解决方案5】:

      我认为你应该在更方便的层次结构中重构你的包,所以它们不在基础包中。

      但如果你做不到,试试:

      <context:component-scan base-package="com.example">
          ...
          <context:exclude-filter type="regex" expression="com\.example\.ignore.*"/>
      </context:component-scan>
      

      您可以在此处找到更多示例:Using filters to customize scanning

      【讨论】:

      • 谢谢,原则上我同意重构的想法——这是我的第一个想法。不幸的是,对于我的特殊情况,这并不是一个很好的选择。
      • 好的包装设计可以避免这种情况,但是如果你不能……你就不能哈哈哈,:)
      【解决方案6】:

      这适用于 Spring 3.0.5。所以,我认为它会在 3.1 中工作

      <context:component-scan base-package="com.example">  
          <context:exclude-filter type="aspectj" expression="com.example.dontscanme.*" />  
      </context:component-scan> 
      

      【讨论】:

      • 请注意,如果类路径中不存在 aspectj,它会默默地忽略排除。
      【解决方案7】:

      似乎对我有用的一件事是:

      @ComponentScan(basePackageClasses = {SomeTypeInYourPackage.class}, resourcePattern = "*.class")
      

      或者在 XML 中:

      <context:component-scan base-package="com.example" resource-pattern="*.class"/>
      

      这会覆盖默认的resourcePattern,即"**/*.class"。

      这似乎是只包含您的基础包的最安全的方式,因为该 resourcePattern 将始终与您的基础包相同且相对于您的基础包。

      【讨论】:

        【解决方案8】:

        您也可以使用@SpringBootApplication,根据 Spring 文档,它与以下三个注解具有相同的功能: @Configuration、@EnableAutoConfiguration @ComponentScan 在一个注释中。

        @SpringBootApplication(exclude= {Foo.class})
        public class MySpringConfiguration {}
        

        【讨论】:

        • 根据 Javadoc,exclude 是为了排除特定的自动配置类,而不是组件扫描中的所有类。如果您尝试,您将收到错误消息“无法排除以下类,因为它们不是自动配置类”。
        【解决方案9】:

        您还可以包含特定的包并排除它们,例如:

        包含和排除(两者)

         @SpringBootApplication
                (
                        scanBasePackages = {
                                "com.package1",
                                "com.package2"
                        },
                        exclude = {org.springframework.boot.sample.class}
                )
        

        只排除

        @SpringBootApplication(exclude= {com.package1.class})
        public class MySpringConfiguration {}
        

        【讨论】:

        • 根据 Javadoc,exclude 是为了排除特定的自动配置类,而不是组件扫描中的所有类。如果您尝试,您将收到错误消息“无法排除以下类,因为它们不是自动配置类”。
        【解决方案10】:

        只是对现有答案的补充。
        如果您想从子包中排除类而不是从基础包中排除类,则可以将"com.example.ignore.* 更改为"com.example.ignore.*..*",如下所示

        在 spring-boot 中验证了这一点:2.4.1

        取自this答案的代码sn-p

        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
        import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
        import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration;
        import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration;
        import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
        import org.springframework.context.annotation.ComponentScan;
        import org.springframework.context.annotation.FilterType;
        import org.springframework.context.annotation.ImportResource;
        
        @SpringBootApplication
        @EnableAutoConfiguration
        @ComponentScan(basePackages = { "com.example" },
            excludeFilters = @ComponentScan.Filter(type = FilterType.ASPECTJ, pattern = "com.example.ignore.*..*"))
        public class Application {
            public static void main(String[] args) {
                SpringApplication.run(Application.class, args);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2016-12-17
          • 1970-01-01
          • 1970-01-01
          • 2019-05-03
          • 2014-06-22
          • 1970-01-01
          • 1970-01-01
          • 2011-01-23
          • 1970-01-01
          相关资源
          最近更新 更多