@EJB inject from different jar

僤鯓⒐⒋嵵緔 提交于 2020-01-23 08:07:49

问题


I'm trying to inject a bean located in a different jar file then the bean i'm trying to inject it into. Both beans are just basic @Stateless beans with local and remote interfaces. If i use the normal injection

@EJB
IBean injectedBean;

or

@EJB
IBeanLocal injectedBean;

i get a NullPointerException when deploying the application.

If i use:

@EJB(mappedName="Bean")
IBean injectedBean;

or

@EJB(mappedName="Bean")
IBeanLocal injectedBean;

everything works and JBoss throws no deployment errors.

I might mention i use JBoss 5.

The bean class i'm injecting is declared as:

@Remote
public interface IBean

@Local
public interface IBeanLocal extends IBean

@Stateless(name = "Bean")
public class Bean implements IBean, IBeanLocal

My problem is that as specified in the documentation the mappedName property is vendor-specific. Is there any other way i could get this to work?

SOLVED:

I managed to solve the problem.

The problem was that i tried to deploy both jars individually which meant that each would get it's own ClassLoader in JBoss so that they couldn't find each other and would return a NullPointerException when trying to inject the bean.

The sollution was to add the jars to an ear and add an META-INF containing an application.xml looking like this:

<application xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
         http://java.sun.com/xml/ns/j2ee/application_1_4.xsd"
         version="1.4">

  <display-name>Simple example of application</display-name>

  <module>
    <ejb>ejb1.jar</ejb>
  </module>
  <module>
    <ejb>ejb2.jar</ejb>
  </module>
</application>

I also had to change some JNDI lookups i made to match the new structure by adding the ear name before the classes: "ear-name/bean"

After this i just added the jars to the ear and everything deployed nicely.


回答1:


You need to declare the local interface in order to have JBoss find the bean based on the interface only (assuming you're using EJB 3.0):

@Stateless(name = "Bean")
@Local ( IBeanLocal.class  )
@Remote ( IBean.class )
public class Bean implements IBean, IBeanLocal { ... }

Edit: IBean is a remote interface (see comment).




回答2:


Try injecting your bean with @EJB(beanName = "Bean")

Not sure if it'll work, but we had a similar issue and it was caused by the lack of the beanName attribute.



来源:https://stackoverflow.com/questions/8181146/ejb-inject-from-different-jar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!