Dependency Injection in a Java 7 standalone application

假如想象 提交于 2019-12-05 01:40:58
LaurentG

There is not only one way to use dependency injection with Java.

(1) You could for instance use the standard CDI, where the reference implementation is Weld. There is documentation about using Weld in a Java SE environment, what is probably what you mean by standalone application.

You could alternatively also use Spring Framework, which also supports the common CDI annotation (e.g. @Inject). In this case, you will typically create a ClasspathXmlApplicationContext at the program startup and let Spring manages (create/destroy) all the beans you need.

(2) You current dependencies only imports the API of Java EE. Thus I'm not surprised if you get a NullPointerException at the execution. You need to add an implementation (like Weld) or use Spring.

(3) See links above.

Also take a look at Differences between Java EE 6 CDI Implementations to get reference about other Java CDI available implementations.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!