LibraryToggle FramesPrintFeedback

Defining a Service Bean

Overview

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.

Blueprint bean element

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]Note

The Spring DM specification version 2.0 or later, allows you to mix both kinds of bean element under the beans root element, (as long as you define each bean elements using the appropriate namespace prefix).

Sample beans

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;
    }
    ...
}

Differences between Blueprint and Spring

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 2. 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 2. Comparison of Spring bean with Blueprint bean

Spring DM Attributes/TagsBlueprint Attributes/Tags
idid
name/<alias>N/A
classclass
scopescope=("singleton"|"prototype")
lazy-init=("true"|"false") activation=("eager"|"lazy")
depends-ondepends-on
init-methodinit-method
destroy-methoddestroy-method
factory-methodfactory-bean
factory-beanfactory-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.

References

For more details on defining blueprint beans, consult the following references:

Comments powered by Disqus