Spring怎样在一个bean里反复生成另一个bean的新实例
2017-02-07 11:49
369 查看
在说到prototype是,有可能我们就会引现出一个需求,这个需求就是,我能不能在一个bean里,在需要的情况下每次都能产生一个新的prototype的bean。基本的特性是不能满足这个需求的。比如你把bean A的scope定义为prototype,注入bean B, 只有在注入的这一刻有新的bean A生成,在beanB内部执行的过程中是不会新生成Bean A的。那么有没有办法满足这一需求?答案是肯定。最不友好的一种方式是使bean B实现接口ApplicationContextAware,这样在Bean B里就可以访问ApplicationContext,这样就可以显式调用getBean来每次生成新的Bean A。但这种方式使spring的类入侵了Bean B,不是优雅的方式。有没有更优雅的反方式?大事是有。我们可以用Method Injection。例子如下:XML-Basedpackage 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-BasedpublicabstractclassCommandManager {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 Injection的Method 应该满足如下形式:<public|protected> [abstract] <return-type> theMethodName(no-arguments); From<https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-factory-method-injection>
// 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-BasedpublicabstractclassCommandManager {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 Injection的Method 应该满足如下形式:<public|protected> [abstract] <return-type> theMethodName(no-arguments); From<https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-factory-method-injection>
相关文章推荐
- spring每次getBean(),获得的是否是同一个实例
- spring每次getBean(),获得的是否是同一个实例
- 从头认识Spring-1.7 怎样通过属性注入Bean?(2)-怎样通过属性向对象注入另一个对象的引用?
- spring每次getBean(),获得的是否是同一个实例
- Spring怎样把Bean实例暴露出来?
- Spring入门学习——调用一个实例工厂方法创建Bean
- 【spring set注入 注入集合】 使用set注入的方式注入List集合和Map集合/将一个bean注入另一个Bean
- spring每次getBean(),获得的是否是同一个实例
- spring每次getBean(),获得的是否是同一个实例
- 使一个类在某一时刻只生成一个实例
- 一个spring+hibernate开发的小程序的修改(其中配置文件使用的是HibernateSynchronizer插件生成)
- Spring中如何获取和重置一个BeanFactory对象?或者说,如何动态修改Hibernate Configuration?
- 怎样让程序只开一个实例
- 使用Buffalo集成Spring写的一个登录实例
- 怎样获得Sqlserver 2000得实例列表和运行在一个实例上得数据库列表(转)
- 一个用Spring AOP实现异常处理和记录程序执行时间的实例--邵京国
- Spring学习笔记之Bean的实例化
- 一个用基于智能标签的生成HTML实例
- stl 中怎样遍历一个map中的所有元素。请给是实例
- 关于动态生成WML文件的一个Java实例(1)