How do I @Inject a CDI @ApplicationScoped bean into a @RequestScoped JAX-RS bean?

為{幸葍}努か 提交于 2020-01-03 13:35:34

问题


I've added the @ApplicationScoped CDI annotation to a simple bean:

@ApplicationScoped
public class History {

And tried to then @Inject this into a JAX-RS (resteasy) bean:

@RequestScoped
@Path("/history")
public class HistoryAPI {

@Inject
private History history;

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: 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>

回答1:


Try removing @RequestScoped from HistoryAPI.

There is a good JAX-RS example in wildfly-quickstart projects

I tried it on WF8 beta1 and everything gets injected nicely.




回答2:


strange... i think your code should work...

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

i have it all done without web.xml and without beans.xml

tested with:

3 times F5 in a Browser to the url http://localhost:8080/mavenproject1-1.0-SNAPSHOT/webresources/history

output:

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 


来源:https://stackoverflow.com/questions/20150993/how-do-i-inject-a-cdi-applicationscoped-bean-into-a-requestscoped-jax-rs-bean

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