gpt4 book ai didi

java - Bean 定义被自动配置覆盖

转载 作者:行者123 更新时间:2023-11-30 10:15:59 27 4
gpt4 key购买 nike

在我的 Spring 应用程序中,我定义了一个 AuditListener 类型的 bean。

@Component
public class AuditListener {
}

我的项目还包含对 spring-boot-starter-actuator 的依赖,它还通过 AuditAutoConfiguration 定义了一个 AuditListener 类型的 bean。

当我尝试启动我的应用程序时它失败了,因为我自己的 AuditListener 不可用。

// successful
beanFactory.getBean(org.springframework.boot.actuate.audit.listener.AuditListener.class);

// fails with NoSuchBeanDefinitionException
beanFactory.getBean(demo.AuditListener.class);

异常(exception):

java.lang.IllegalStateException: Failed to execute ApplicationRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:791)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:778)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:335)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243)
at demo.DemoApplication.main(DemoApplication.java:14)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'demo.AuditListener' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:347)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:334)
at demo.DemoApplication.run(DemoApplication.java:27)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:788)
... 11 more

DefaultListableBeanFactory 日志

INFO 10140 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'auditListener' with a different definition: replacing [Generic bean: class [demo.AuditListener]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [C:\workspace\spring-autoconfiguration-conflict-demo\target\classes\demo\AuditListener.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.actuate.autoconfigure.audit.AuditAutoConfiguration; factoryMethodName=auditListener; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/actuate/autoconfigure/audit/AuditAutoConfiguration.class]]

如何在不重命名自己的情况下将两个 AuditListener bean 放入我的上下文中?

编辑:如果我在不同的包中定义两个具有相同类名的 bean,我会得到一个 ConflictingBeanDefinitionException,因此 ApplicationContext 甚至不会启动。

最佳答案

你可以给你的bean起一个名字,这样它就不会和SpringBoot bean冲突

@Component(value = "myCustomAuditListener ")
public class AuditListener {
}

然后使用 @Qualifies 通过名称 myCustomAuditListener 注入(inject)它

关于java - Bean 定义被自动配置覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50233325/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com