【问题标题】:@Autowired a DAO inside a Validator@Autowired 验证器内部的 DAO
【发布时间】:2014-03-16 08:51:26
【问题描述】:

是否可以在 Validator 中 @Autowired 一个 DAO ? 当我使用它时,我得到一个空指针。 当 userDAO 在验证器中被调用时,运行它会给我一个 NullPointerException。我包含所有代码和应用程​​序上下文

public class UserFormValidator implements Validator {

@Autowired
private UserDAO userDAO;  

@Override
public boolean supports(Class<?> clazz) {
    return UserForm.class.equals(clazz);
}

@Override
public void validate(Object target, Errors errors) {
    System.out.println ("User -> " + userDAO.findById(new Integer(1)));
}
}

@Repository("userDAO")
public class UserDAOImpl extends GenericDAOImpl implements UserDAO {

public UserDAOImpl() {
    super();
}
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:sws="http://www.springframework.org/schema/web-services"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/web-services 
        http://www.springframework.org/schema/web-services/web-services-2.0.xsd">

<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />

<!-- Scans the classpath for annotated components that will be auto-registered 
    as Spring beans. For example @Controller and @Service. Make sure to set the 
    correct base-package -->
<context:component-scan base-package="com.myapp" />
<context:annotation-config/>



<!-- Configures the annotation-driven Spring MVC Controller programming 
    model. Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
<mvc:annotation-driven />

<!-- Load Hibernate related configuration -->
<!-- Here you can also add spring security context, if exist -->
<import resource="hibernate-context.xml" />


</beans>

谢谢!

【问题讨论】:

标签: spring validation spring-mvc dependency-injection autowired


【解决方案1】:

尝试在 UserFormValidator 中添加@Component 注解

【讨论】:

    【解决方案2】:

    变化

    binder.setValidator(new UserFormValidator());

    为

    binder.setValidator(userFormValidator);

    解决了问题

    【讨论】:

      【解决方案3】:

      验证器包名称:com.myapp.

      添加@Component注解:

      这里是我实例化验证器的控制器

      @Controller
      public class HomeController {
      
      private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
      
      
      @InitBinder
      protected void initBinder(WebDataBinder binder) {
          binder.setValidator(new UserFormValidator());
      }
      
      /**
       * Simply selects the home view to render by returning its name.
       */
      @RequestMapping(value = "/home", method = RequestMethod.GET)
      public String home(Locale locale, Model model) {
      
          model.addAttribute("userForm", new UserForm());
          return "home";
      }
      
      @RequestMapping(value = "/login",method = RequestMethod.POST)
       public String submitForm(@Valid UserForm  userForm, BindingResult result,ModelMap model) {
      
        if (result.hasErrors()) {
               return "home";
           }
      
        model.addAttribute("userForm",userForm);
        return "doors";
      
       }
      
      @ModelAttribute("userForm")
      public UserForm createModel() {
          return new UserForm();
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多