# Dynamic AOP

## Hot Deploment

With JBoss AOP you can change advice and interceptor bindings at runtime. You can unregister existing bindings, and hot deploy new bindings if the given joinpoints have been instrumented.

Hot-deploying within the JBoss application server is as easy as putting (or removing) a `*-aop.xml` file or `.aop` jar file within the deploy/ directory.

There is also a runtime API for adding advice bindings at runtime. Getting an instance of `org.jboss.aop.AspectManager.instance()`, you can add your binding.

          org.jboss.aop.advice.AdviceBinding binding = new AdviceBinding("execution(POJO->new(..))", null);
          binding.addInterceptor(SimpleInterceptor.class);
          AspectManager.instance().addBinding(binding);

First, you allocated an `AdviceBinding` passing in a pointcut expression. Then you add the interceptor via its class and then add the binding through the AspectManager. When the binding is added the AspectManager will iterate through ever loaded class to see if the pointcut expression matches any of the joinpoints within those classes.

## Per Instance AOP

Any class that is instrumented by JBoss AOP, is forced to implement the `org.jboss.aop.Advised interface.`

    public interface InstanceAdvised
    {
       public InstanceAdvisor _getInstanceAdvisor();
       public void _setInstanceAdvisor(InstanceAdvisor newAdvisor);
    }

    public interface Advised extends InstanceAdvised
    {
       public Advisor _getAdvisor();
    }

The InstanceAdvisor is the interesting interface here. InstanceAdvisor allows you to insert Interceptors at the beginning or the end of the class's advice chain.

    public interface InstanceAdvisor
    {
       public void insertInterceptor(Interceptor interceptor);
       public void removeInterceptor(String name);
       public void appendInterceptor(Interceptor interceptor);

       public void insertInterceptorStack(String stackName);
       public void removeInterceptorStack(String name);
       public void appendInterceptorStack(String stackName);

       public SimpleMetaData getMetaData();

    }

So, there are three advice chains that get executed consecutively in the same java call stack. Those interceptors that are added with the `insertInterceptor()` method for the given object instance are executed first. Next, those advices/interceptors that were bound using regular `bind`s . Finally, those interceptors added with the `appendInterceptor()` method to the object instance are executed. You can also reference `stack`s and insert/append full stacks into the pre/post chains.

Besides interceptors, you can also append untyped metadata to the object instance via the getMetaData() method.

## Preparation

Dynamic AOP cannot be used unless the particular joinpoint has been instrumented. You can force intrumentation with the `prepare` functionality