简体   繁体   中英

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

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>

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.

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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