【问题标题】:AspectJ: Autowired fields are null in InitbinderAspectJ:Initbinder 中的自动装配字段为空
【发布时间】:2014-02-07 11:28:40
【问题描述】:

我刚刚实现了 AspectJ,如下所述:https://stackoverflow.com/a/10998044/2182503

此解决方案运行良好,直到我注意到我的 @Autowired 字段在 @InitBinder 中为空。这些字段仅在 @InitBinder 中为空。

@Controller
public class EmployeeController {
    @Autowired private GenericDaoImpl<Role, Integer> roleDao;
    @Autowired private GenericDaoImpl<Employee, Integer> employeeDao;
    @Autowired private EmployeeValidator employeeValidator;

    @InitBinder
    private void initBinder(WebDataBinder binder) {
        // autowired fields are null 
        binder.setValidator(employeeValidator);
        binder.registerCustomEditor(Set.class, "roles", new CustomCollectionEditor(Set.class) {
            protected Object convertElement(Object element) {
                if (element != null) {
                    Integer id = new Integer((String) element);
                    Role role = roleDao.findById(id);
                    return role;
                }
                return null;
            }
        });
    }

    @PreAuthorize("hasRole('MASTERDATA_VIEW')")
    @RequestMapping(value = { "/employees" }, method = RequestMethod.GET)
    public ModelAndView showEmployeeList() {
        // dao not null
        List<Employee> employees = employeeDao.findAll();
            ...
    }

我无法理解为什么它们有时为空而有时不是。(在同一类中)

【问题讨论】:

    标签: spring spring-mvc aspectj aspectj-maven-plugin spring-mvc-initbinders


    【解决方案1】:

    @Initbinder 必须声明为 public

    【讨论】:

    • 谢谢,我的控制器中的方法之一被标记为私有。无法弄清楚为什么所有其他的都在工作,而那个却没有。
    • 也使用受保护的。谢谢。
    猜你喜欢
    • 2015-07-28
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 2016-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多