简体   繁体   English

EJB注入不适用于不同的类

[英]EJB injection not working in different classes

I have tried to inject an Entity EJB(Facade and entity from MySQL database) but it doesn't work depending on the class it is in. I use a Rest webService and it work in the webService class. 我尝试注入一个Entity EJB(MySQL数据库中的Facade和实体),但是根据它所在的类,它不起作用。我使用Rest webService,它在webService类中工作。 I use a multi layered architecture and I don't want my EJB to be injected in this webService. 我使用多层体系结构,并且我不希望将EJB注入此webService中。

So, this is the @Stateless facade : 因此,这是@Stateless门面:

public class ActivityEntityFacade extends AbstractFacade<ActivityEntity> {
@PersistenceContext(unitName = "persistance")
private EntityManager em;

@Override
protected EntityManager getEntityManager() {
    return em;
}

public ActivityEntityFacade() {
    super(ActivityEntity.class);
}

} }

this is the rest webService method working with EJB : 这是使用EJB的其余webService方法:

@EJB
private ActivityEntityFacade activityfacade;

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{date}")
public List<ActivityEntity> activities(@PathParam("date") String date){
    List<ActivityEntity> activityList = activityfacade.findAll();
    return activityList;
}

And this is my activityManager which is supposed to work with the EJB and supposed to return this List to another layer : 这是我的activityManager,应该与EJB一起使用,并且应该将此List返回到另一层:

public class ActivityManager implements Serializable, IActivity {

@EJB
private ActivityEntityFacade activityfacade;

public ActivityManager(){
}

public List<ActivityEntity> selectAll(){
    return  activityfacade.findAll();
}

} }

This snipet return nullPointerException on the activityEntityFacade. 此摘要在activityEntityFacade上返回nullPointerException。 Have you any idea why it doesn't work in this case ? 您知道为什么在这种情况下不起作用吗?

EDIT: the instance of activityManager is created in the previous layer: 编辑:activityManager的实例在上一层中创建:

 class ServActivityConsult implements IActivable{

@Override
public List<ActivityEntity> activityList() {
    IActivity managerActivity = new ActivityManager();
    return managerActivity.selectAll();
}

public ServActivityConsult(){
}

} }

But the error come from return activityfacade.findAll IN ActivityManager in our case. 但是在我们的例子中,错误来自于在ActivityManager中返回activityfacade.findAll。

You're creating an ActivityManager yourself, using new ActivityManager() . 您将使用new ActivityManager()自己创建一个ActivityManager。 So the basic rules of Java apply: the constructor is called, and since it doesn't affect any value to activityfacade , this field is set to null. 因此适用Java的基本规则:调用了构造函数,并且由于它不影响activityfacade任何值,因此将该字段设置为null。

For dependency injection to happen, you can't create the objects yourself, but get them from the container. 为了进行依赖注入,您不能自己创建对象,而要从容器中获取它们。 So your class should look like 所以你的课应该看起来像

public class ServActivityConsult implements IActivable{

    @Inject
    private ActivityManager activityManager;

    @Override
    public List<ActivityEntity> activityList() {
        return activityManager.selectAll();
    }
}

And this class itself should be injected in a Jersey controller or an EJB. 并且此类本身应注入到Jersey控制器或EJB中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM