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

  • Java Factory Bean and Spring MVC

    Hello,
    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

    Code:
    @Component
    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
    Code:
    @Controller
    @SessionAttributes("managedBean")
    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) {
                                          
                            bean.doSomething(..);
     
    	}
     
    }

    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

    Daniele
Working...
X