和Spring一样,blueprint也支持prototype作用域。但与Spring不同的是,我看不到任何关于如何使用它的文档。
在Spring中,你可以询问上下文来给你一个新的bean,在Blueprint世界中有什么等价物呢?
发布于 2013-11-06 10:26:23
BlueprintContainer.getComponentInstance()做的正是您想要的。
osgi documentation:
蓝图容器表示蓝图包的托管状态。Blueprint Container提供对所有托管组件的访问。这些是bean、服务和服务引用。蓝图容器可以通过注入预定义的"blueprintContainer“组件id来获得。
示例
blueprint.xml:
<!-- blueprintContainer is predefined component here -->
<bean id="myService" class="myPackage.MyService">
<property name="container" ref="blueprintContainer"/>
</bean>
<!-- prototype which can be created from myService -->
<bean id="myPrototype" class="myPackage.MyPrototype" scope="prototype"/>MyService.java:
// ...
// create new instance
MyPrototype myPrototype =
(MyPrototype) container.getComponentInstance("myPrototype");pom.xml:
<!-- BlueprintContainer from Apache Aries-->
<dependency>
<groupId>org.apache.aries.blueprint</groupId>
<artifactId>org.apache.aries.blueprint.core</artifactId>
<version>1.3.0</version>
</dependency>发布于 2013-10-16 14:38:31
如果bean的作用域是prototype,那么每次将bean注入到某个地方时,都会创建一个新的bean实例。因此,每个获得prototype注入作用域的bean的客户机都会获得bean的一个新实例。
https://stackoverflow.com/questions/19384659
复制相似问题