Enter EJB 3 in Spring Bean

I am trying to introduce EJB into the Spring service (3.1.2) (as in different WARs) Both are very simple (methods removed to simplify the example):

EJB:

@Remote public interface MyBean { } @Singleton public class MyBeanImpl implements MyBean{ } 

Services:

 @Service public class MyServiceImpl implements MyService{ } 

At first glance, the thing is very simple, but I tried:

 @EJB(lookup = "java:global/ejbApp/MyBeanImpl!com.my.MyBean") private MyBean myBean; 

and he did not work. Then I also tried:

 @EJB(mappedName = "java:global/ejbApp/MyBeanImpl!com.my.MyBean") private MyBean myBean; 

and

 @Resource(mappedName = "java:global/ejbApp/MyBeanImpl!com.my.MyBean") private MyBean myBean; 

but not one of them worked.

I managed to introduce EJB using:

 <jee:jndi-lookup id="myBean" jndi-name="java:global/ejbApp/MyBeanImpl!com.my.MyBean" /> 

in my Spring configuration and in the service:

 @Autowired private MyBean myBean; 

But I really don't like this solution. I would like my JNDI path to be in some annotation in order to be able to do, for example:

 @EJB(lookup = MyBean.JNDI_NAME) private MyBean myBean; 
+7
source share
2 answers

We found a pretty nice and easy solution. In the spring configuration file, you need to put:

 <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"> <property name="alwaysUseJndiLookup" value="true" /> </bean> 

And that allows spring to look for beans annotated with @Resource in JNDI. So now you can do:

 @Resource(mappedName = MyBean.JNDI_NAME) private MyBean myBean; 
+8
source

Do you want to get rid of XML or have the JNDI name in the annotation? If the first one, I have not tested it, but should work:

 @Configuration public class EjbCfg { @Bean public JndiObjectFactoryBean myBean() { JndiObjectFactoryBean factory = new JndiObjectFactoryBean(); factory.setJndiName(MyBean.JNDI_NAME); return factory; } } 

Now you can simply enter:

 @Autowired private MyBean myBean; 
0
source

All Articles