Spring中的奇怪NoSuchBeanDefinitionException

时间:2016-03-23 11:19:05

标签: java spring

我得到一个经典的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

&#34; MyClass的&#34;在上面的堆栈跟踪中当然是我班级的FQN,在这种情况下我替换了(我无法显示我的包裹)。它是一个简单的Netty Handler:

@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包中的其他服务正在自动装配就好了!有没有人有想法?

编辑:经过一些进一步的测试,看来&#34;访问&#34;方法就是问题所在。如果我重命名它,应用程序启动正常。这可能是Spring的错误吗?

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

3 个答案:

答案 0 :(得分:1)

您的类MyClass没有包名?

"No qualifying bean of type [MyClass]" 
在配置中

将组件扫描设置为特定包

@ComponentScan("root.package")

所以Spring找不到你的豆子。

修复:删除(&#34; root.package&#34;)或添加包&#34; root.package&#34;到你的班级

答案 1 :(得分:0)

关于Spring注释的事情是类必须与转轮或上下文位于同一个包树中。

例如,如果您的Spring应用程序类位于com.dippity.dot.myapp中,则所有其他注释为@Service的类,@Component等也必须位于com.dippity.dot.myapp包树下。< / p>

@Service带注释的包可以生效com.dippity.dot.myapp.service并且可以正常工作。

答案 2 :(得分:0)

这个答案让我顺道而行:Spring instantiates bean but can't use it

问题是我也使用了Spring方面,它们配置了

@EnableAspectJAutoProxy

但是,默认情况下,这会启用JDK代理,这些代理的工作方式不同(有关详细信息,请参阅该问题的答案)。

@EnableAspectJAutoProxy(proxyTargetClass=true)

启用CGLIB代理,这是解决我的问题的方法。