< Previous | Next >

Lesson 6: Add dependency injection within a bundle

Dependency injection allows one bean to access another bean without having to implement any code to create the bean instance. The required bean instance is created by the blueprint container by using information that is contained 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.

Adding a service to the bundle

Procedure

  1. Create the interface class:
    1. In Enterprise Explorer, expand CounterServiceBundle > src.
    2. Right-click com.ibm.ws.eba.counter and then select New > Interface. The New Java™ Interface wizard opens.
    3. In the Name field, type Greet and then click Finish. The interface is created in the package and it opens in the editor.
    4. Add the getText() prototype to the interface. The following code is the result:
      package com.ibm.ws.eba.counter;
      
      public interface Greet {
      		public String getText();
      }
  2. Create the implementation class:
    1. Right-click com.ibm.ws.eba.counter and then select New > Class. The New Java Class wizard opens.
    2. In the Name field, type GreetImpl.
    3. Click Add. The Implemented Interfaces Selection dialog opens.
    4. In the Choose interfaces field, type Greet. Select the Greet interface and then click OK.
    5. Click Finish. The class is created in the package and it opens in the editor.
    6. Replace the GreetImpl class with the following implementation:
      package com.ibm.ws.eba.counter;
      
      public class GreetImpl implements Greet {
      	
      	private Counter counter;
      	
      	public void setCounter(Counter c) {
      		counter = c;
      	}
      
      	@Override
      	public String getText() {
      		return counter.getCount()+" Hello";
      	}
      	
      	public void init() {
      		System.out.println("GreetImpl.init() called");
      	}
      
      }
    7. Save GreetImpl.java.

Results

The private variable counter is the reference to an instance of the Counter service that is initialized by the container by using dependency injection. It is set by using the setCounter method.

Updating the blueprint configuration file

About this task

Carry out the following steps to configure the blueprint configuration file to start the Greet service and inject an instance of the Counter service:

Procedure

  1. In Enterprise Explorer, right-click CounterServiceBundle and then select OSGi > Open Blueprint File to open the blueprint file in the editor.
  2. Add the component assembly and configuration information to the blueprint configuration file:
    1. In the Design tab of the editor, select Blueprint and then click Add. The Add Item dialog opens.
    2. Click Bean and then click OK. The Bean Details dialog opens.
    3. Configure the bean:
      1. In the Bean ID field, type GreetBean.
      2. In the Class field, click Browse. The Type Selection dialog opens. In the Choose type name field, type GreetImpl and then select the GreetImpl class. Click OK.
      3. Click OK to accept the changes and close the dialog.
      4. In the editor, in the Initialization method field, type init.
      The bean is added to your blueprint file.
    4. In the Overview section, click GreetBean (Bean) and then click Add. The Add Item dialog opens.
    5. Click Property and then click OK.
    6. Configure the Property:
      • In the Name field, type counter.
      • In the Reference field, click Browse. The Reference Selection dialog opens. Click Bean: CounterBean and then click OK.
    7. In the Overview section, click Blueprint and then click Add.
    8. Click Service and then click OK. The Service Details dialog opens.
    9. Configure the service:
      1. In the Service Interface field, click Browse and then select the Greet interface.
      2. In the Bean Reference field, click Browse and then select Bean: GreetBean. Click OK.
      3. Click OK to accept the changes and close the dialog.
      The service is added to your configuration.
  3. Save the file.

Updating the servlet to invoke the new service

Procedure

  1. In Enterprise Explorer, expand CounterWebBundle > Java Resources > src > com.ibm.ws.eba.servlet.
  2. Double-click CounterServlet.java to open it in the editor.
  3. Locate the doGet() method and replace it with the following implementation:
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		Greet greet;
    		try {
    			InitialContext ic = new InitialContext();
    			greet = (Greet) ic.lookup("osgi:service/"+Greet.class.getName());
    			response.getOutputStream().println("greet.getText()="+greet.getText());
    		} catch (NamingException e) {
    			e.printStackTrace(System.out);
    		}
    	}
  4. In the main menu click Source > Organize Imports. The import statements are updated.
  5. Save CounterServlet.java.

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 is displayed by 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() and GreetImpl.init(), based on the initialization method entries for the CounterImpl and GreetImpl beans in the blueprint file:
[3/30/10 16:59:41:734 EDT] 00000072 StepStartBLA  A   CWWMH0300I: Starting business-level application "WebSphere:blaname=CounterApp".
[3/30/10 16:59:42:406 EDT] 00000072 webapp        I com.ibm.ws.webcontainer.webapp.WebGroupImpl WebGroup SRVE0169I: Loading Web Module: CounterWebBundle.
[3/30/10 16:59:42:453 EDT] 00000072 WASSessionCor I SessionContextRegistry getSessionContext SESN0176I: Will create a new session context for application key default_hostCounterWebBundle
[3/30/10 16:59:42:468 EDT] 00000072 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/30/10 16:59:42:468 EDT] 00000072 FileLocatorIm E   CWPST0164E: The CounterWebBundle composition unit is not found.
[3/30/10 16:59:42:500 EDT] 00000072 StepStartBLA  A   CWWMH0196I: Business-level application "WebSphere:blaname=CounterApp" was started successfully.
[3/30/10 16:59:42:500 EDT] 00000016 SystemOut     O CounterImpl.init() called
[3/30/10 16:59:42:500 EDT] 00000016 SystemOut     O GreetImpl.init() called
Note: If the output from the CounterImpl.init() and GreetImpl.init() is not displayed 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 about the following topics:
  • How to write the code for a bean that uses another bean.
  • How to define property in the blueprint configuration file that instructs the blueprint container to initialize the variable by using 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_lesson7.html