org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyController':
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyController':
I am doing spring + hibernate application. When I run the application on tomcat server I am getting some exceptions. Errors
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization started INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'appServlet-servlet': startup date [Sat May 17 19:51:03 CEST 2014]; parent: Root WebApplicationContext INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml] INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/inscription],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.gestEtu.project.controller.CompteController.hello(java.lang.String,java.lang.String,java.lang.String,org.springframework.ui.Model) INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.gestEtu.project.controller.HomeController.home(java.util.Locale,org.springframework.ui.Model) INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0' ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'compteController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.gestEtu.project.model.service.CompteServiceImp com.gestEtu.project.controller.CompteController.compteServ; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'compteServiceImp': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.gestEtu.project.model.service.CompteServiceImp.setCompteDAO(com.gestEtu.project.model.dao.CompteDAOHib); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'compteDAOHib' defined in file [C:\Users\zouhair\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\TESTER\WEB-INF\classes\com\gestEtu\project\model\dao\CompteDAOHib.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.gestEtu.project.model.dao.CompteDAOHib]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.gestEtu.project.model.dao.CompteDAOHib.() at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672) at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543) at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484) at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) at javax.servlet.GenericServlet.init(GenericServlet.java:160) at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1189) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1103) at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1010) at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4935) at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5262) at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5257) 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) Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.gestEtu.project.model.service.CompteServiceImp com.gestEtu.project.controller.CompteController.compteServ; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'compteServiceImp': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.gestEtu.project.model.service.CompteServiceImp.setCompteDAO(com.gestEtu.project.model.dao.CompteDAOHib); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'compteDAOHib' defined in file [C:\Users\zouhair\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\TESTER\WEB-INF\classes\com\gestEtu\project\model\dao\CompteDAOHib.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.gestEtu.project.model.dao.CompteDAOHib]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.gestEtu.project.model.dao.CompteDAOHib.() at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) ... 27 more Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'compteServiceImp': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.gestEtu.project.model.service.CompteServiceImp.setCompteDAO(com.gestEtu.project.model.dao.CompteDAOHib); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'compteDAOHib' defined in file [C:\Users\zouhair\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\TESTER\WEB-INF\classes\com\gestEtu\project\model\dao\CompteDAOHib.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.gestEtu.project.model.dao.CompteDAOHib]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.gestEtu.project.model.dao.CompteDAOHib.() at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1017) at org.springframework.be root-context.xml
com/gestEtu/project/model/bo/Utilisateur.hbm.xml com/gestEtu/project/model/bo/Compte.hbm.xml com/gestEtu/project/model/bo/Message.hbm.xml com/gestEtu/project/model/bo/Groupe.hbm.xml com/gestEtu/project/model/bo/Etudiant.hbm.xml com/gestEtu/project/model/bo/Fichelecture.hbm.xml com/gestEtu/project/model/bo/Document.hbm.xml com/gestEtu/project/model/bo/Commentaire.hbm.xml
org.hibernate.dialect.MySQLDialect true update servlet-context.xml
CompteController.java
package com.gestEtu.project.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import com.gestEtu.project.model.bo.Compte; import com.gestEtu.project.model.service.CompteServiceImp; @Controller public class CompteController { @Autowired private CompteServiceImp compteServ; // @Autowired // public CompteController(CompteServiceImp compteServ) { // this.compteServ=compteServ; // } // @RequestMapping(value = "/inscription") public String hello( @RequestParam(value = "login", required = false, defaultValue = "zouhair") String login, @RequestParam(value = "password", required = false, defaultValue = "pass") String password, @RequestParam(value = "profil", required = false, defaultValue = "Etudiant") String profil, Model model) { int id = 1; Compte compte = new Compte(id, login, password, profil); compteServ.ajouterCompte(compte); model.addAttribute("inscription", compte); return "addCompte"; } } compteService
package com.gestEtu.project.model.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.springframework.stereotype.Service; import com.gestEtu.project.model.bo.Compte; import com.gestEtu.project.model.bo.Utilisateur; import com.gestEtu.project.model.dao.CompteDAOHib; @Service @Transactional(readOnly = true) public class CompteServiceImp implements CompteService { private CompteDAOHib compteDAO; @Autowired public void setCompteDAO(CompteDAOHib compteDAO) { this.compteDAO = compteDAO; } @Override public void ajouterCompte(Compte compte) { compteDAO.ajouterCompte(compte); } @Override public List getComptes() { return compteDAO.getComptes(); } @Override public Compte findCompteByLogin(String login) { return compteDAO.findCompteByLogin(login); } @Override public void ajouterUtilisateur(Compte compte, Utilisateur utilisateur) { this.ajouterCompte(compte); } @Override public Utilisateur getUtilisateurById(int id) { // TODO Auto-generated method stub return null; } @Override public List getUtilisateurs() { // TODO Auto-generated method stub return null; } } Can anybody help to resolve this. I have tried a lot and google it as well.But did get the solution.
CompteDAOHib
package com.gestEtu.project.model.dao; import java.util.List; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.gestEtu.project.model.bo.Compte; @Repository @Transactional(propagation=Propagation.REQUIRED) public class CompteDAOHib extends HibernateDaoSupport implements CompteDAO { public CompteDAOHib(SessionFactory sessionFactory) { setSessionFactory(sessionFactory); } @Override public void ajouterCompte(Compte compte) { getHibernateTemplate().saveOrUpdate(compte); } @Override public List getComptes() { try{ return (List) getHibernateTemplate().find("from compte") .iterator(); }catch (Exception e) { return null; } } @Override public Compte findCompteByLogin(String login) { if ((login == null) || (login == "")) { return null; } else try { return (Compte) getHibernateTemplate() .find("from Compte c where c.login = ?", login) .iterator().next(); } catch (Exception e) { return null; } } } CompteService Code
package com.gestEtu.project.model.service; import java.util.List; import com.gestEtu.project.model.bo.Compte; import com.gestEtu.project.model.bo.Utilisateur; public interface CompteService { public void ajouterCompte(Compte compte); public List getComptes(); public Compte findCompteByLogin(String login); public List getUtilisateurs(); public Utilisateur getUtilisateurById(int id); public void ajouterUtilisateur(Compte compte, Utilisateur utilisateur); } Answer by Sajan Chandran for org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyController':
Exception clearly indicates the problem.
CompteDAOHib: No default constructor found
For spring to instantiate your bean, you need to provide a empty constructor for your class CompteDAOHib.
Answer by geddamsatish for org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyController':
Change from @Controller to @Service to CompteController and add @Service annotation to CompteDAOHib. Let me know if you still face this issue.
Answer by matsev for org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyController':
Copied from the stacktrace:
BeanInstantiationException: Could not instantiate bean class [com.gestEtu.project.model.dao.CompteDAOHib]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.gestEtu.project.model.dao.CompteDAOHib.
()
By default, Spring will try to instantiate beans by calling a default (no-arg) constructor. The problem in your case is that the implementation of the CompteDAOHib has a constructor with a SessionFactory argument. By adding the @Autowired annotation to a constructor, Spring will attempt to find a bean of matching type, SessionFactory in your case, and provide it as a constructor argument, e.g.
@Autowired public CompteDAOHib(SessionFactory sessionFactory) { // ... } Answer by Lalchand Mali for org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyController':
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyController'
Make sure that you have added ojdbc14.jar into your library.
Answer by Damiani for org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'MyController':
I have been getting similar error, and just want to share with you. maybe it will help someone.
If you want to use EntityManagerFactory to get an EntityManager, make sure that you will use:
and not:
in persistance.xml
clean and rebuild project, it should help.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment