Java 从 Spring Boot 应用程序中排除 @Configuration
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55403688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exclude @Configuration from Spring Boot Application
提问by Ankur Garg
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
我有一堆模块(比如 3)。两个是基于 Spring Boot 的模块,另一个是基于 Spring 的模块。说模块 1 - SpringBoot 模块 2 - Spring Boot 模块 3 - 通用模块仅基于 Spring
Module 3 @Configuration file defined which needs to be picked only by Module 2 and not 1.
定义的模块 3 @Configuration 文件只需要由模块 2 而不是模块 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);
}
}
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
但是@Configiration 类仍然没有被排除在外,Spring 正试图在我不想要的应用程序上下文中加载它。我的配置类
@Configuration
public class WorkerConfig {
@Bean
public WorkerExecutors workerExec() {
WorkerExecutors executors = new WorkerExecutors();
return executors;
}
}
Also I do read in @ComponentScan annotation that
我也读过@ComponentScan 注释
* <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.
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 {
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
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.
知道如何在 spring 引导模块中禁用组件扫描非 spring 引导模块,而不是放入不同的包。我不想放入不同的包装。
Any help is appreciated!!
任何帮助表示赞赏!
回答by preetham
You can try this it works for me:
你可以试试这个对我有用:
@SpringBootApplication
@ComponentScan(excludeFilters = {@ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE, classes = {WorkerConfig.class, WorkerExecutors.class, Worker.class})})
回答by Perimosh
Auto-config packages lives under org.springframework.boot.autoconfigure. That's the reason why you can't do:
自动配置包位于 org.springframework.boot.autoconfigure 下。这就是你不能这样做的原因:
@SpringBootApplication(exclude= {WorkerExecutors.class, Worker.class,WorkerConfig.class})
Spring does what you tell to do. You are calling:
Spring 会按照您的吩咐去做。您正在致电:
@ComponentScan(basePackages = {"com.adobe"}
, excludeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {WorkerConfig.class, WorkerExecutors.class, Worker.class})})
Therefore Spring won't load any of those Worker classes. That's why Spring doesn't "execute" your classes annotated with @Configuration.
因此 Spring 不会加载任何这些 Worker 类。这就是为什么 Spring 不会“执行”您用 @Configuration 注释的类的原因。
That said, what you are trying to do doesn't make sense to me. Sounds like you have "modules" (java classes), but all of them are part of the same spring context. If you have a single Spring Context, then you can tell Spring to load some @Configuration classes and not some others. Then, from your "modules", you can inject whatever you need. Module 1 will inject Beans from Module 3, but Module 2 won't. Simple as that.
也就是说,你试图做的事情对我来说没有意义。听起来你有“模块”(java 类),但它们都是同一个 spring 上下文的一部分。如果您只有一个 Spring Context,那么您可以告诉 Spring 加载一些 @Configuration 类而不是其他一些类。然后,从你的“模块”,你可以注入任何你需要的东西。模块 1 将从模块 3 注入 Bean,但模块 2 不会。就那么简单。
If for some reason you really need to prevent Module 2 accessing to beans from Module 3, but still keep Module 3 visible from Module 1, then I would separate Module 1 and Module 2 in two different Spring Boot Apps, and Module 3 becomes common code. But this approach may break your current architecture.
如果由于某种原因你真的需要阻止模块 2 从模块 3 访问 bean,但仍然保持模块 3 对模块 1 可见,那么我会将模块 1 和模块 2 分开在两个不同的 Spring Boot 应用程序中,模块 3 成为通用代码. 但是这种方法可能会破坏您当前的架构。
UPDATE ON Fri Mar 29 2019
2019 年 3 月 29 日星期五更新
Try this:
尝试这个:
@SpringBootApplication
@ComponentScan(basePackages = { "com.myapp" }, excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = { MyClass2.class }) })
It worked for me. I have MyClass and MyClass2, and MyClass is loaded and MyClass2 is not. I tried it with Spring Boot 1.5.9.RELEASE and Spring Bom for all the dependencies.
它对我有用。我有 MyClass 和 MyClass2,MyClass 已加载而 MyClass2 未加载。我尝试使用 Spring Boot 1.5.9.RELEASE 和 Spring Bom 来处理所有依赖项。