【错误解决】Exception in thread “main“ org.springframework.beans.factory.NoSuchBeanDefinitionException

使用spring注解开发时候报错:
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'bookDao' available

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'bookDao' available
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:892)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1318)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1132)
	at com.zcongfly.app.App.main(App.java:11)

在排除:

  1. applicationContext.xml配置文件没有进行context扫描:
  2. 文件扫描路径错误
  3. @Component("className")及其衍生注解类名定义与main入口处中getBean("className")不一致(一般是首字母大小写出错):@Component("BookDao")在main中写成getBean("bookDao")

之后。

查看当前项目使用的spring版本

如果是spring6以上的版本,把版本降为spring5就能正常运行!

JDK17+spring6.0.5按照之前版本注解开发的逻辑写的代码,会直接报No bean named 'bookDao' available的错误!

你可能感兴趣的:(spring,java,spring,boot)