在Java SE中使用CDI和JPA的最简单方法是什么? [英] What is the easiest way to have CDI and JPA in Java SE?

查看:98
本文介绍了在Java SE中使用CDI和JPA的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拥有Java SE

  @Stateless 
公共类CarDAO {
@注入
私有EntityManager em;

public Car findById(Long id){
return em.find(Car.class,id);
}
}

@Singleton
公共类申请{
@Inject
私人CarDAO carDAO;

public void run(){
Car car = carDAO.findById(44);
System.out.println(car);
}
}

公共类EntryPoint {
public static void main(String [] args){
Application application = // missing code
application.run();
}
}

我需要做些什么来实现这一目标?我正在使用postgres数据库,并在我的项目中使用maven。



我已经在阅读有关Weld的内容(但它看起来只有CDI)。我不知道如何添加到Weld
possibilty来注入实体管理器。我知道我可以获得实体经理

  EntityManagerFactory emf = Persistence.createEntityManagerFactory(mgr); 
EntityManager em = emf.createEntityManager();

但它不如注射方便。



<如果有任何关于此的教程,那将是很棒的。无论如何,感谢您的帮助!

解决方案

首先,EJB是Java EE的一部分,因此您不能在Java中使用它们SE。但是,CDI可以在Java SE环境中使用,我的示例将向您展示如何将它与Weld一起使用,但也有其他实现 - 请注意,CDI只是规范,而Weld是该规范的实现之一。



要使用Weld,您需要输入 在类路径上使用weld-se-xxx-Final.jar 或在Maven中指定其依赖关系,如

 < dependency> 
< groupId> org.jboss.weld.se< / groupId>
< artifactId> weld-se< / artifactId>
< version><! - 请参阅https://mvnrepository.com/artifact/org.jboss.weld.se/weld-se了解最新版本 - >< / version>
< / dependency>

然后你需要在main方法中启动容器,所以做这样的事情

  public static void main(String [] args)throws IOException {
Weld weld = new Weld();
WeldContainer container = weld.initialize();
应用程序应用程序= container.instance()。select(Application.class).get();
application.run();
weld.shutdown();
}

这应该让你入门,然后你就可以使用CDI Producers来制作你的 EntityManager injectable

  @Produces 
@RequestScoped
public EntityManager createEntityManager(){
return Persistence.createEntityManagerFactory(mgr)。createEntityManager();
}

public void closeEM(@Disposes EntityManager manager){
manager.close();
}

另见关于在Java SE中使用CDI的焊接文档


I would like to have in Java SE

@Stateless
public class CarDAO {
    @Inject
    private EntityManager em;

    public Car findById(Long id) {
        return em.find(Car.class, id);
    }
}

@Singleton
public class Application {
    @Inject
    private CarDAO carDAO;

    public void run() {
        Car car = carDAO.findById(44);
        System.out.println(car);
    }
}

public class EntryPoint {
    public static void main(String[] args) {
        Application application = // missing code
        application.run();
    }
}

What I have to do to achieve that? I'm using postgres database, and maven in my project.

I was already reading something about Weld (but it looks only like CDI). I don't know how to add to Weld possibilty to inject Entity Manager. I know that I can obtain Entity Manager with

EntityManagerFactory emf = Persistence.createEntityManagerFactory("mgr");
EntityManager em = emf.createEntityManager();

but it's not so convenient as injecting.

It would be great if there is any tutorial about that. Anyway, thanks for any help!

解决方案

First of all, EJBs are part of Java EE, therefore you cannot use them in Java SE. However, CDI can be used in Java SE environment, my example will show you how to use it with Weld but there are also other implementations - note that CDI is just specification and Weld is one of the implementations of that specification.

In order to use Weld, you need to either put weld-se-x.x.x-Final.jar on the classpath or specify its dependency in Maven like

<dependency>
    <groupId>org.jboss.weld.se</groupId>
    <artifactId>weld-se</artifactId>
    <version><!-- See https://mvnrepository.com/artifact/org.jboss.weld.se/weld-se for current version --></version>
</dependency>

Then you need to startup the container in your main method, so do something like this

public static void main(String[] args) throws IOException {
    Weld weld = new Weld();
    WeldContainer container = weld.initialize();
    Application application = container.instance().select(Application.class).get();
    application.run();
    weld.shutdown();
}

This should get you started, then you can use CDI Producers to make your EntityManager injectable

@Produces
@RequestScoped
public EntityManager createEntityManager() {
   return Persistence.createEntityManagerFactory("mgr").createEntityManager();
}

public void closeEM(@Disposes EntityManager manager) {
   manager.close();
}

See also Weld documentation on using CDI in Java SE.

这篇关于在Java SE中使用CDI和JPA的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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