I have following code using which I am manually getting instance of class as per request came for processing.
Suppose I have one Interface which will be implemented by each class :
interface fooBar{
}
and following are class in which I am implementing that interface and each class have different body as per request :
class AddFoo implements fooBar{
}
class DeleteFoo implements fooBar{
}
class AddBar implements fooBar{
}
class DeleteBar implements fooBar{
}
following is class that return instance manually as per request :
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;
}
}
I have also tried like return new AddFoo();
but that give me null instance so I have used Autowired
and work perfectly.
now when I will call fooBargetProcessor(fooBar f)
in any class to process request it will give me instance of that class as per request.
So my question is that is there any method so I can get instance automatically without @Autowired
and other static data in fooBargetProcessor()
method ?
UPDATE :
I have called this method in other method name ProcessRequest()
.
And for all I have made commands and mapped each with form data using ModelMapper.
like :
ModelMapper mapper = new ModelMapper();
AddFoo f = mapper.map(form, AddFoo.class);
ProcessRequest(f);
and in ProcessRequest() i have called fooBargetProcessor()
method.
Sample class code :
@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";
}
}
now as per request to the /api/foo/ it will pass to various method like if POST then add and PUT then update and if DELETE then delete methods like so on, to explain full flow and code of my project is somewhat impossible because it takes so much time to make first skeleton.
Now as per request came in Controller I have to just Map data as per request and called method ProcessRequest()
in that controller class.
Anyone know about it then please give me guidance.