Anveshan
Anveshan

Reputation: 624

unable to inject bean in simple spring mvc application

I am trying a simple application in the spring mvc using annotations only. In my service class, I am unable to inject DAO class even though I have used relevant annotations.Please let me know what mistake I am doing. Below are my class definitions.

            public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

                @Override
                protected Class<?>[] getRootConfigClasses() {
                    return new Class[] { SpringRootConfig.class };
                    //return null;
                }

                @Override
                protected Class<?>[] getServletConfigClasses() {
                    return new Class[] { SpringWebConfiguration.class };
                }

                @Override
                protected String[] getServletMappings() {
                    return new String[] { "/" };
                }
            }

         @Configuration
         @EnableWebMvc
         @ComponentScan(basePackages="com.emp.controller")
         public class SpringWebConfiguration extends WebMvcConfigurerAdapter{

            @Bean
            public InternalResourceViewResolver jspViewResolver() {
            InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();       
                        viewResolver.setPrefix("/WEB-INF/view/");
                        viewResolver.setSuffix(".jsp");
                        return viewResolver;

                 }
            }

    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages="com.emp.svc,com.emp.dao")
    public class SpringRootConfig {

    }


    package com.emp.svc;

    import org.springframework.beans.factory.annotation.Autowired;
    import com.emp.dao.LoginDAO;
    public class LoginSvc {

        @Autowired 
        private LoginDAO dao;

        public boolean validateLogin(){
            System.out.println("In the svc method");
            return dao.validateLogin();
        }
    }


    package com.emp.dao;

    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Repository;

    @Repository
    public class LoginDAO {

        public boolean validateLogin(){
            System.out.println("In the DAO method");
            return true;
        }
    }

package com.emp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.emp.forms.LoginForm;
import com.emp.svc.LoginSvc;

@Controller
public class LoginController {

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

    @RequestMapping(value = {"/welcome"},method = RequestMethod.GET)
    public ModelAndView validateLogin(@ModelAttribute LoginForm form){
        ModelAndView model = new ModelAndView();
        LoginSvc svc = new LoginSvc();
        if(svc.validateLogin()){
            model.setViewName("welcome");
        }
        else
        {
            model.setViewName("login");
        }
        return model;
    }

LoginSvc is called from controller. I am expecting that in LoginSvc, LoginDAO will be injected and hence the call to dao dao.validateLogin() will be successful. But what I am getting is Null pointer at dao.validateLogin(), indicating LoginDAO is not injected.

If you can let me know what I am missing, it will be great help.

Upvotes: 1

Views: 420

Answers (1)

Monzurul Shimul
Monzurul Shimul

Reputation: 8396

Add @Service annotation in your LoginSvc class.

package com.emp.svc;

import org.springframework.beans.factory.annotation.Autowired;
import com.emp.dao.LoginDAO;

@Service
public class LoginSvc {

    @Autowired 
    private LoginDAO dao;

    public boolean validateLogin(){
        System.out.println("In the svc method");
        return dao.validateLogin();
    }
}

And autowire LoginSvc inside your controller. As you are creating the object yourself invoking constructor of LoginSvc, spring will not provide you autowire candidate.

@Controller
public class LoginController {

    @Autowired 
    private LoginSvc svc;

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

    @RequestMapping(value = {"/welcome"},method = RequestMethod.GET)
    public ModelAndView validateLogin(@ModelAttribute LoginForm form){
        ModelAndView model = new ModelAndView();            
        if(svc.validateLogin()) {
            model.setViewName("welcome");
        } else {
            model.setViewName("login");
        }
        return model;
    }
}

Upvotes: 2

Related Questions