## JBoss + Spring example:

The source shows how simple it is to inject Spring managed fine grained POJO beans into EJB components.

First make sure that you have Spring deployer in your JBOSS_HOME/server/default/deploy directory. That should be jboss-spring-jdk5.deployer directory with three jars and META-INF directory inside (jboss-spring-jdk5.jar, spring-beans.jar, spring-core.jar and META-INF/jboss-service.xml).

Since our code is dependant on Spring library, we must make sure that all of the needed classes are in our classpath. Copy spring-aop.jar, spring-context.jar, spring-web.jar and spring-webmvc.jar into JBOSS_HOME/server/default/lib directory.

Fix ejb3-interceptors-aop.xml in your JBOSS_HOME/server/default/deploy directory:

       <interceptor class="org.jboss.ejb3.AllowedOperationsInterceptor" scope="PER_VM"/>

       <interceptor class="org.jboss.spring.interceptor.SpringInjectionInterceptor" scope="PER_VM"/>

       <typedef name="ejb3intercepted" expr="class(@javax.ejb.Interceptors) OR class(@javax.ejb.Interceptor) OR has(* *->@javax.ejb.AroundInvoke(..))"/>

       <domain name="Stateless Bean">

          <bind pointcut="execution(*->new(..))">

             <interceptor-ref name="org.jboss.spring.interceptor.SpringInjectionInterceptor"/>

          </bind>

The added code is bold.

Looking at the source code you will see that our RandomizerBean class is declared as @Stateless session bean. The pointcut above will pick up our stateless session bean at instantiation time and perform Spring injection interception.

The second example from source code shows how we can do Spring bean injection in a non AOP way. Look at the HoroscopeBean class which has a @CallbackListener annotation with the value SpringCallbackListener.class. Read the EJB3Trail application’s documentation (http://trailblazer.demo.jboss.com/EJB3Trail/serviceobjects/lifecycle/index.html) on how callback listeners are used.

Now you are all set to go. Unzip example’s archive into APP_HOME. Run Ant’s build script (APP_HOME/build.bat in Windows or APP_HOME/build.sh in Linux/Unix). Run default JBoss server (JBOSS_HOME/bin run). Drop JBoss-Spring.ear from APP_HOME/build/jar into JBOSS_HOME/server/default/deploy. The Spring injection should take place the moment you will click on the application’s web page (http://localhost:8080/jboss-spring).

Our Spring deployer watches for the Spring components which should have the following suffixes: .spring for archive or –spring.xml for the beans descriptor. Spring beans descriptor inside archive is expected to be in META-INF directory by the name jboss-spring.xml.

By default our bean factory’s JNDI name equals Spring components name: <name>.spring or <name>-spring.xml. It can be overridden inside beans descriptor description tag. You can also set parent bean factory JNDI name:

    <beans>

        <description>BeanFactory=(<name>) ParentBeanFactory=(<parent name>)</description>

There are different ways to deploy your Spring beans. One with .ear file we just saw. But you can also deploy only your Spring beans and let them be used by different applications. Instead of droping the whole .ear, you can just deploy .spring archive file or only beans descriptor. In descriptor’s case, its name must end with –spring.xml and, as expected, all the classes must already be made available on the classpath to the descriptor.

Let’s look this on the example. Just drop spring-pojo.spring file from APP_HOME/build/jar into JBOSS_HOME/server/default/deploy . As before, bean factory with JNDI name ‘spring-pojo’ gets deployed. You can now include this bean factory by setting its name as the parent bean factory name in your new beans factory xml descriptor:

    <beans>

        <description>ParentBeanFactory=(spring-pojo)</description>

When undeploying single files, be careful of the order of undeploying – take notice of bean factory hierarchy.

When shutting down application server, the Spring deployer will take care of correct undeployment order.