如何正确配置多个组件扫描和“使用默认过滤器=false”

e5njpo68  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(289)

我正在开发一个“新应用程序”,它希望利用现有应用程序中的代码,但是我很难让“contextloads()”传入新应用程序。
此配置不起作用:

//This is the main common library B application class:
@Configuration
@ComponentScan(basePackages=["com.acme.cacheb.lib"])
@Import(CacheACommonLibConfig.class)
@EnableConfigurationProperties 
class CacheBCommonLib {

}

//This  is the Config class Imported above:
@Configuration
@ComponentScan(basePackages=["com.acme.cachea.lib"], 
                             useDefaultFilters = false, 
                             includeFilters = [@ComponentScan.Filter(type = FilterType.CUSTOM, 
                             value = RescHandshakeTypeFilter.class)])
class CacheACommonLibConfig {

}

报告的错误是autowire故障:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cacheA_RepoImpl': Unsatisfied dependency expressed through field 'cacheA_Repo'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.acme.cachea.lib.jpa.repository.ICacheA_Repository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

我知道我的自定义过滤器匹配了我想要的所有东西(包括名为icachea\u repository的“缺失”接口),而且没有我不想要的东西。我怀疑这个错误是因为“usedefaultfilters=false”在起作用,以及组件扫描是如何组合的。如果我这么做

@ComponentScan(basePackages=["com.acme.cacheb.lib", "org.springframework"])

在新的主应用程序类中,测试在报告不同错误失败之前运行的时间更长,我还收到了一个相当可怕的“spring警告”


**WARNING**: Your ApplicationContext is unlikely to start due to a @ComponentScan of 'org.springframework'.

非常感谢您的帮助。

b4lqfgs4

b4lqfgs41#

我放弃了应用依赖jar的“include filtering”(使用默认过滤器=false)并在组件扫描中切换到“exclude filtering”,从而在b应用中传递了“context loads”。但在我“真的”让它工作之后,我不确定我真的需要切换到“排除过滤”。我认为这是因为在我的旅程结束时,我开始看到我第一次看到的错误,这就是为什么“thymeleaf视图解析器”现在被排除在自动配置(如果我在错误第一次出现时尝试过,而不是完全禁用自动配置…)
b app主类配置现在如下所示:

@Configuration
@ComponentScan(basePackages=["com.acme.cacheb.lib"])
@Import(CacheACommonLibConfig.class)
@EnableAutoConfiguration(exclude=[WebMvcAutoConfiguration.class, ThymeleafAutoConfiguration.class])
@EnableConfigurationProperties
@EntityScan('com.acme.cacha.lib.jpa.entity')
@EnableJpaRepositories('com.acme.cacha.lib.jpa.repository')
//@EnableScheduling
class CacheBCommonLib {

}

导入的config类现在配置如下:

@Configuration
@ComponentScan(basePackages=["com.acme.cacha.lib.msg.auth",
                         "com.acme.cacha.lib.msg.emp",
                         "com.acme.cacha.lib.msg.processor",
                         "com.acme.cacha.lib.jpa"],
    excludeFilters = [@ComponentScan.Filter(type = FilterType.CUSTOM, value = CacheBExcludeCacheATypesFilter.class),
                      @ComponentScan.Filter(type = FilterType.REGEX,
                       pattern = "com.acme.cacha.lib.msg.processor.*"),
                      @ComponentScan.Filter(type = FilterType.REGEX,
                       pattern = "com.acme.cacha.lib.msg.auth.(sas|radius).*")
                     ])
class CacheACommonLibConfig {

}

相关问题