0

When i try to add data's into database ,i got this error here i'm not using @Autowired annotation because when i use it got multiple errors instead here i used constructor level injection....

SEVERE: Exception sending context initialized event to listener instance of class [org.springframework.web.context.ContextLoaderListener] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController' defined in file [C:\Users\yuva\eclipse2.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\SSLNew\WEB-INF\classes\com\changepond\spring\controller\HomeController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.changepond.spring.controller.HomeController]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.changepond.spring.controller.HomeController.() at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1076) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1021) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) 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:700) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4766) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5230) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1396) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1386) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:140) at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:919) at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:835) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1396) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1386) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:140) at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:919) at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:263) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.StandardService.startInternal(StandardService.java:432) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:927) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.startup.Catalina.start(Catalina.java:772) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:345) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:476) Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.changepond.spring.controller.HomeController]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.changepond.spring.controller.HomeController.() at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:85) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1069) ... 43 more Caused by: java.lang.NoSuchMethodException: com.changepond.spring.controller.HomeController.() at java.base/java.lang.Class.getConstructor0(Class.java:3349) at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2553) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:80) ... 44 more

Controller:

    @Controller
public class HomeController {
    
    
    //@Autowired
    private ProductService pservice;
    
    
    public HomeController(ProductService pservice) {
        
        this.pservice = pservice;
    }

    @RequestMapping(value = { "/"}, method = RequestMethod.GET)
    public ModelAndView welcomePage() 
    {
        ModelAndView model = new ModelAndView();
        model.setViewName("welcomePage");
        return model;
    }

    @RequestMapping(value = { "/homepage"}, method = RequestMethod.GET)
    public ModelAndView homepage() 
    {
        
        ModelAndView model = new ModelAndView();
        model.setViewName("homepage");
        return model;
    }
    
    @RequestMapping(value = {"/addproduct"}, method = RequestMethod.POST)
    public ModelAndView addproduct(Product product) 
    {
        this.pservice.saveProduct(product);
        ModelAndView model = new ModelAndView();
        model.setViewName("welcomePage");
        return model;
    }
    
    @RequestMapping(value = "/loginPage", method = RequestMethod.GET)
    public ModelAndView loginPage(@RequestParam(value = "error",required = false) String error,
    @RequestParam(value = "logout", required = false) String logout) 
    {
                
        ModelAndView model = new ModelAndView();
        if (error != null) {
            model.addObject("error", "Invalid Credentials provided.");
        }

        if (logout != null) {
            model.addObject("message", "Logged out from JournalDEV successfully.");
        }

        model.setViewName("loginPage");
        return model;
    }

}



@Service
public class ProductService  {

    //@Autowired
    private ProductRepo productRepo;
    
    public ProductService (ProductRepo productRepo)
    {
        this.productRepo = productRepo;
    }
    
    @Transactional
    public boolean saveProduct(Product product) 
    {
        
        this.productRepo.save(product);
        return true;
    }
    
    
}

    Repository
    @Repository
    public interface ProductRepo extends JpaRepository<Product, Integer>{
        
    }
user1712
  • 29
  • 4
  • Remove the _no-arg_ constructor, or annotate `HomeController(ProductService pservice)` with `@Autowired`. Right now, Spring is probably using the no-arg constructor, meaning that `pservice` is `null`. – Mark Rotteveel Jun 24 '22 at 07:37
  • Hi @MarkRotteveel thanks for your reply , i removed the no-arg constructor, now i'm getting 1.org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController' defined in file 2.No default constructor found; nested exception is java.lang.NoSuchMethodException: com.changepond.spring.controller.HomeController.() 3.java.lang.NoSuchMethodException: com.changepond.spring.controller.HomeController.() – user1712 Jun 24 '22 at 10:40
  • Which Spring version do you use, and do you by any chance define beans through XML config? – Mark Rotteveel Jun 24 '22 at 10:43
  • now i'm updated my question and i'm using spring version 4 and not using any xml config – user1712 Jun 24 '22 at 10:48
  • Have you tried adding `@Autowired` to the constructor, instead of the field? I'm not sure which version made `@Autowired` optional for no-arg constructors, but it might have been Spring 5. – Mark Rotteveel Jun 24 '22 at 10:52
  • yes , private ProductService pservice; @Autowired public HomeController(ProductService pservice) { this.pservice = pservice; } then i got expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} – user1712 Jun 24 '22 at 10:57
  • That suggests that `ProductService` is not a bean. – Mark Rotteveel Jun 24 '22 at 11:00
  • ohh..ok can u help me what changes i need to do now? – user1712 Jun 24 '22 at 11:08
  • Likely, `ProductService` needs the `@Component` (or `@Service`) annotation. I do have to say that this is basic Spring knowledge, so you might want to consult a Spring tutorial and/or the Spring documentation. – Mark Rotteveel Jun 24 '22 at 11:12
  • Sure....i will and really thank you for your valuable time – user1712 Jun 24 '22 at 11:19

0 Answers0