Osgi 在蓝图中连接原型

Osgi 在蓝图中连接原型,osgi,blueprint-osgi,Osgi,Blueprint Osgi,与Spring一样,blueprint支持原型范围。但与Spring不同,我看不到任何关于如何使用它的文档 在Spring中,您可以要求上下文为您提供一个新bean,Blueprint世界中的等价物是什么?如果一个bean属于范围原型,那么每次在某处注入bean时,都会创建一个新的bean实例。因此,每个注入了范围原型bean的客户机都会获得一个新的bean实例。如果一个bean是范围原型bean,那么每次在某个地方注入bean时都会创建一个新的bean实例。因此,每个获得注入的范围原型bean

与Spring一样,blueprint支持原型范围。但与Spring不同,我看不到任何关于如何使用它的文档


在Spring中,您可以要求上下文为您提供一个新bean,Blueprint世界中的等价物是什么?

如果一个bean属于范围原型,那么每次在某处注入bean时,都会创建一个新的bean实例。因此,每个注入了范围原型bean的客户机都会获得一个新的bean实例。

如果一个bean是范围原型bean,那么每次在某个地方注入bean时都会创建一个新的bean实例。因此,每个获得注入的范围原型bean的客户机都会获得该bean的一个新实例。

BlueprintContainer.getComponentInstance()正是您想要的

蓝图容器表示蓝图的托管状态 捆Blueprint容器提供对所有受管对象的访问 组件。这些是bean、服务和服务引用。A. 蓝图容器可以通过注入预定义的 “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"/>
// ...
// create new instance
MyPrototype myPrototype = 
     (MyPrototype) container.getComponentInstance("myPrototype");
<!-- BlueprintContainer from Apache Aries-->
<dependency>
  <groupId>org.apache.aries.blueprint</groupId>
  <artifactId>org.apache.aries.blueprint.core</artifactId>
  <version>1.3.0</version>
</dependency>
pom.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"/>
// ...
// create new instance
MyPrototype myPrototype = 
     (MyPrototype) container.getComponentInstance("myPrototype");
<!-- BlueprintContainer from Apache Aries-->
<dependency>
  <groupId>org.apache.aries.blueprint</groupId>
  <artifactId>org.apache.aries.blueprint.core</artifactId>
  <version>1.3.0</version>
</dependency>

org.apache.aries.blueprint
org.apache.aries.blueprint.core
1.3.0

BlueprintContainer.getComponentInstance()完全满足您的需求

蓝图容器表示蓝图的托管状态 捆Blueprint容器提供对所有受管对象的访问 组件。这些是bean、服务和服务引用。A. 蓝图容器可以通过注入预定义的 “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"/>
// ...
// create new instance
MyPrototype myPrototype = 
     (MyPrototype) container.getComponentInstance("myPrototype");
<!-- BlueprintContainer from Apache Aries-->
<dependency>
  <groupId>org.apache.aries.blueprint</groupId>
  <artifactId>org.apache.aries.blueprint.core</artifactId>
  <version>1.3.0</version>
</dependency>
pom.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"/>
// ...
// create new instance
MyPrototype myPrototype = 
     (MyPrototype) container.getComponentInstance("myPrototype");
<!-- BlueprintContainer from Apache Aries-->
<dependency>
  <groupId>org.apache.aries.blueprint</groupId>
  <artifactId>org.apache.aries.blueprint.core</artifactId>
  <version>1.3.0</version>
</dependency>

org.apache.aries.blueprint
org.apache.aries.blueprint.core
1.3.0

bean不会被注入任何地方。bean将是一个有状态的方法对象,它将引用其他单例。因此,工厂将需要创建一个完全与自己的依赖项连接的工厂。对象本身是服务方法中的局部变量,即实现给定任务的委托实例。bean不会被注入任何地方。bean将是一个有状态的方法对象,它将引用其他单例。因此,工厂将需要创建一个完全与自己的依赖项连接的工厂。对象本身是服务方法中的局部变量,即实现给定任务的委托实例。