JBoss.org Community Documentation

13.1. Deployment actions

In Part I - Getting Started we covered how to add behaviour to beans during the deployment and undeployment process using AOP lifecycle callbacks. This gave us a powerful way to apply common logic to a number of beans, identified using pointcut expressions, at various points in their lifecycles. However setting up AOP lifecycle callbacks for occasions when an individual bean needs to call arbitrary methods before deployment or after undeployment can be time consuming. Fortunately JBoss Microcontainer provides an alternative way to do this using deployment/undeployment actions.

To specify a method within a bean that should be called after the bean reaches the START state you should use the @InstallMethod annotation or <install> element as follows:


@InstallMethod
public String doSomething() {
    ...
}

<bean name="SimpleBean" class="org.jboss.example.SimpleBean">
    <install method="doSomething"/>
</bean>

Parameters can also be passed in using nested <parameter> elements or by defining values with annotations.


@InstallMethod
public String doSomething(@StringValue("10") Integer integer, @StringValue("my string") String string) {
    ...
}

<bean name="SimpleBean" class="org.jboss.example.SimpleBean">
    <install method="doSomething">
        <parameter>10</parameter>
        <parameter>my string</parameter>
    </install>
</bean>

If necessary multiple install methods can be defined. They will be called in the order in which they appear as annotations or in the deployment descriptor.


@InstallMethod
public String doSomething() {
    ...
}

@InstallMethod
public String doSomethingElse() {
    ...
}

<bean name="SimpleBean" class="org.jboss.example.SimpleBean">
    <install method="doSomething"/>
    <install method="doSomethingElse"/>
</bean>

During undeployment uninstall methods can also be defined which will be called before the bean reaches the STOP state.


@UninstallMethod
public String doSomething() {
    ...
}

@UninstallMethod
public String doSomethingElse() {
    ...
}

<bean name="SimpleBean" class="org.jboss.example.SimpleBean">
    <uninstall method="doSomething"/>
    <uninstall method="doSomethingElse"/>
</bean>

If you want a bean to call a method on another bean during deployment or undeployment then you can use the @ExternalInstalls/@ExternalUninstalls annotations or the bean attribute of the <install>/<uninstall> elements. You can also define parameters to pass in if necessary.


@ExternalInstalls(@ExternalInstall(bean="OtherBean", method="doWork")
@ExternalUninstalls(@ExternalInstall(bean="OtherBean", method="doBye", parameters="{@Value(string=@StringValue("Goodbye")),@Value(string=@StringValue("Saturday"))}"))
public class MyBean {
    ...
}

<bean name="SimpleBean" class="org.jboss.example.SimpleBean">
    <install bean="OtherBean" method="doWork"/>
    <uninstall bean="OtherBean" method="doBye">
        <parameter name="message">Goodbye</parameter>
        <parameter name="day">Saturday</parameter>
    </uninstall>
</bean>

Note

Currently both the @ExternalInstalls and @ExternalUninstalls annotations take a list of @ExternalInstall annotations.

By default the other bean must have reached the INSTALLED state before its method will be called. This can be changed by using the dependentState/state attribute.


@ExternalInstalls(@ExternalInstall(bean="OtherBean", method="doWork", dependentState="Configured")
public class MyBean {
    ...
}

<bean name="SimpleBean" class="org.jboss.example.SimpleBean">
    <install bean="OtherBean" method="doWork" state="Configured"/>
</bean>