Spring报错org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'helloSpring'

Spring运行时报错信息:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'helloSpring' is defined

	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:575)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1111)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:276)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1119)
	at com.springdemo.test.TestHelloSpring.testHelloSpring(TestHelloSpring.java:15)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:389)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:167)
	at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:163)

首先我们对错误信息进行分析,找出错误关键提示,上文中关键提示为No bean named 'helloSpring' is defined,该语句翻译为没有定义为 'helloSpring'的元素所以此时我们检查的目标应该为字段对应的调用类以及Spring的配置文件applicationContext.xml

  • 字段对应调用类
    Spring报错org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'helloSpring'_第1张图片从上图发现getBean()中的参数与控制台提示的信息是一致的,所以我们就可以排除是此处的错误啦,那么我们的目标就为applicationContext.xml文件啦!

  • applicationContext.xml配置文件:
    Spring报错org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'helloSpring'_第2张图片从以上内容中可以发现,bean元素中的id属性为hellospring这与调用类中的参数以及控制台提示的字段名不一致,所以错误就在此处啦,我们将它修改为正确的字段helloSpring,不出意外的话,我们再次运行我们的程序应该就可以正确运行啦!

  • 修改并重新运行程序
    Spring报错org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'helloSpring'_第3张图片 运行结果
    Spring报错org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'helloSpring'_第4张图片程序成功的跑起来啦,又一个问题被我们完美解决!

你可能感兴趣的:(Spring异常合集,Spring,Exception)