< Previous | Next >

Lesson 7: Add dependency injection between bundles

In the previous lesson, you learned how to use dependency injection within a bundle. Dependency injection between bundles requires an extra reference entry in the blueprint configuration file.

Before you begin

Before you begin this lesson, stop the server. In the Servers view (Window > Show View > Servers), right-click WebSphere Application Server and select Stop.

Creating a bundle project

About this task

To create a bundle project and add it to an OSGi application project:

Procedure

  1. Click File > New > Other and then expand OSGi.
  2. Click OSGi Bundle Project and then click Next. The New OSGi Bundle Project opens.
  3. In the Project name field, type CounterWorldBundle.
  4. Select one of the following servers from the Target runtime list:
    • WebSphere Application Server v7.0
    • WebSphere Application Server v8.0
    • WebSphere Application Server v8.5
    • WebSphere Application Server V8.5 Liberty
  5. Ensure that CounterApp is displayed in the Application project field and then click Finish.

Adding a service to the bundle

About this task

In this lesson, you create a simple service, World that returns a text string from the method World.getText().

Procedure

  1. Create the package:
    1. In Enterprise Explorer, expand CounterWorldBundle.
    2. Right-click src and then select New > Package. The New Java™ Package wizard opens.
    3. In the Name field, type com.ibm.ws.eba.world and then click Finish. The package is created in the src folder.
  2. Create the interface class:
    1. Right-click the package com.ibm.ws.eba.world and then click New > Interface. The New Java Interface wizard opens.
    2. In the Name field, type World and then click Finish. The interface is created in the package and it opens in the editor.
    3. Add the getText() method to the interface. The following code is the result:
      package com.ibm.ws.eba.world;
      
      public interface World {
      	public String getText();
      }
    4. Save World.java.
  3. Create the implementation class:
    1. Right-click the package com.ibm.ws.eba.world and then click New > Class. The New Java Class wizard opens.
    2. In the Name field, type WorldImpl.
    3. Click Add. The Implemented Interfaces Selection dialog opens.
    4. In the Choose interfaces field, type World. Select the World interface for com.ibm.ws.eba.world and then click OK.
    5. Click Finish. The class is created in the package and it opens in the editor.
    6. Add the implementation for the getText() method. Add an initialization method that confirms that the service starts on the server. The following code is the result:
      package com.ibm.ws.eba.world;
      
      public class WorldImpl implements World {
      
        @Override
        public String getText() {
          return " World!";
        }
      		
        public void init() {
         System.out.println("WorldImpl.init() called.");
        }
      }
    7. Save WorldImpl.java.
  4. Create the blueprint configuration file:
    1. Right-click the project CounterWorldBundle and select New > Other > OSGi > Blueprint File and then click Next.
    2. Click Finish. The blueprint configuration file opens in the editor.
  5. Add the component assembly and configuration information to the blueprint configuration file:
    1. In the Design tab of the editor, click Add. The Add Item dialog opens.
    2. Click Bean and then click OK. The bean is added to your configuration.
    3. Configure the bean:
      • In the ID field, type WorldBean.
      • In the Class field, click Browse. The Type Selection dialog opens. In the Choose type name field, type WorldImpl and then select the WorldImpl class. Click OK.
      • In the Initialization method field, type init.
    4. Click Blueprint and then click Add. The Add Item dialog opens.
    5. Click Service and then click OK. The service is added to your configuration.
    6. Configure the service:
      • In the ID field, type WorldService.
      • In the Interface field, click Browse and then select the World interface.
      • In the Reference field, click Browse and then select Bean: WorldBean. Click OK.
    7. Save the file.
  6. Export the package:
    1. Expand CounterWorldBundle and then double-click Manifest: CounterWorldBundle to open the bundle manifest file in the editor.
    2. Select the Runtime tab.
    3. In the Exported Packages section of the editor, click Add. The Exported Packages dialog opens.
    4. Click com.ibm.ws.eba.world from the packages list and then click OK.
    5. Save the bundle manifest file.

Updating the bean to use the new service

About this task

Now that you have implemented the World service, you need to update the GreetImpl bean and the CounterServiceBundle bundle manifest file to use the World service.

Procedure

  1. Import the package:
    1. In Enterprise Explorer, expand CounterServiceBundle and then double-click Manifest: CounterServiceBundle to open the bundle manifest file in the editor.
    2. Select the Dependencies tab.
    3. In the Imported Packages section of the editor, click Add. The Imported Packages dialog opens.
    4. Click com.ibm.ws.eba.world from the packages list and then click OK.
    5. Save the bundle manifest file.
  2. Update GreetImpl.java:
    1. In Enterprise Explorer, expand CounterServiceBundle > src > com.ibm.ws.eba.counter and then double-click GreetImpl.java to open the file in the editor.
    2. Add the following variable declaration and set method for the World bean:
      private World worldBean;
      public void setWorldBean(World b)
      {
        worldBean = b;
      }
    3. In the main menu click Source > Organize Imports. The missing import statements are added to your code.
    4. Update the getText() method to call the new service:
      public String getText(){
        return counter.getCount()+" Hello"+worldBean.getText();
      }
    5. Save the file.
    The following code is the result:
    package com.ibm.ws.eba.counter;
    
    import com.ibm.ws.eba.world.World;
    
    public class GreetImpl implements Greet {
      private World worldBean;
      public void setWorldBean(World b)
      {
        worldBean = b;
      }
    	
      private Counter counter;
    	
      public void setCounter(Counter c) {
        counter = c;
      }
    
      @Override
      public String getText(){
        return counter.getCount()+" Hello"+worldBean.getText();
    }
    	
      public void init() {
        System.out.println("GreetImpl.init() called");
      }
    
    }

Updating the blueprint configuration file

About this task

In order for the blueprint container to inject an instance of the World bean into GreetImpl, you need to add the reference to the blueprint configuration file.

Procedure

  1. Expand CounterServiceBundle > BundleContent > OSGI-INF > blueprint and then double-click blueprint.xml to open the blueprint configuration file in the editor.
  2. In the Design tab of the editor, select Blueprint and then click Add. The Add Item dialog opens.
  3. Click Reference and then click OK. The Reference Details dialog opens.
  4. Configure the Reference:
    • In the Reference ID field, type WorldRef.
    • In the Reference Interface field, click Browse and then select the World interface that you created. Click OK.
    • Click OK to accept the changes and close the dialog.
    The reference is added to your blueprint file.
  5. In the Overview section, click the GreetBean (Bean) node and then click Add. The Add Item dialog opens.
  6. Click Property and then click OK. The property is added to your configuration.
  7. Configure the property:
    • In the Name field, type worldBean.
    • In the Reference field, click Browse. The Reference Selection dialog opens. Click Reference:WorldRef and then click OK.
  8. Save the file.

Deploying the application

Procedure

  1. In Enterprise Explorer, expand CounterWebBundle > CounterWebBundle > Servlets.
  2. Right click CounterServlet and select Run As > Run on Server. The Run On Server dialog opens.
  3. Click Finish.

Results

The string greet.getText()=0 Hello World! is displayed in the browser. Each time the page is reloaded the value increments.

Switch to the Console view (Window > Show View > Console) to view the output from the server. A successful outcome displays the output from CounterImpl.init(), GreetImpl.init(), and WorldImpl.init(), based on the initialization method entries for the CounterImpl, GreetImpl, and WorldImpl beans in the blueprint file:
1/10 13:07:26:250 EDT] 000000aa StepStartBLA  A   CWWMH0300I: Starting business-level application "WebSphere:blaname=CounterApp".
[3/31/10 13:07:27:000 EDT] 000000aa webapp        I com.ibm.ws.webcontainer.webapp.WebGroupImpl WebGroup SRVE0169I: Loading Web Module: CounterWebBundle.
[3/31/10 13:07:27:046 EDT] 000000aa WASSessionCor I SessionContextRegistry getSessionContext SESN0176I: Will create a new session context for application key default_hostCounterWebBundle
[3/31/10 13:07:27:062 EDT] 000000aa webcontainer  I com.ibm.ws.wswebcontainer.VirtualHost addWebApplication SRVE0250I: Web Module CounterWebBundle has been bound to default_host[*:9083,*:80,*:9446,*:5067,*:5066,*:443].
[3/31/10 13:07:27:078 EDT] 000000aa FileLocatorIm E   CWPST0164E: The CounterWebBundle composition unit is not found.
[3/31/10 13:07:27:093 EDT] 000000aa StepStartBLA  A   CWWMH0196I: Business-level application "WebSphere:blaname=CounterApp" was started successfully.
[3/31/10 13:07:27:109 EDT] 00000066 SystemOut     O WorldImpl.init() called
[3/31/10 13:07:27:109 EDT] 00000015 SystemOut     O CounterImpl.init() called
[3/31/10 13:07:27:125 EDT] 00000015 SystemOut     O GreetImpl.init() called
Note: If the output from the CounterImpl.init(), GreetImpl.init(), and WorldImpl.init() does not display in the console output, check the output for error messages during deployment or startup of the application and then check the blueprint files for possible errors in the bean and service definitions.

Lesson checkpoint

You learned how to use blueprint dependency injection to allow one bean to use the services of another.

In this lesson, you learned how to use a blueprint Reference to configure cross bundle dependency injection.
< Previous | Next >
Icon that indicates the type of topic Tutorial lesson topic
Timestamp icon Last updated: July 17, 2017 21:58

File name: counter_lesson8.html