开发者社区> 问答> 正文

运行页面报错org.springframework.beans.factory.?报错

@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)


展开
收起
爱吃鱼的程序员 2020-06-09 14:39:08 751 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    这个是配置问题,仔细检查一下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里。知道了吗?

    2020-06-09 14:39:24
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
introduction to Apache Bean 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载