Announcement Announcement Module
No announcement yet.
Java Factory Bean and Spring MVC Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Java Factory Bean and Spring MVC

    I have a doubt regarding FactoryBean pattern, Interfaces and how to integrate this concept with Spring MVC controller. Finally I have a bunch of bean that has to support the same operation so I would like to have one controller as interface to the client that deals with an interface implemented by all my backing beans, I will explain better the concept, imagine that you have

    public class MyObjectFactory {
        public static MyObject getMyObject(String idObject) {
            MyObject myObject;
            idObject= idObject.toLowerCase();
            if (idObject.equals("one")) {
                myObject= new MyConcreteObject1();
            } else if (idObject.equals("two")) {
                myObject= new MyConcreteObject2();
            } else if (idObject.equals("three")) {
                myObject= new MyConcreteObject3();
            return myObject;
    public interface MyObject {
     void doSomething();
    public class MyConcreteObject1 implements MyObject {...}
    public class MyConcreteObject2 implements MyObject {...}
    public class MyConcreteObject3 implements MyObject {...}
    Then I would like to use the abstract object in the controller to write just one Controller and to invoke the right method implemented
    public class HelloController {
    	@RequestMapping(value="/create/{idObject}", method = RequestMethod.GET)
    	public String create((@PathVariable String idObject,Model model) {
                    MyObject myObject = myObjectFactory.getMyObject(idObject);
                    model.addAttribute("managedBean", myObject);
    		return idObject;
    	@RequestMapping(value="/doSomething", method = RequestMethod.GET)
    	public String create(@ModelAttribute("managedBean") MyObject bean) {

    The Spring serialization of the Object even if it's not specified the type at compilation time is working? I mean the call bean.doSomething() will be redirected (as in Java) to the right method or Spring will thrown an exception because it needs to have specified the concrete implementation of the interface (MyConcreteObject1 ,MyConcreteObject2, MyConcreteObject3 ) ?

    thank you so much