6

ここで説明されているように 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の場合とそうでない場合があるのか​​ 理解できません。(同じクラス内で)

4

1 に答える 1

15

@Initbinderとして宣言する必要がありますpublic

于 2014-02-07T12:02:43.207 に答える