覆盖配置为春天

发布于 2025-02-13 06:59:57 字数 649 浏览 0 评论 0原文

这是我的情况。我有一个父级项目,它具有bean配置,如下所示,

@Configuration
public class Configuration {
@Bean
public BeanA beanA(@Autowired BeanB beanB) {
  return new BeanA(beanB);
}

我想覆盖此配置,因为我需要覆盖BeanB上的某些定义。

@Configuration
public class Configuration {
@Bean
public BeanA beanA(@Autowired BeanC beanC) {
  return new BeanA(beanC);
}

我的C型豆类在哪里

public class BeanC extends BeanB { ... }

,但是当我运行应用程序时,我总是得到来自父母的配置。还启用了bean-depinition-coverriding

spring.main.allow-bean-definition-overriding=true

,任何人都知道如何告诉弹簧容器使用我的bean定义,而不是来自我父母项目的定义。 提前致谢!

Here is my situation. I have a parent project which has a bean configuration as follows

@Configuration
public class Configuration {
@Bean
public BeanA beanA(@Autowired BeanB beanB) {
  return new BeanA(beanB);
}

I want to override this configuration, because I need to override some of the definitions on BeanB.

@Configuration
public class Configuration {
@Bean
public BeanA beanA(@Autowired BeanC beanC) {
  return new BeanA(beanC);
}

Where my bean of type C

public class BeanC extends BeanB { ... }

But when I run the application, I am always getting the configuration coming from the parent. Also have enabled the bean-definition-overriding

spring.main.allow-bean-definition-overriding=true

Does anyone knows how can I tell the spring container to use my bean definition instead the one that is coming from my parent project.
Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最冷一天 2025-02-20 06:59:57

您可以用@primary注释来注释豆(请参阅: https://wwwww.baeldung-com/spring-.com/spring-.com/spring-.com/spring-.com/主要

@Configuration
public class Configuration {

@Primary
@Bean
public BeanA beanA(@Autowired BeanC beanC) {
  return new BeanA(beanC);
}

You can annotate your bean with @Primary annotation (see: https://www.baeldung.com/spring-primary)

@Configuration
public class Configuration {

@Primary
@Bean
public BeanA beanA(@Autowired BeanC beanC) {
  return new BeanA(beanC);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文