Similarly to the Spring container, the blueprint container enables you to
instantiate Java classes using a bean
element. You can create all
of your main application objects this way. In particular, you can use the
bean
element to create a Java object that represents an OSGi
service instance.
The blueprint bean
element is defined in the blueprint schema
namespace, http://www.osgi.org/xmlns/blueprint/v1.0.0
. The
blueprint {http://www.osgi.org/xmlns/blueprint/v1.0.0}bean
element
should not be confused with the Spring
{http://www.springframework.org/schema/beans}bean
selement,
which has a similar syntax but is defined in a different namespace.
![]() | Note |
---|---|
The Spring DM specification version 2.0 or later, allows you to mix both
kinds of |
The blueprint bean
element enables you to create objects using a
similar syntax to the conventional Spring bean
element. One
significant difference, however, is that blueprint constructor arguments are
specified using the argument
child element, in contrast to Spring's
constructor-arg
child element. The following example shows how
to create a few different types of bean using blueprint's bean
element:
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"> <bean id="label" class="java.lang.String"> <argument value="LABEL_VALUE"/> </bean> <bean id="myList" class="java.util.ArrayList"> <argument type="int" value="10"/> </bean> <bean id="account" class="org.fusesource.example.Account"> <property name="accountName" value="john.doe"/> <property name="balance" value="10000"/> </bean> </blueprint>
Where the Account
class referenced by the last bean example could
be defined as follows:
// Java package org.fusesource.example; public class Account { private String accountName; private int balance; public Account () { } public void setAccountName(String name) { this.accountName = name; } public void setBalance(int bal) { this.balance = bal; } ... }
Althought the syntax of the blueprint bean
element and the Spring
bean
element are similar, there are a few differences, as you
can see from Table 10.1. In
this table, the XML tags (identifiers enclosed in angle brackets) refer to child
elements of bean
and the plain identifiers refer to
attributes.
Table 10.1. Comparison of Spring bean with Blueprint bean
Spring DM Attributes/Tags | Blueprint Attributes/Tags |
---|---|
id | id |
name /<alias> | N/A |
class | class |
scope | scope=("singleton"|"prototype") |
lazy-init=("true"|"false")
| activation=("eager"|"lazy") |
depends-on | depends-on |
init-method | init-method |
destroy-method | destroy-method |
factory-method | factory-bean |
factory-bean | factory-ref |
<constructor-arg> | <argument> |
<property> | <property> |
Where the default value of the blueprint scope
attribute is
singleton
and the default value of the blueprint
activation
attribute is eager
.
For more details on defining blueprint beans, consult the following references:
Spring Dynamic Modules Reference Guide v2.0 (see the blueprint chapters).
Section 121 Blueprint Container Specification, from the OSGi Compendium Services R4.2 specification.