Posts Tagged Add new tag

Grails and EJB2.1 integration

If you are working on a Grails project and you want to use existing business logic from EJB2.1 inside your application, you can easily achive this using Spring.

The first step is to include the Beans you want to use, inside your conf/spring/resources.groovy file like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
beans = {
               ejbJndi(org.springframework.jndi.JndiTemplate){
                       environment = [
                               "java.naming.factory.initial" : "org.jnp.interfaces.NamingContextFactory",
                               "java.naming.provider.url" : "jnp://localhost:1099"]
               }

               businessBean(org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean){
                       jndiName = "EJBBusinessBean"
                       businessInterface = "com.my.ejb.EJBBusinessBean"
                       jndiTemplate = ref("ejbJndi")
               }

               personBean(org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean){
                       jndiName = "EJBPerson"
                       businessInterface = "com.my.ejb.EJBPerson"
                       jndiTemplate = ref("ejbJndi")
               }
}

Then you can use any of the defined Beans inside your services or controllers like this:

1
2
3
4
5
6
7
class MyService {
   def personBean //spring injects the bean automatically

   def myMethod(){
     personBean.list()
  }
}

That’s it!
Just remember to include the required JAR files (the EJB jars and the jars required to connect to the container, for instance, JBoss)

, , , , ,

1 Comment