Ank*_*arg 5 java spring spring-boot
I have bunch of modules (say 3). Two are Spring boot based module and another one is Spring based. Say Module 1 - SpringBoot Module 2 - Spring Boot Module 3 - Common Module only Spring based
Module 3 @Configuration file defined which needs to be picked only by Module 2 and not 1.
I tried bunch of things to exclude the configuration file. For ex:-
@SpringBootApplication
@ComponentScan(basePackages = {"com.adobe"}
, excludeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {WorkerConfig.class, WorkerExecutors.class, Worker.class})})
public class Application {
private static final Logger logger = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
But Still the @Configiration Class is not getting excluded and Spring is trying to load it in application context which I dont want. My config class
@Configuration
public class WorkerConfig {
@Bean
public WorkerExecutors workerExec() {
WorkerExecutors executors = new WorkerExecutors();
return executors;
}
}
Run Code Online (Sandbox Code Playgroud)
Also I do read in @ComponentScan annotation that
* <p>Note that the {@code <context:component-scan>} element has an
* {@code annotation-config} attribute; however, this annotation does not. This is because
* in almost all cases when using {@code @ComponentScan}, default annotation config
* processing (e.g. processing {@code @Autowired} and friends) is assumed. Furthermore,
* when using {@link AnnotationConfigApplicationContext}, annotation config processors are
* always registered, meaning that any attempt to disable them at the
* {@code @ComponentScan} level would be ignored.
Run Code Online (Sandbox Code Playgroud)
So looks like excluding in Component Scan wont work. Other than above, I also tried excluding using
@SpringBootApplication(exclude= {WorkerExecutors.class, Worker.class,WorkerConfig.class})
public class Application {
Run Code Online (Sandbox Code Playgroud)
But spring boot throws
java.lang.IllegalStateException: The following classes could not be excluded because they are not auto-configuration classes:
- com.adobe.repository.worker.lib.config.WorkerConfig
- com.adobe.acp.repository.worker.lib.core.WorkerExecutors
- com.adobe.acp.repository.worker.lib.core.Worker
Run Code Online (Sandbox Code Playgroud)
Any idea how can I disable component scanning a non spring boot module in a spring boot module other than putting in a different package. I dont want to put in different package.
Any help is appreciated!!
pre*_*am 9
您可以尝试此方法对我有用:
@SpringBootApplication
@ComponentScan(excludeFilters = {@ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE, classes = {WorkerConfig.class, WorkerExecutors.class, Worker.class})})
Run Code Online (Sandbox Code Playgroud)
自动配置包位于 org.springframework.boot.autoconfigure 下。这就是你不能这样做的原因:
@SpringBootApplication(exclude= {WorkerExecutors.class, Worker.class,WorkerConfig.class})
Run Code Online (Sandbox Code Playgroud)
Spring 会按照你的指示去做。您正在呼叫:
@ComponentScan(basePackages = {"com.adobe"}
, excludeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {WorkerConfig.class, WorkerExecutors.class, Worker.class})})
Run Code Online (Sandbox Code Playgroud)
因此 Spring 不会加载任何这些 Worker 类。这就是为什么 Spring 不会“执行”用 @Configuration 注解的类。
也就是说,你想做的事情对我来说没有意义。听起来你有“模块”(java类),但它们都是同一个 spring 上下文的一部分。如果您有一个 Spring Context,那么您可以告诉 Spring 加载一些 @Configuration 类而不是其他类。然后,从您的“模块”中,您可以注入您需要的任何内容。模块 1 将从模块 3 注入 Bean,但模块 2 不会。就那么简单。
如果出于某种原因,您确实需要阻止模块 2 访问模块 3 中的 bean,但仍然保持模块 3 从模块 1 中可见,那么我会将模块 1 和模块 2 分开在两个不同的 Spring Boot 应用程序中,而模块 3 则成为公共代码。但这种方法可能会破坏您当前的架构。
2019 年 3 月 29 日星期五更新
尝试这个:
@SpringBootApplication
@ComponentScan(basePackages = { "com.myapp" }, excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = { MyClass2.class }) })
Run Code Online (Sandbox Code Playgroud)
这对我有用。我有 MyClass 和 MyClass2,MyClass 已加载,而 MyClass2 未加载。我尝试使用 Spring Boot 1.5.9.RELEASE 和 Spring Bom 来获取所有依赖项。
| 归档时间: |
|
| 查看次数: |
7318 次 |
| 最近记录: |