无法在简单的spring mvc应用程序中注入bean

时间:2017-04-25 17:26:19

标签: spring spring-mvc

我正在使用注释在spring mvc中尝试一个简单的应用程序。在我的服务类中,即使我使用了相关的注释,我也无法注入DAO类。请让我知道我在做什么错误。以下是我的班级定义。

            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。我期待在LoginSvc中,将注入LoginDAO,因此对dao dao.validateLogin()的调用将成功。但我得到的是dao.validateLogin()的Null指针,表示没有注入LoginDAO。

如果你能告诉我我所缺少的东西,那将会很有帮助。

1 个答案:

答案 0 :(得分:2)

在LoginSvc类中添加@Service注释。

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();
    }
}

并在控制器内自动装载LoginSvc。当你自己调用LoginSvc的构造函数创建对象时,spring不会为你提供autowire候选者。

@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;
    }
}