在 Java SE 中使用 CDI 和 JPA 的最简单方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20935977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the easiest way to have CDI and JPA in Java SE?
提问by Dariusz Mydlarz
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). I don't know how to add to Weld possibilty to inject Entity Manager. I know that I can obtain Entity Manager with
我已经在阅读有关 Weld 的内容(但它看起来只像 CDI)。我不知道如何添加焊接可能性以注入实体管理器。我知道我可以获得实体管理器
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!
如果有任何关于此的教程就太好了。无论如何,感谢您的帮助!
采纳答案by Petr Mensik
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.
首先,EJB 是 Java EE 的一部分,因此您不能在 Java SE 中使用它们。但是,CDI 可以在 Java SE 环境中使用,我的示例将向您展示如何将它与 Weld 一起使用,但还有其他实现 - 请注意,CDI 只是规范,而 Weld 是该规范的实现之一。
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
为了使用 Weld,您需要weld-se-x.x.x-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
然后你需要在你的主方法中启动容器,所以做这样的事情
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 Producers 来制作你的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 的 Weld 文档。
回答by leftbit
Peter's answer seems to work, but the Maven dependencies are outdated (see this bug)
彼得的答案似乎有效,但 Maven 依赖项已过时(请参阅此错误)
<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>