禁用第三方 Bean 的自动装配

huangapple go评论75阅读模式
英文:

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! 禁用第三方 Bean 的自动装配

... (+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 &amp; edit original (com.third.party) Configuration (omit unwanted parts/beans)
} // &lt;- 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) -&gt; {
    ((BeanDefinitionRegistry) beanFactory).removeBeanDefinition(&quot;filter&quot;); // 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 &amp; type!
    return new com.my.example.DoesNothingFilter(); // as name (and interface) implies
}

...

Ideally

Provider would make it @ConditionalOnXXX/bind it to @Profile! 禁用第三方 Bean 的自动装配

... (+any other alternatives)

答案2

得分: 1

Spring Boot包括@Profile注解。

使用特定的@Profile标记“要被忽略”的类,当标识的配置文件未激活时,Spring Boot将忽略它们。

例如:

@Profile("BlammyProfile")
public class Blammy
{
  ...
}

这是一个Spring配置文件Baeldung文章链接

英文:

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(&quot;BlammyProfile&quot;)
public class Blammy
{
  ...
}

Here is a Spring Profile Baeldung Article link

huangapple
  • 本文由 发表于 2023年6月16日 14:26:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定