ここで説明されているように AspectJ を実装しました: https://stackoverflow.com/a/10998044/2182503
@Autowired
私のフィールドが 内で null であることに気付くまで、このソリューションは正常に機能します@InitBinder
。フィールドは、 内でのみ null@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();
...
}
なぜそれらがnullの場合とそうでない場合があるのか 理解できません。(同じクラス内で)