Question

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.

Était-ce utile?

La solution 2

After doing some code change I have removed class ProcessAllRequest and the place where I am calling it's method fooBargetProcessor(fooBar f) I have written this code and works fine for me, now I have got instance of any operation / class at run time.

Feeling so good because first time I have done this type of code and works fine.

Code :

First I have Autowired application context in class like :

@Autowired private ApplicationContext context;

then in ProcessRequest() method I have write this code for to get instance :

String processorName = Introspector.decapitalize(f.getClass().getSimpleName() + "Processor");
ICommandProcessor processor = (ICommandProcessor) context.getBean(processorName);

Here f is fooBar object.

So short and simple code, have removed so much un necessary code.

Thank You All.

Autres conseils

If you want to simply get all of your fooBar, why not simply ask Spring for an array or collection of them? Then, in your fooBargetProcessor() you could do something like:

@Autowired
private List<fooBar> processors;

public <T extends fooBar> T getProcessor(Class<T> type) {
   fooBar processor = null;
   for (fooBar p : processors) {
      if (p.getClass().equals(type)) {
         processor = p;
         break;
      }
   }
   return processor;
}

Caveats: This was typed from memory, so some testing and code cleanup might be necessary.

Basically, we ask Spring to give us all fooBars in a List. You will need to have the fooBar instances tagged with @Component or something so that Spring will find them. Spring should find all instances and wire them into the List (see docs for @Autowired). The method then uses Java Generics to figure out which type you want, then loops through the List to find it. You could use a Set instead of a List to guarantee uniqueness of the fooBar objects.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top