如何将CDI @ApplicationScoped bean注入@RequestScoped JAX-RS bean? [英] How do I @Inject a CDI @ApplicationScoped bean into a @RequestScoped JAX-RS bean?
问题描述
我已将@ApplicationScoped
CDI批注添加到一个简单的bean:
I've added the @ApplicationScoped
CDI annotation to a simple bean:
@ApplicationScoped
public class History {
然后尝试将其@Inject
转换为JAX-RS(resteasy)bean:
And tried to then @Inject
this into a JAX-RS (resteasy) bean:
@RequestScoped
@Path("/history")
public class HistoryAPI {
@Inject
private History history;
但是历史记录仍然为空.我在WEB-INF中有一个beans.xml
文件.我已经尝试了很多关于此主题的变体,但是尽管应用服务器(Wildfly)承认它是从CDI开始的,但是我无法使注入工作.有什么想法我想念的吗?谢谢.
But history remains null. I've got a beans.xml
file in WEB-INF. I've tried a lot of variations on this theme, but while the app server (Wildfly) acknowledges it's starting with CDI I can't get the injection to work. Any ideas what I"m missing? Thanks.
ETA:我尝试了一个空的beans.xml
和几个变体,最新的是:
ETA: I've tried an empty beans.xml
and a couple of variants, the latest being:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:weld="http://jboss.org/schema/weld/beans"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://docs.jboss.org/cdi/beans_1_0.xsd
http://jboss.org/schema/weld/beans http://jboss.org/schema/weld/beans_1_1.xsd">
</beans>
推荐答案
奇怪...我认为您的代码应该可以工作...
strange... i think your code should work...
我在GitHub上创建了此问题的示例(已通过Wildly 8.2.0.Final和Glassfish 4.1进行了测试):
i have create a example for this question on GitHub (Tested with Wildly 8.2.0.Final and Glassfish 4.1): https://github.com/StefanHeimberg/stackoverflow-20150993
在没有web.xml和bean.xml的情况下,我都能完成所有工作
i have it all done without web.xml and without beans.xml
经过以下测试:
3倍F5在浏览器中访问的URL http://localhost:8080/mavenproject1 -1.0-SNAPSHOT/webresources/history
3 times F5 in a Browser to the url http://localhost:8080/mavenproject1-1.0-SNAPSHOT/webresources/history
输出:
20:32:12,955 SEVERE [com.mycompany.mavenproject1.HistoryAPI] (default task-2) HistoryAPI.init() called
20:32:12,956 SEVERE [com.mycompany.mavenproject1.HistoryAPI] (default task-2) HistoryAPI.doSomething() called
20:32:12,956 SEVERE [com.mycompany.mavenproject1.History] (default task-2) ===> History.init() called
20:32:12,956 SEVERE [com.mycompany.mavenproject1.History] (default task-2) ===> History.doSomething() called
20:36:09,830 SEVERE [com.mycompany.mavenproject1.HistoryAPI] (default task-3) HistoryAPI.init() called
20:36:09,831 SEVERE [com.mycompany.mavenproject1.HistoryAPI] (default task-3) HistoryAPI.doSomething() called
20:36:09,831 SEVERE [com.mycompany.mavenproject1.History] (default task-3) ===> History.doSomething() called
20:36:10,549 SEVERE [com.mycompany.mavenproject1.HistoryAPI] (default task-4) HistoryAPI.init() called
20:36:10,549 SEVERE [com.mycompany.mavenproject1.HistoryAPI] (default task-4) HistoryAPI.doSomething() called
20:36:10,549 SEVERE [com.mycompany.mavenproject1.History] (default task-4) ===> History.doSomething() called
这篇关于如何将CDI @ApplicationScoped bean注入@RequestScoped JAX-RS bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!