# Annotations support

We learned how to configure our POJOs with Microcontainer's XML elements. In today's development most of the configuration is done with annotations. The Microcontainer is no exception.

Almost all of the features available in XML are also present in the Microcontainer's annotations support, except for the classloading configuration, which for obvious reasons doesn't have its equivalent (the class needs to be loaded before we can check for annotations, right). Let's now look at the annotations supported and some of its caveats.

Like in most other configurations XML and predetermined bean metadata override annotations. This is the current list of supported annotations and their XML equivalents. Later we will show how you can extend support for your own annotations directly into Microcontainer's configuration.

- @Aliases - <alias>

           /**
            * The aliases.
            * Equivalent to deployment's alias element.
            *
            * @author <a href="mailto:ales.justin@jboss.com">Ales Justin</a>
            */
            @Retention(RetentionPolicy.RUNTIME)
            @Target({ElementType.TYPE})
            public @interface Aliases
            {
               /**
                * Get aliases array.
                *
                * @return the aliases
                */
               String[] value();

               /**
                * Do system property replace.
                *
                * @return true to replace system property, false otherwise
                */
               boolean replace() default true;
            }

  Example:

            @Aliases({"MyAlias", "YetAnotherSimpleBean"})
            public class AliasSimpleBeanImpl extends SimpleBeanImpl

- @ArrayValue - <array>

  @CollectionValue - <collection>

  @ListValue - <list>

  @SetValue - <set>

            /**
             * Injecting array value.
             *
             * @author <a href="mailto:ales.justin@jboss.com">Ales Justin</a>
             */
            @Retention(RetentionPolicy.RUNTIME)
            @Target({ElementType.METHOD, ElementType.PARAMETER})
            public @interface ArrayValue
            {
               /**
                * Set the array class.
                *
                * @return array class name
                */
               String clazz() default "";

               /**
                * Array's element class.
                *
                * @return element class name
                */
               String elementClass() default "";

               /**
                * Get the values.
                *
                * @return the values
                */
               Value[] value();
            }

  Example:

            @ArrayValue(
               value = {
                  @Value(string = @StringValue("string1")),
                  @Value(string = @StringValue("string2")),
                  @Value(string = @StringValue("string2")),
                  @Value(string = @StringValue("string1"))
               },
               elementClass = "java.lang.String",
               clazz = "[Ljava.lang.Comparable;"
            )
            public void setArray(Object[] array)
            {
               super.setArray(array);
            }

            @CollectionValue(
               value = {
                  @Value(string = @StringValue("string1")),
                  @Value(string = @StringValue("string2")),
                  @Value(string = @StringValue("string2")),
                  @Value(string = @StringValue("string1"))
               },
               elementClass = "java.lang.String",
               clazz = "java.util.ArrayList"
            )
            public void setCollection(Collection collection)
            {
               super.setCollection(collection);
            }

- @Constructor - <constructor>

            /**
             * Mark the constructor used to instantiate bean.
             *
             * @author <a href="mailto:ales.justin@jboss.com">Ales Justin</a>
             */
            @Retention(RetentionPolicy.RUNTIME)
            @Target({ElementType.CONSTRUCTOR})
            public @interface Constructor
            {
            }

  Example:

            @Constructor
            public TestConstructorBean(@Inject(bean = "testBean") TestBean test)
            {
               ...
            }

- @Create - <create>

  @Start - <start>

  @Stop - <stop>

  @Destroy - <destroy>

            /**
             * Mark lifecycle create method.
             *
             * @author <a href="mailto:ales.justin@jboss.com">Ales Justin</a>
             */
            @Retention(RetentionPolicy.RUNTIME)
            @Target({ElementType.METHOD})
            public @interface Create
            {
               /**
                * Is this lifecycle callback ignored.
                *
                * @return ignored
                */
               boolean ignored() default false;
            }

  Example:

            @Create
            public void notCreate(
                       @Inject(bean = "Name1") SimpleBeanWithLifecycle bean)
            {
               super.notCreate(bean);
            }

            @Start
            public void notStart(@Inject(bean = "Name2")SimpleBeanWithLifecycle bean)
            {
               super.notStart(bean);
            }

            @Stop
            public void notStop(@Inject(bean = "Name3")SimpleBeanWithLifecycle bean)
            {
               super.notStop(bean);
            }

            @Destroy
            public void notDestroy(
                       @Inject(bean = "Name4")SimpleBeanWithLifecycle bean)
            {
               super.notDestroy(bean);
            }

- @Demands - <demands>

            /**
             * The demands.
             *
             * @author <a href="mailto:ales.justin@jboss.com">Ales Justin</a>
             */
            @Retention(RetentionPolicy.RUNTIME)
            @Target({ElementType.TYPE})
            public @interface Demands
            {
               /**
                * Get demands.
                *
                * @return the demands
                */
               Demand[] value();
            }

  Example:

            @Demands({@Demand("WhatIWant")})
            public class DemandSimpleBeanImpl extends SimpleBeanImpl

- @Depends - <depends>

            /**
             * The depends values.
             *
             * @author <a href="mailto:ales.justin@jboss.com">Ales Justin</a>
             */
            @Retention(RetentionPolicy.RUNTIME)
            @Target({ElementType.TYPE})
            public @interface Depends
            {
               /**
                * Get the depends values.
                *
                * @return the depends
                */
               String[] value();
            }

  Example:

            @Depends({"Name1"})
            public class DependSimpleBeanWithLifecycle extends SimpleBeanWithLifecycle

- @ExternalInstalls - <install bean="..." method="..."/>

  @ExternalUninstalls - <uninstall bean="..." method="..."/>

            /**
             * Array of external installs.
             *
             * @author <a href="mailto:ales.justin@jboss.com">Ales Justin</a>
             */
            @Retention(RetentionPolicy.RUNTIME)
            @Target({ElementType.TYPE})
            public @interface ExternalUninstalls
            {
               /**
                * Get external installs.
                *
                * @return the external installs
                */
               ExternalInstall[] value();
            }

  Example:

            @ExternalInstalls({
               @ExternalInstall(bean = "Name1", method = "addSimpleBean",
                  parameters = {@Value(thisValue = @ThisValue)})})
            @ExternalUninstalls({
               @ExternalInstall(bean = "Name1", method = "removeSimpleBean",
                  parameters = {@Value(thisValue = @ThisValue)})})
            public class ExternalInstallSimpleBeanImpl implements Serializable, SimpleBean

- @Factory - <constructor><factory ...>

            /**
             * Define constructor factory.
             *
             * @author <a href="mailto:ales.justin@jboss.com">Ales Justin</a>
             */
            @Retention(RetentionPolicy.RUNTIME)
            @Target({ElementType.TYPE})
            public @interface Factory
            {
               /**
                * Get the factory.
                *
                * @return the factory value
                */
               Value factory() default @Value();

               /**
                * Get the factory class.
                *
                * @return the factory class
                */
               String factoryClass() default "";

               /**
                * Get the factory method.
                *
                * @return the factory method
                */
               String factoryMethod();

               /**
                * Get parameters.
                *
                * @return the parameters
                */
               Value[] parameters() default {};
            }

  Example:

            @Factory(
                  factory = @Value(javabean = @JavaBeanValue(
                    "org.jboss.test.kernel.config.support.SimpleBeanFactory")),
                  factoryMethod = "createSimpleBean",
                  parameters = {@Value(string = @StringValue("Factory Value"))}
            )
            public class FromFactoryWithParamSimpleBean extends SimpleBean

- @FactoryMethod - <constructor><factory factoryMethod="..." ...>

            /**
             * Mark static method as factory method.
             *
             * @author <a href="mailto:ales.justin@jboss.com">Ales Justin</a>
             */
            @Retention(RetentionPolicy.RUNTIME)
            @Target({ElementType.METHOD})
            public @interface FactoryMethod
            {
            }

  Example:

            @FactoryMethod
            public static SimpleInject getInstance(@NullValue Object someNull)
            {
               return new SimpleInject();
            }

- @Inject - <inject>

            /**
             * Beans when injected by class type are by default changed to configured
             * state - if not yet configured.
             * You can change this behavior by setting state.
             *
             * @author <a href="mailto:ales.justin@jboss.com">Ales Justin</a>
             */
            @Retention(RetentionPolicy.RUNTIME)
            @Target({ElementType.METHOD, ElementType.PARAMETER})
            public @interface Inject
            {
               /**
                * Get bean.
                * Default is no bean.
                *
                * @return bean name
                */
               String bean() default "";

               /**
                * Get property.
                * Default is no property.
                *
                * @return property name
                */
               String property() default "";

               /**
                * Get when required.
                *
                * @return when required.
                */
               String whenRequired() default "";

               /**
                * Get dependent state.
                * Default is Installed.
                *
                * @return dependent state.
                */
               String dependentState() default "Installed";

               /**
                * Get injection type.
                * Default is by class.
                *
                * @return injection type
                */
               InjectType type() default InjectType.BY_CLASS;

               /**
                * Get injection option.
                * Default is Strict.
                *
                * @return injection option
                */
               InjectOption option() default InjectOption.STRICT;

               /**
                * Get from context injection.
                *
                * @return from context type
                */
               FromContext fromContext() default FromContext.NONE;

               /**
                * Is this @Inject valid.
                * Used with @Value.
                *
                * @return is this instance valid
                */
               boolean valid() default true;
            }

  Example:

            @Inject(bean = "Name1")
            public void setSimpleBean(SimpleBean bean)
            {
               super.setSimpleBean(bean);
            }

            @Start
            public void startMeUp(
              @Inject(bean = "lifecycleBean") TestBean bean,
                @ValueFactory(bean="valueBean",
                method = "getValue",
                parameter = "123")
              int value)
            {
               test = bean;
               intVF =- value;
            }

- @Install - <incallback>

  @Uninstall - <uncallback>

            /**
             * Install callback.
             *
             * @author <a href="mailto:ales.justin@jboss.com">Ales Justin</a>
             */
            @Retention(RetentionPolicy.RUNTIME)
            @Target({ElementType.METHOD})
            public @interface Install
            {
               /**
                * Get the cardinality.
                * Default is no cardinality.
                *
                * @return cardinality
                */
               String cardinality() default "";

               /**
                * Get when required.
                * Default is Configured.
                *
                * @return when required.
                */
               String whenRequired() default "Configured";

               /**
                * Get dependent state.
                * Default is Installed.
                *
                * @return dependent state.
                */
               String dependentState() default "Installed";
            }

  Example:

            @Install
            public void addDeployer(MyDeployer deployer)
            {
               if (deployers == null)
                  deployers = new HashSet<MyDeployer>();
               deployers.add(deployer);
            }

            @Uninstall
            public void removeDeployer(MyDeployer deployer)
            {
               deployers.remove(deployer);
            }

- @InstallMethod - <install>

  Sethod - <uninstall>

            /**
             * Internal installation method.
             *
             * @author <a href="mailto:ales.justin@jboss.com">Ales Justin</a>
             */
            @Retention(RetentionPolicy.RUNTIME)
            @Target({ElementType.METHOD})
            public @interface InstallMethod
            {
               /**
                * Get dependant state.
                *
                * @return the dependant state
                */
               String dependantState() default "";
            }

  Example:

            @InstallMethod
            public void install()
            {
               installed = true;
            }

            @UninstallMethod
            public void uninstall()
            {
               installed = false;
            }

- @JavaBeanValue - <javabean>

            /**
             * Java bean value.
             *
             * @author <a href="mailto:ales.justin@jboss.com">Ales Justin</a>
             */
            @Retention(RetentionPolicy.RUNTIME)
            @Target({ElementType.METHOD, ElementType.PARAMETER})
            public @interface JavaBeanValue
            {
               /**
                * Get java bean class name.
                * Must have default constructor.
                *
                * @return the class name
                */
               String value() default "";
            }

  Example:

            @Factory(
                  factory = @Value(javabean = @JavaBeanValue(
                     "org.jboss.test.kernel.config.support.SimpleBeanFactory")),
                  factoryMethod = "createSimpleBean"
            )

- @MapValue - <map>

            /**
             * Map value injection.
             *
             * @author <a href="mailto:ales.justin@jboss.com">Ales Justin</a>
             */
            @Retention(RetentionPolicy.RUNTIME)
            @Target({ElementType.METHOD, ElementType.PARAMETER})
            public @interface MapValue
            {
               /**
                * Get the map class name.
                *
                * @return the map class name
                */
               String clazz() default "";

               /**
                * Get the key class name.
                *
                * @return the key class name
                */
               String keyClass() default "";

               /**
                * Get the value class name.
                *
                * @return the value class name
                */
               String valueClass() default "";

               /**
                * Get the entries.
                *
                * @return the entries
                */
               EntryValue[] value();
            }

  Example:

            @Constructor
            public AnnotatedLDAPFactory(
                  @MapValue(
                        keyClass = "java.lang.String",
                        valueClass = "java.lang.String",
                        value = {
                           @EntryValue(
                              key = @Value(string = @StringValue("foo.bar.key")),
                              value = @Value(string = @StringValue("QWERT"))
                           ),
                           @EntryValue(
                              key = @Value(string = @StringValue("xyz.key")),
                              value = @Value(string = @StringValue(" QWERT "))
                           )
                        }
                  )
                  Map<String, String> map
            )
            {
               super(map);
            }

- @NullValue - <null>

            /**
             * Null value.
             *
             * @author <a href="mailto:ales.justin@jboss.com">Ales Justin</a>
             */
            @Retention(RetentionPolicy.RUNTIME)
            @Target({ElementType.METHOD, ElementType.PARAMETER})
            public @interface NullValue
            {
               /**
                * Is valid.
                * Used in @Parameter and @value to define
                * unused value.
                *
                * @return is value valid
                */
               boolean valid() default true;
            }

  Example:

            @UninstallMethod
            public void withUninstall(
               @ThisValue SimpleInject me,
               @NullValue Object plainNull)
            {
               System.out.println(me == this);
               System.out.println("plainNull = " + plainNull);
            }

- @StringValue - <value>somestring</value>

            /**
             * String value.
             *
             * @author <a href="mailto:ales.justin@jboss.com">Ales Justin</a>
             */
            @Retention(RetentionPolicy.RUNTIME)
            @Target({ElementType.METHOD, ElementType.PARAMETER})
            public @interface StringValue
            {
               /**
                * Get the value.
                *
                * @return the value
                */
               String value();

               /**
                * Get type.
                *
                * @return the type
                */
               String type() default "";

               /**
                * Do replace with system properties.
                *
                * @return true for replace with system properties, false otherwise
                */
               boolean replace() default true;
            }

  Example:

            @Constructor
            public ParamIntConstructorAnnBean(@StringValue("7") Integer string)
            {
               super(string);
            }

- @Supplys - <supply>

            /**
             * The supplys.
             *
             * @author <a href="mailto:ales.justin@jboss.com">Ales Justin</a>
             */
            @Retention(RetentionPolicy.RUNTIME)
            @Target({ElementType.TYPE})
            public @interface Supplys
            {
               /**
                * Get supply values.
                *
                * @return the supplys
                */
               String[] value();
            }

  Example:

            @Supplys({"WhatIWant"})
            public class SupplyPlainDependecySimpleBeanImpl
               extends PlainDependecySimpleBeanImpl

- @ThisValue - <this/>

            /**
             * This value.
             * Get the underlying target.
             *
             * @author <a href="mailto:ales.justin@jboss.com">Ales Justin</a>
             */
            @Retention(RetentionPolicy.RUNTIME)
            @Target({ElementType.METHOD, ElementType.PARAMETER})
            public @interface ThisValue
            {
               /**
                * Is instance valid.
                *
                * @see @org.jboss.beans.metadata.api.annotations.Value
            * @see @org.jboss.beans.metadata.api.annotations.Parameter
            * @return true for valid
                */
               boolean valid() default true;
            }

  Example:

            @InstallMethod
            public void whenInstalled(
               @ThisValue SimpleInject me,
      		 @NullValue Object plainNull)
            {
               System.out.println(me == this);
               System.out.println("plainNull = " + plainNull);
            }

- @ValueFactory - <value-factory>

            /**
             * The value factory.
             *
             * @author <a href="mailto:ales.justin@jboss.com">Ales Justin</a>
             */
            @Retention(RetentionPolicy.RUNTIME)
            @Target({ElementType.METHOD, ElementType.PARAMETER})
            public @interface ValueFactory
            {
               /**
                * Get the bean.
                *
                * @return the bean
                */
               String bean();

               /**
                * Get the method.
                *
                * @return the method
                */
               String method();

               /**
                * Get single parameter.
                *
                * @return the single parameter
                */
               String parameter() default "";

               /**
                * Get parameters.
                *
                * @return the parameters
                */
               Parameter[] parameters() default {};

               /**
                * Get default value.
                *
                * @return the default value
                */
               String defaultValue() default "";

               /**
                * Get dependant state.
                *
                * @return the dependant state
                */
               String dependantState() default "Installed";

               /**
                * Get when required state.
                *
                * @return the when required state
                */
               String whenRequiredState() default "Configured";
            }

  Example:

            @ValueFactory(bean = "ldap",
               method = "getValue", parameter = "foo.bar.key")
            public void setValue(String value)
            {
               super.setValue(value);
            }

TODO - extending annotations support.