@Inject在Java EE中给出NullPointer异常 [英] @Inject gives a NullPointer Exception in java EE

查看:124
本文介绍了@Inject在Java EE中给出NullPointer异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Web服务,该服务将提供从数据库通过休眠获取的一些结果。

I'm trying to create a webservice that gives some results taken through hibernate from the database.

@Path("/book")
public class BookService {

    @Inject
    private dbController db;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getBookTitle() {
        return "H2G2";
    }

    @GET
    @Path("/users")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getUsers(){

        List<UserEntity> users = db.getUsers();


        return Response.ok(users,MediaType.APPLICATION_JSON).build();
    }
}

每当我调用 http:// localhost / book / users 始终为空。

dbController是:

The dbController is:

public class dbController {

    @Inject
    private HibernateUtil util;

    public List<UserEntity> getUsers(){

        List<UserEntity> result = null;
        try{
            result = (List<UserEntity>) this.util.createQuery("select e from UserEntity e");
        }catch (Exception e){
            System.out.println(e.getMessage());
        }

        return result;
    }
}

而HibernateUtil是:

and the HibernateUtil is:

public class HibernateUtil {

    private static final EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("NewPersistenceUnit");
    private EntityManager entityManager = null;


    private void createEntityManager(){
        if (this.entityManager==null){
            this.entityManager = entityManagerFactory.createEntityManager(); // here is your persistence unit name
        }


    }

    private void closeConnection(){
        this.entityManager.close();
        this.entityManager = null;
    }

    public List createQuery(String query) throws Exception{
        this.createEntityManager();
        List result;

        try{
            result = this.entityManager.createQuery(query).getResultList();
        }catch (Exception e){
            throw new Exception(e.getMessage());
        }

        return result;
    }


}

我使用intellij,我在db.getUsers()处添加了一个断点,并通过添加新的dbController()来设置变量db。但是,Intellij给我错误类未加载:controller.dbController。

I'm using intellij and I added a break point at the db.getUsers() and set the variable db by adding new dbController(). However, the Intellij gives me the error "Class not loaded : controller.dbController".

休眠肯定可以工作...因此问题不存在。这是我第一次尝试使用依赖注入,但是我不确定自己在做什么错。

The hibernate works for sure...so the problem is not there. It's the first time I'm trying to use Dependency Injection, but I'm not sure what I'm doing wrong.

谢谢

推荐答案

您不能注入POJO,因为它必须是Bean。因此,使其成为一个bean需要注释,例如:

You cannot inject POJO it has to be a Bean. So making it a bean requires the annotations, for example:

@Stateful
@LocalBean
public class dbController {..} // Should be DbController, start with CAPS

@Stateful // or maybe @Stateless ?
@LocalBean
public class HibernateUtil {..}

然后一个Bean不允许使用静态最终值,因此需要进行如下更改:

Then when you have a Bean it is not allowed to use static final so change like this is needed:

private EntityManagerFactory entityManagerFactory =
            Persistence.createEntityManagerFactory("NewPersistenceUnit");

但实际上获取 EntityManager的最简单方法也是要注入它。像这样:

But actually the easiest way to get EntityManager is just to inject it also. Like:

@PersistenceContext// maybe also with unit name (unitName = "whatever_the_unit_name_is")
private EntityManager em;

这篇关于@Inject在Java EE中给出NullPointer异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆