我刚刚实现了 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();
...
}
我无法理解为什么它们有时为空而有时不是。(在同一个班级内)