gpt4 book ai didi

java - Spring中奇怪的NoSuchBeanDefinitionException

转载 作者:行者123 更新时间:2023-11-30 07:30:46 25 4
gpt4 key购买 nike

我得到一个经典的 org.springframework.beans.factory.BeanCreationException:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Server': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private MyClass Server.myClass; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [MyClass] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
at root.package.Application.main(Application.java:14)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private MyClass Server.myClass; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [MyClass] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 12 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [MyClass] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
... 14 more

上面堆栈跟踪中的“MyClass”当然是我的类的 FQN,在本例中我替换了它(我无法显示我的包)。这是一个简单的 Netty 处理程序:

@Service
public class MyClass extends SimpleChannelInboundHandler<Protocol> implements Visitor {
@Autowired
private RestTemplate restTemplate;

protected void channelRead0(ChannelHandlerContext channelHandlerContext, Protocol protocol) throws Exception {
channelHandlerContext.writeAndFlush(protocol.accept(this, channelHandlerContext));
}

public Object visit(Protocol protocol, ChannelHandlerContext channelHandlerContext) {
return restTemplate.exchange(...);
}
}

依赖于MyClass的类:

@Component
public class Server {
@Autowired
private MyClass myClass;

private ChannelFuture serverChannelFuture;

@PostConstruct
public void start() throws Exception {
serverChannelFuture = bootstrap().bind(tcpPort()).sync();
}

@PreDestroy
public void stop() throws Exception {
serverChannelFuture.channel().close();
serverChannelFuture.channel().closeFuture().sync();
}

private ServerBootstrap bootstrap() {
// Bootstrap setup
}
}

我的配置:

@Configuration
@ComponentScan("root.package")
public class SpringConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}

@Bean
public Logger logger() {
return Logger.getLogger(InternalLoggerFactory.class.toString());
}
}

我的所有类都位于 root.package 或其子包中。 MyClass 也在子包中。

我不知道为什么这会导致上述异常。我的 root.package 包中有其他正在 Autowiring 的服务!有人有想法吗?

编辑:经过一些进一步的测试,“访问”方法似乎是问题所在。如果我重命名它,应用程序可以正常启动。这可能是 Spring 中的一个错误吗?

编辑2:我庆祝得太早了。我在访问者界面中重命名了该方法,现在又回到了同样的问题。即使我注释掉我的接口(interface),我也不能拥有与该接口(interface)同名的任何方法。 MyClass 是唯一实现作为 Spring bean 的 Visitor 的类。

最佳答案

你的类MyClass没有包名?

"No qualifying bean of type [MyClass]" 

在您的配置中将组件设置为特定的包

@ComponentScan("root.package")

所以Spring无法找到你的bean。

修复方法:删除(“root.package”)或将包“root.package”添加到您的类中

关于java - Spring中奇怪的NoSuchBeanDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36176737/

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