框架:具有约束性的支撑我们实现功能的半成品的项目。
遇到Spring的错误,就看最后一个错误就行了,前提是只有一处错误。
SpringError_one:
Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type [com.atguigu.spring.mod.Person] is defined: expected single matching bean but found 2: person,person1
错误说明:使用getbean(class)方法获取对象,由于该类对象有多个,不知道该取哪一个报错.
解决方案:推荐改为使用getbean(String id,class )两个条件获取.
SpringError_two:
Caused by: java.lang.NoSuchMethodException: com.atguigu.spring.mod.Person.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getDeclaredConstructor(Unknown Source)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80)
... 14 more
错误说明:用反射生成对象时只能调用无参构造,错误为没有找到对应类中的无参构造。
解决方案:加入无参构造.
SpringError_three:
Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.util.ArrayList]
to required type [com.atguigu.spring.di.Student] for property 'students[0]': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:267)
at org.springframework.beans.TypeConverterDelegate.convertToTypedCollection(TypeConverterDelegate.java:537)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:202)
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:458)
... 17 more
错误说明:类型转换异常,集合与引用对象不能相互转换
解决方案:改变数据类型
SpringError_fore:
Caused by: java.lang.ClassNotFoundException: org.springframework.aop.TargetSource
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 25 more
错误说明:当报类未找到这个错误并且Caused by: java.lang.ClassNotFoundException: org.springframework.aop.TargetSource中
的org.springframework.aop.TargetSource不是Java或你导过的jar包开头的,一般证明没有导入相应的jar包
解决方案:导入相应jar包
SpringError_five:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.atguigu.ioc.userMod.service.UserService]
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:1100)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
... 15 more
错误说明:没有足够的bean被定义异常,一般的错误来源是由于没有给类加相应的注解
,再者是有可能在Spring配置文件中,扫描组件的部分包导错或写错。
解决方案:给类添加注解,还有问题就去配置文件的扫描组件的部分检查导包或书写问题。
SpringError_six:
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:170)
at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:921)
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:841)
at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:551)
at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:115)
at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:5165)
at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5829)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:221)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:149)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
错误说明: BeanFactory not initialized or already closed
①要么是因为spring的配置文件中有语法错误,
②要么是因为找不到spring的配置文件。
③要么是因为spring的配置文件还没有创建。
解决方案:由于第一个的错误原因比较复杂,这里只说明后两种的解决方案
在web.xml中写了这个listener而没有配置spring的路径,因为默认spring的路径是在WEB-INF下默认名字为applicationContext.xml
配置了<context-param>这个标签系统就能找到classpath:spring.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>