Announcement Announcement Module
Collapse
No announcement yet.
Lookup-method injection Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Lookup-method injection

    In spring IoC chapter, there introduces Lookup method injection. This allows us to dynamically inject a prototype beans into a singleton bean.

    Code:
    <!-- a stateful bean deployed as a prototype (non-singleton) -->
    <bean id="command" class="fiona.apple.AsyncCommand" scope="prototype">
       <!-- inject dependencies here as required -->
    </bean>
    
    <!-- commandProcessor uses statefulCommandHelper -->
    <bean id="commandManager" class="fiona.apple.CommandManager">
       <lookup-method name="createCommand" bean="command"/>
    </bean>
    Code:
    package fiona.apple;
    public abstract class CommandManager {
    
     public Object process(Object commandState) {
        // grab a new instance of the appropriate Command interface
        Command command = createCommand();
        // set the state on the (hopefully brand new) Command instance
        command.setState(commandState);
        return command.execute();
     }
    
      // okay... but where is the implementation of this method?
     protected abstract Command createCommand();
    }
    I wanna ask whether it is OK for me to make the CammondManager as prototype bean and then injected by other propotype bean.

    I have implemented this concept. There is no exception right now. However, I don't know whether there is any potential exception/issue which makes the web application to crash or perform bad.

    For my understanding, I know that we cannot "create" or "new" a instance of an abstract class in java. However, in my orderManager is trying to assign/create an instance of abstract class "commandManager" in the spring config. file.

    Actaully, I am using Vaadin for my UI design. I make the Vaadin UI component as a prototype bean (as codes described below) while my serive and DAO layer are singletone beans.

    Please kindly tell me if I misunderstand the concept of Spring IoC.

    Code:
    <!-- a stateful bean deployed as a prototype (non-singleton) -->
    <bean id="command" class="fiona.apple.AsyncCommand" scope="prototype">
       <!-- inject dependencies here as required -->
    </bean>
    
    <!-- commandProcessor uses statefulCommandHelper -->
    <bean id="commandManager" class="fiona.apple.CommandManager" scope="prototype">
       <lookup-method name="createCommand" bean="command"/>
    </bean>
    
    <bean id="orderManager" class="fiona.apple.CommandManager" scope="prototype">
       <property name="commandManager" ref="commandManager"/>
    </bean>
    Code:
    package fiona.apple;
    public [B]abstract[B] class OrderManager{
        private [B]CommandManager commandManager[B];
        //Setter here
    }

    Thanks so much~

  • #2
    Hi Everyone,

    Is it a must that singleton bean is only allowed to work with "Lookup" method?

    Comment

    Working...
    X