코드 재사용 가능성을 향상시키기 위해 일반 컨트롤러를 작성하려고 시도해 왔습니다. 아래는 내가 지금까지 무엇을 가지고 :Spring MVC의 일반 컨트롤러
public abstract class CRUDController<T> {
@Autowired
private BaseService<T> service;
@RequestMapping(value = "/validation.json", method = RequestMethod.POST)
@ResponseBody
public ValidationResponse ajaxValidation(@Valid T t,
BindingResult result) {
ValidationResponse res = new ValidationResponse();
if (!result.hasErrors()) {
res.setStatus("SUCCESS");
} else {
res.setStatus("FAIL");
List<FieldError> allErrors = result.getFieldErrors();
List<ErrorMessage> errorMesages = new ArrayList<ErrorMessage>();
for (FieldError objectError : allErrors) {
errorMesages.add(new ErrorMessage(objectError.getField(),
objectError.getDefaultMessage()));
}
res.setErrorMessageList(errorMesages);
}
return res;
}
@RequestMapping(method = RequestMethod.GET)
public String initForm(Model model) {
service.initializeForm(model);
return "country"; // how can I make this generic too ?
}
}
T
나라, 항목, 등록 및 사용자 같은 것들을 할 수 있습니다. 현재 자동 배선 프로세스에 직면 한 문제는 다음 오류로 인해 실패했습니다.
No unique bean of type [com.ucmas.cms.service.BaseService] is defined: expected single matching bean but found 4: [countryServiceImpl, itemServiceImpl, registrationServiceImpl, userServiceImpl].
내가 원하는 것을 얻을 수 있습니까? 이 문제를 어떻게 해결할 수 있습니까?
스프링 4부터 제네릭 타입을 기반으로 자동 와이어 링이 가능합니다. 내 대답은 업데이트를 참조하십시오. – matsev