Spring怎樣在一個bean裏反覆生成另一個bean的新實例

在說到prototype是,有可能我們就會引現出一個需求,這個需求就是,我能不能在一個bean裏,在需要的情況下每次都能產生一個新的prototypebean。基本的特性是不能滿足這個需求的。比如你把bean Ascope定義爲prototype,注入bean B, 只有在注入的這一刻有新的bean A生成,在beanB內部執行的過程中是不會新生成Bean A的。那麼有沒有辦法滿足這一需求?答案是肯定。最不友好的一種方式是使bean B實現接口ApplicationContextAware,這樣在Bean B裏就可以訪問ApplicationContext,這樣就可以顯式調用getBean來每次生成新的Bean A。但這種方式使spring的類***了Bean B,不是優雅的方式。有沒有更優雅的反方式?大事是有。我們可以用Method Injection。例子如下:

XML-Based

package fiona.apple;

// no more Spring imports!

publicabstractclass CommandManager {

public Object process(ObjectcommandState) {
       
// grab a new instance ofthe appropriate Command interface
        Command command =createCommand();
       
// set the state on the(hopefully brand new) Command instance
       command.setState(commandState);
       
returncommand.execute();
    }

//okay... but where is the implementation of this method?
   
protectedabstract CommandcreateCommand();
}

 

From<https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-factory-method-injection>

 

<!-- a statefulbean deployed as a prototype (non-singleton) -->
<beanid="myCommand"class="fiona.apple.AsyncCommand"scope="prototype">
   
<!-- inject dependencies here as required-->
</bean>

<!--commandProcessor uses statefulCommandHelper -->
<beanid="commandManager"class="fiona.apple.CommandManager">
   
<lookup-methodname="createCommand"bean="myCommand"/>
</bean>

 

From<https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-factory-method-injection>

 

Annotation-Based

publicabstractclassCommandManager {

public Object process(ObjectcommandState) {
        Command command =createCommand();
       command.setState(commandState);
       
returncommand.execute();
    }

@Lookup("myCommand")
   
protectedabstract CommandcreateCommand();
}

 

From<https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-factory-method-injection>

 

 

 

 

這樣每次調用createCommand,就有新的Command生成。

 

Method InjectionMethod 應該滿足如下形式:

<public|protected> [abstract] <return-type> theMethodName(no-arguments);

 

From<https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-factory-method-injection>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章