Chapter 4. Starting Examples

Table of Contents

A Simple IoC Examples
Build and Package the Application

A Simple IoC Examples

The best way to learn the Microcontainer is through examples. The Microcontainer distribution is bundled with several examples, which we will discuss in later this guide. In this section, let us first have a look at the simple example. It shows the structure of a simple Microcontainer application and how to run the application in both standalone and JBoss AS environments.

The simple example is located in the examples/simple directory of the Microcontainer distribution. It contains a single class under the src/main directory.



   public class SimpleBean
   {
      public SimpleBean()
      {
         System.out.println("SimpleBean() constructor");
      }
   }


      

The SimpleBean object prints to the system console when it is instantiated via the default constructor. Now, we need to use the Microcontainer to instantiate a SimpleBean POJO. We do this by invoking the Microcontainer with the src/resources/META-INF/jboss-beans.xml configuration file.



   <deployment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="urn:jboss:bean-deployer bean-deployer_2_0.xsd"
      xmlns="urn:jboss:bean-deployer:2.0">

      <bean name="Simple"
         class="org.jboss.example.microcontainer.simple.SimpleBean"/>

   </deployment>


      

This configuration file tells the Microcontainer to create an instance of the SimpleBean POJO and manage it under the name Simple . When other objects in the application need to access this SimpleBean instance, they can simply ask the Microcontainer API for the Simple object. Essentially, we just created a SimpleBean singleton instance here. When we run this application, we are expected to see the "SimpleBean() constructor" printout on the console when the Microcontainer creates the Simple object.