요청을 처리 할 때마다 수동으로 클래스 인스턴스를 가져 오는 데 사용하는 다음 코드를 사용합니다.어떤 클래스의 인스턴스를 얻는 방법?
가정하자 나는 각 클래스에 의해 구현됩니다 하나의 인터페이스를 가지고 :
class AddFoo implements fooBar{
}
class DeleteFoo implements fooBar{
}
class AddBar implements fooBar{
}
class DeleteBar implements fooBar{
}
:
interface fooBar{
}
와 다음은 요청에 따라 다른 신체 나는 그 인터페이스와 각 클래스를 구현하고있는 클래스했다입니다
다음은 요청별로 인스턴스를 수동으로 반환하는 클래스입니다.
class ProcessAllRequest{
@Autowired
private AddFoo addfoo;
@Autowired
private DeleteFoo deletefoo;
public fooBar fooBargetProcessor(fooBar f){
if(f.getClass.equals(AddFoo.class)){
return addfoo;
}else if(f.getClass.equals(DeleteFoo.class)){
return deletefoo;
}else return null;
}
}
나는 또한
return new AddFoo();
과 같은 시도했지만 그게 null 인스턴스를 제공하므로
Autowired
을 사용하고 완벽하게 작동합니다.
이제 모든 클래스에서 fooBargetProcessor(fooBar f)
을 호출하여 요청을 처리하면 요청 당 해당 클래스의 인스턴스가 제공됩니다.
그래서 내 질문은 fooBargetProcessor()
메서드에 @Autowired
및 기타 정적 데이터없이 인스턴스를 자동으로 얻을 수있는 방법은 무엇입니까?
UPDATE는 :
나는 다른 방법 이름 ProcessRequest()
에서이 방법이라고했다.
그리고 내가 명령을 내리고 각각을 ModelMapper를 사용하여 양식 데이터로 매핑했습니다.
는 같은
ModelMapper mapper = new ModelMapper();
AddFoo f = mapper.map(form, AddFoo.class);
ProcessRequest(f);
및 ProcessRequest() i have called fooBargetProcessor()
방법
샘플 클래스 코드 : POST 후 추가 업데이트 후 PUT 및 DELETE 경우 다음과 같은 방법을 삭제할 경우처럼
@Controller
@RequestMapping(value = "/api/foo")
public class Foo extends BaseApi{
@RequestMapping(method= RequestMethod.POST,consumes = "application/json")
public @ResponseBody
String add(@RequestBody AddFooForm form){
ModelMapper mapper = new ModelMapper();
AddFoo f = mapper.map(form, AddFoo.class);
ProcessRequest(f);
return "Hello";
}
}
이제/API/foo는에 요청에 따라 /는 다양한 방법으로 전달합니다 너무, 첫 번째 해골을 만드는 데 너무 많은 시간이 걸리기 때문에 프로젝트의 전체 흐름과 코드를 설명하는 것은 다소 불가능합니다.
요청에 따라 요청에 따라 데이터를 매핑하고 해당 컨트롤러 클래스에 ProcessRequest()
메서드를 호출해야합니다.
누구나 알고 있으면 알려주세요.
@Autowired가 어떻게 작동하는지 모르지만 fooBargetProcessor 함수에 대한 반환 유형이 없어야합니까? – TheWalkingCube
코드가 실행되지 않고 반환 유형이 누락되어 코드가 의도하는 바를 확인하지 못합니다. – Kayaman
예이 코드에서는 반환 형식을 제공하지 않았지만 실제로는 모두 반환 형식을 사용하므로 실행되고 클래스의 singleton 인스턴스 만 원합니다. –