简体   繁体   English

如何排除某些@Configuration并仅在Spring Boot中使用@Import导入它们

[英]How to exclude some @Configuration and only import them using @Import in Spring boot

I'm developing a spring-boot webapp which has the following package structure.. 我正在开发一个具有以下程序包结构的spring-boot webapp。

- com.myname.configs
    - CommonConfiguration.java  
    - DevConfiguration.java  
    - ProdConfiguration.java
    - SomeOtherConfiguration.java
- com.myname.profiles
    - DevProfile.java
    - ProdProfile.java

All of these classes are @Configuration classes but the DevProfile.java and ProdProfile.java also have the @Profile and @Import annotations. 所有这些类都是@Configuration类,但是DevProfile.java和ProdProfile.java也具有@Profile和@Import批注。

CommonConfiguration.. CommonConfiguration ..

@Configuration  
public class CommonConfiguration {  
    //commmon configs / beans..  
}  

DevConfiguration.. DevConfiguration ..

@Configuration
@Profile("dev")
public class DevConfiguration {

    @Bean
    //dev specific beans..
}

DevProfile.. DevProfile ..

@Configuration  
@Import(value = {CommonConfiguration.class, DevConfiguration.class})  
@Profile("dev")  
public class DevProfile {}  

ProdProfile.. ProdProfile ..

@Configuration
@Import(value = {CommonConfiguration.class, ProdConfiguration.java})
@Profile("prod")
public class ProdProfile {}

For some reason, even with -Dspring.profile.active=prod, the beans for DevConfiguration are created. 由于某些原因,即使使用-Dspring.profile.active = prod,也会为DevConfiguration创建Bean。 The only workaround is to add a @Profile("dev") annotation to DevConfiguration.java 唯一的解决方法是在DevConfiguration.java中添加@Profile(“ dev”)批注

Is there a way to only create the beans for classes in the @Import annotations? 有没有一种方法只能为@Import批注中的类创建Bean? It seems logical to manage the Imports in one Profile class than add the Profile to various Configuration classes. 在一个Profile类中管理导入似乎比将Profile添加到各种Configuration类中更合乎逻辑。

I'm looking for a way to do what @Aaron Digulla suggested in #1 here How to exclude some @Configuration file in Spring? 我正在寻找一种方法来执行@Aaron Digulla在此处#1中建议的操作如何在Spring中排除某些@Configuration文件?

If you use config-classes (CommonConfiguration , DevConfiguration and etc.) only by importing in profile-classes ( DevProfile and etc.) then you just may remove @Configuration from config-classes. 如果仅通过导入配置文件类( DevProfile等)来使用配置类(CommonConfigurationDevConfiguration等),则可以从配置类中删除@Configuration
In this case beans from config-classes will be created only via active profile-class (now config-classes and profile-classes are both scanned by spring for beans but @Profile("...") affect only to profile-classes). 在这种情况下,将仅通过活动概要文件类来创建来自配置类的Bean(现在,弹簧会扫描config类和概要文件类中的bean,但是@Profile("...")仅影响概要文件类) 。

Actually, shortly after posting the comment I found the answer. 实际上,发表评论后不久,我找到了答案。 It is Kirby's answer here Exclude subpackages from Spring autowiring? 这是Kirby的答案。 从Spring自动装配中排除子包?

In my case I've added this after the @SpringBootApplication annotation. 就我而言,我已经在@SpringBootApplication注释之后添加了它。

@SpringBootApplication
@ComponentScan(basePackages = { "com.myname" },
              excludeFilters = @ComponentScan.Filter(type = FilterType.ASPECTJ, pattern = "com.myname.configs.*"))
public class DemoWebappApplication {...}  

With this, the profile classes are being wired along with the @Imports in them. 这样,概要文件类与它们中的@Imports一起被连接。

Did you try @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) annotation in below link (16.2 section) 您是否在下面的链接(16.2节)中尝试了@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})批注?

https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM