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
About this task
Adding a service to the bundle
Procedure
Results
Updating the blueprint configuration file
About this task
Procedure
Updating the servlet to invoke the new service
Procedure
Deploying the application
Procedure
- In Enterprise Explorer, expand .
- Right click CounterServlet and select . The Run On Server dialog opens.
- 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 (
) 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.