@gaussic 你好,想跟你请教个问题:我看了您的spring mvc项目实例教程,一步步参照编写出来后,在用户管理功能上显示页面报错。这是报错:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.gaussic.repository.UserRepository com.gaussic.controller.MainController.userRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#1926e9c6' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1926e9c6': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: No Persistence provider for EntityManager named defaultPersistenceUnit org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:667) org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:633) org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:681) org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:552) org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:493) org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) javax.servlet.GenericServlet.init(GenericServlet.java:158) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436) org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078) org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2517) org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2506) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)
这个是配置问题,仔细检查一下mvc-dispatcher-servlet和persistence.xml
不要忽视错误信息
org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'mainController':Injectionofautowireddependenciesfailed;nestedexceptionisorg.springframework.beans.factory.BeanCreationException:Couldnotautowirefield:com.gaussic.repository.UserRepositorycom.gaussic.controller.MainController.userRepository;nestedexceptionisorg.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'userRepository':Cannotcreateinnerbean'(innerbean)#1926e9c6'oftype[org.springframework.orm.jpa.SharedEntityManagerCreator]whilesettingbeanproperty'entityManager';nestedexceptionisorg.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'(innerbean)#1926e9c6':Cannotresolvereferencetobean'entityManagerFactory'whilesettingconstructorargument;nestedexceptionisorg.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'entityManagerFactory'definedinServletContextresource[/WEB-INF/spring-servlet.xml]:Invocationofinitmethodfailed;nestedexceptionisjavax.persistence.PersistenceException:NoPersistenceproviderforEntityManagernameddefaultPersistenceUnit
上面已经说的很清楚了。
Injectionofautowireddependenciesfailed:autowired的自动注入失败了。再看后面:
Couldnotautowirefield:com.gaussic.repository.UserRepositorycom.gaussic.controller.MainController.userRepository
不能注入这些字段。说明你的在spring-servlet.xml中的配置有问题,被autowired修饰类属性,spring窗口启动时会扫描所有你指定的包中的被autowired修饰的字段,并找到类型名、字段名相同的注入进对象中,而现在容器抛出了这个异常,说明这两个类的在你的spring-serlvet.xml中的配置是有问题。具体的问题还得看你具体的实现。再看后面:
Errorcreatingbeanwithname'userRepository':Cannotcreateinnerbean'(innerbean)#1926e9c6'oftype[org.springframework.orm.jpa.SharedEntityManagerCreator]whilesettingbeanproperty'entityManager'
在设置属性entityManager的时候,创建叫做'<spanstyle="font-size:13.3333px;">userRepository'的bean时出现错误,因为不能创建这个类中的内部类。现在你知道错误在哪儿了吗?后面还有更详细的:
Errorcreatingbeanwithname'(innerbean)#1926e9c6':Cannotresolvereferencetobean'entityManagerFactory'whilesettingconstructorargument
构建bean时出现错误:设置构造方法参数时,无法解析对bean'<spanstyle="font-size:13.3333px;">entityManagerFactory'的引用。
这还不够详细?后面还有:Errorcreatingbeanwithname'entityManagerFactory'definedinServletContextresource[/WEB-INF/spring-servlet.xml]:Invocationofinitmethodfailed
在创建spring-servlet.xml中定义的<spanstyle="font-size:13.3333px;">entityManagerFactory的bean时出现错误:调用init方法失败。
<spanstyle="font-size:13.3333px;">说明你的代码问题出现在<spanstyle="font-size:13.3333px;">entityManagerFactory里。知道了吗?
<divclass='ref'>不要忽视错误信息
org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'mainController':Injectionofautowireddependenciesfailed;nestedexceptionisorg.springframework.beans.factory.BeanCreationException:Couldnotautowirefield:com.gaussic.repository.UserRepositorycom.gaussic.controller.MainController.userRepository;nestedexceptionisorg.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'userRepository':Cannotcreateinnerbean'(innerbean)#1926e9c6'oftype[org.springframework.orm.jpa.SharedEntityManagerCreator]whilesettingbeanproperty'entityManager';nestedexceptionisorg.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'(innerbean)#1926e9c6':Cannotresolvereferencetobean'entityManagerFactory'whilesettingconstructorargument;nestedexceptionisorg.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'entityManagerFactory'definedinServletContextresource[/WEB-INF/spring-servlet.xml]:Invocationofinitmethodfailed;nestedexceptionisjavax.persistence.PersistenceException:NoPersistenceproviderforEntityManagernameddefaultPersistenceUnit
上面已经说的很清楚了。
Injectionofautowireddependenciesfailed:autowired的自动注入失败了。再看后面:
Couldnotautowirefield:com.gaussic.repository.UserRepositorycom.gaussic.controller.MainController.userRepository
不能注入这些字段。说明你的在spring-servlet.xml中的配置有问题,被autowired修饰类属性,spring窗口启动时会扫描所有你指定的包中的被autowired修饰的字段,并找到类型名、字段名相同的注入进对象中,而现在容器抛出了这个异常,说明这两个类的在你的spring-serlvet.xml中的配置是有问题。具体的问题还得看你具体的实现。再看后面:
Errorcreatingbeanwithname'userRepository':Cannotcreateinnerbean'(innerbean)#1926e9c6'oftype[org.springframework.orm.jpa.SharedEntityManagerCreator]whilesettingbeanproperty'entityManager'
在设置属性entityManager的时候,创建叫做'<spanstyle="font-size:13.3333px;">userRepository'的bean时出现错误,因为不能创建这个类中的内部类。现在你知道错误在哪儿了吗?后面还有更详细的:
Errorcreatingbeanwithname'(innerbean)#1926e9c6':Cannotresolvereferencetobean'entityManagerFactory'whilesettingconstructorargument
构建bean时出现错误:设置构造方法参数时,无法解析对bean'<spanstyle="font-size:13.3333px;">entityManagerFactory'的引用。
这还不够详细?后面还有:Errorcreatingbeanwithname'entityManagerFactory'definedinServletContextresource[/WEB-INF/spring-servlet.xml]:Invocationofinitmethodfailed
在创建spring-servlet.xml中定义的<spanstyle="font-size:13.3333px;">entityManagerFactory的bean时出现错误:调用init方法失败。
<spanstyle="font-size:13.3333px;">说明你的代码问题出现在<spanstyle="font-size:13.3333px;">entityManagerFactory里。知道了吗?
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。