英文:
Disable automatic autowiring of third party Bean
问题
我正在实现一个使用Spring的第三方库,该库以以下方式声明一个过滤器:
@Bean
@Autowired
public Filter filter() {
return new Filter();
}
我使用@Import(Configuration.class)
将配置类加载到我的应用程序中。我的Spring Boot应用程序加载了这个过滤器并似乎尝试使用它,但我不想这样。如何让Spring忽略这些Bean,即不加载它们(假设第三方库可以在没有它们的情况下工作)?
英文:
I am implementing a third party library that uses Spring, which is declaring a Filter in the following way:
@Bean
@Autowired
public Filter filter() {
return new Filter();
}
I load the configuration class to my application with a @Import(Configuration.class)
. My Spring Boot application loads the Filter and seems to try to use it, but I don't want that. How can I make Spring ignore such Beans, i.e., simply don't load them (supposing the third party library can work without them)?
答案1
得分: 2
We followed provider recommendation/decided for:
Appropriate and edit config
Means: Copy, paste and edit the original "Configuration" instead of/before importing/scanning it:
@Configuration
class MyConfig {
// copy, paste & edit original (com.third.party) Configuration (omit unwanted parts/beans)
} // <- use this
One alternative approach is:
Destroy Bean
As described by:
https://stackoverflow.com/q/6855811/592355
..., we only need to "plug it" (e.g.) simple like:
@Bean
BeanFactoryAware myDestroy() {
return (beanFactory) -> {
((BeanDefinitionRegistry) beanFactory).removeBeanDefinition("filter"); // bean name (+ type, see [javadoc](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/support/BeanDefinitionRegistry.html))
};
}
..also possible:
Replace Bean
Here we replace the Bean (by type and name), with a "NO-OP" bean:
@Primary @Bean
public Filter filter() { // same name & type!
return new com.my.example.DoesNothingFilter(); // as name (and interface) implies
}
Ideally
Provider would make it @ConditionalOnXXX
/bind it to @Profile
!
... (+any other alternatives)
英文:
We followed provider recommendation/decided for:
Appropriate and edit config
Means: Copy, paste and edit the original "Configuration" instead of/before importing/scanning it:
@Configuration
class MyConfig {
// copy, paste & edit original (com.third.party) Configuration (omit unwanted parts/beans)
} // <- use this
One alternative approach is:
Destroy Bean
As described by:
https://stackoverflow.com/q/6855811/592355
..., we only need to "plug it" (e.g.) simple like:
@Bean
BeanFactoryAware myDestroy() {
return (beanFactory) -> {
((BeanDefinitionRegistry) beanFactory).removeBeanDefinition("filter"); // bean name (+ type, see [javadoc](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/support/BeanDefinitionRegistry.html))
};
}
..also possible:
Replace Bean
Here we replace the Bean (by type and name), with an "NO-OP" bean:
@Primary @Bean
public Filter filter() { // same name & type!
return new com.my.example.DoesNothingFilter(); // as name (and interface) implies
}
...
Ideally
Provider would make it @ConditionalOnXXX
/bind it to @Profile
!
... (+any other alternatives)
答案2
得分: 1
Spring Boot包括@Profile
注解。
使用特定的@Profile标记“要被忽略”的类,当标识的配置文件未激活时,Spring Boot将忽略它们。
例如:
@Profile("BlammyProfile")
public class Blammy
{
...
}
英文:
Spring Boot includes the @Profile
annotation.
Make the "to be ignored" classes with a specific @Profile and Spring Boot will ignore them when the identified profile is not active.
For example:
@Profile("BlammyProfile")
public class Blammy
{
...
}
Here is a Spring Profile Baeldung Article link
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论