# Examples

## A Simple Example

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's 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_1_0.xsd"
        xmlns="urn:jboss:bean-deployer">

      <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 for the `Simple` object from the Microcontainer API. Essentially, we just created a `SimpleBean` singleton instance here. When we run this application, we are expected to see the `"SimpleBean() constructor"` printout from the console when the Microcontainer creates the `Simple` object.

## Build and Package the Application

To build, package, and run the `simple` application, we can simply execute the `ant` command in the `examples/simple` directory. It runs the `build.xml` script, which further invokes the `examples/build-common.xml` script. The `compile` and `dist` tasks in the build script builds the application.

    <target name="compile">

       <mkdir dir="build"/>

       <javac destdir="build"
              debug="on"
              deprecation="on"
              optimize="on"
              classpathref="compile.classpath">
          <src path="src"/>
       </javac>
    </target>

    <target name="dist" depends="compile">

       <copy todir="build">
          <fileset dir="src/resources"/>
       </copy>

       <mkdir dir="dist"/>

       <jar jarfile="dist/${ant.project.name}.beans" basedir="build"/>

    </target>

The `compile` task compiles the Java source files into class files under the `build` directory. Then, the `dist` task packages the class files and the `META-INF/jboss-beans.xml` file together in a JAR file named `example-simple.beans` in the `dist` directory. Note that the `.beans` extension is important if you need to run the application inside the JBoss AS. For more on Microcontainer application packaging, please refer to packaging.

In the next two sections, let's discuss how to run the `.beans` application both as a standalone Java SE application and as a service in the JBoss AS.

## Run the Application in Standalone Mode

The `run` task in the `build.xml` script loads the Microcontainer with the `example-simple.beans` JAR file on the classpath. The Microcontainer then scans for the `META-INF/jboss-beans.xml` file on the classpath and creates / manages objects according to the configuration.

    <target name="run" depends="dist">

       <java
    classname="org.jboss.kernel.plugins.bootstrap.standalone.StandaloneBootstrap"
    fork="true">
         <classpath refid="run.classpath"/>
      </java>
    </target>

    <path id="run.classpath">
       <fileset dir="dist">
           <include name="*.beans"/>
       </fileset>
       <fileset dir="${jboss.micro.home}/lib">
           <include name="*.jar"/>
       </fileset>
    </path>

The `StandaloneBootstrap` class is a utility class to load the Microcontainer as a standalone application in the Java SE environment. We will discuss more about it in standalone. As expected, the Microcontainer instantiates the `SimpleBean` POJO and the message is successfully printed from the constructor.

    run:
         [java] SimpleBean() constructor

    BUILD SUCCESSFUL
    Total time: 12 seconds

## Run the Application as a JBoss Service

To run the application as a shared JBoss service, you first need to change the `jboss.home` property in the `examples/build.properties` file and point it to your local JBoss installation. Then, you run `ant deploy` to copy the `example-simple.beans` file into the `deploy` directory of your JBoss instance. If the server is already running, the `.beans` file is automatically deployed and you will see the `SimpleBean` constructor message printed out in the console window. If the server is not running, you will see the message confirming the creation of the `Simple` object when you start up the server the next time.

To remove the `.beans` package from the server, just run `ant undeploy` in the example directory.

## Other Bundled Examples

Besides the `simple` example, the Microcontainer distribution bundles other examples to illustrate various aspects of the framework. We will cover those examples in more detail in basics. Each example is in a subfolder under the `examples/` directory with the following structure:

    readme.txt - a short description of the example including the expected output
    build.xml - the ant build script
    src/resources/META-INF/jboss-beans.xml - the MicroContainer configuration for the example
    src/main - the java source for the example

Here is a list of those examples.

- simple: a simple example to make sure you have everything working

- constructor: simple constructor configuration

- factory: construction using factories

- properties: property configuration

- injection: referencing other beans

- collections: creating collections

- lifecycle: the create/start/stop/destroy lifecycle

- locator: implementing the locator pattern with the Microcontainer

The JBoss Microcontainer uses any logging mechanism supported by org.jboss.logging. e.g. if you want to use log4j, add log4j.jar and a directory containing either a log4j.properties or log4j.xml to the classpath. The default distribution uses the "null" logging implementation, which is why the examples all use System.out.println().

This just skims the surface of the MicroContainer, showing the most common usecases. Other more complicated examples can be found in the tests (available from cvs).