简体   繁体   English

在Java SE中拥有CDI和JPA的最简单方法是什么?

[英]What is the easiest way to have CDI and JPA in Java SE?

I would like to have in Java SE 我想在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. 我在我的项目中使用postgres数据库和maven。

I was already reading something about Weld (but it looks only like CDI). 我已经在阅读有关Weld的内容(但看起来只像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. 首先,EJB是Java EE的一部分,因此您不能在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. 但是,CDI可以在Java SE环境中使用,我的示例将向您展示如何将其与Weld一起使用,但还有其他实现-请注意,CDI只是规范,而Weld是该规范的实现之一。

In order to use Weld, you need to either put weld-se-xxx-Final.jar on the classpath or specify its dependency in Maven like 为了使用Weld,您需要将weld-se-xxx-Final.jar放在类路径上,或在Maven中指定其依赖项,例如

<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 然后,您需要在您的main方法中启动容器,因此请执行以下操作

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 这应该使您入门,然后可以使用CDI生产者将EntityManager注入

@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的焊接文档

Peter's answer seems to work, but the Maven dependencies are outdated (see this bug ) Peter的答案似乎可行,但Maven依赖项已过时(请参见此bug

<dependency>
    <groupId>org.jboss.weld.se</groupId>
    <artifactId>weld-se-core</artifactId>
    <version>2.3.1.Final</version>
</dependency>
<dependency>
    <groupId>org.jboss.weld</groupId>
    <artifactId>weld-core</artifactId>
    <version>2.3.1.Final</version>
</dependency>
<dependency>
    <groupId>org.jboss</groupId>
    <artifactId>jandex</artifactId>
    <version>1.2.2.Final</version>
</dependency>

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

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