# Starting Examples

## A Simple IoC 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 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.

## 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` target compiles the Java source files into class files under the `build` directory. Then, the `dist` target packages the class files and the `META-INF/jboss-beans.xml` file together in a JAR file named `example-simple.jar` in the `dist` directory.

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

This just skims the surface of the Microcontainer, showing the most common usecases. Other more complicated examples can be found in the tests (available in the source code repository).