-
1. Re: Injection of Simple Environment Entry into Session Bean Using @Resource Annotation
sfcoy Oct 17, 2013 5:02 AM (in response to bafco)What happens if you change the web.xml to:
<env-entry-name>greeting</env-entry-name>
and the java code to:
@Resource(name = "greeting")
?
-
2. Re: Injection of Simple Environment Entry into Session Bean Using @Resource Annotation
bafco Oct 17, 2013 5:33 AM (in response to sfcoy)The result is the same, greeting == null. I gave up on using @Resource.name() in session bean classes as it somehow doesn't seem to work at all. (However, it works great on CDI managed beans.)
-
3. Re: Injection of Simple Environment Entry into Session Bean Using @Resource Annotation
sfcoy Oct 17, 2013 7:27 AM (in response to bafco)Actually, I suspect that env-entry elements in a web.xml will not be picked up by EJBs. You probably need to add then to an ejb-jar.xml file.
-
4. Re: Re: Injection of Simple Environment Entry into Session Bean Using @Resource Annotation
bafco Oct 17, 2013 1:13 PM (in response to sfcoy)As I stated before, declaring the env-entry in ejb-jar.xml doesn't help to solve the problem. It works the same way as web.xml, i.e. it doesn't work.
Here is my WEB-INF/ejb-jar.xml for the @Resource(name = greeting) version:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" metadata-complete="false" version="3.1" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"> <enterprise-beans> <session> <ejb-name>Bar</ejb-name> <session-type>Stateless</session-type> <env-entry> <env-entry-name>greeting</env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <env-entry-value>Hello</env-entry-value> </env-entry> </session> </enterprise-beans> </ejb-jar>
I also tried @Resource(lookup = ...) with names 'java:app/env/greeting' and 'java:comp/env/greeting'.
Everything seems to be the same, e.g. Context.lookup() works just fine and returns the correct string.
Anyway, I can't find anything in any specification indicating that env-entries in web.xml might be "ignored" by EJBs.
I consider this a bug; unless someone can successfully inject values of env-entries defined in a deployment descriptor using @Resource into a session bean, all in a war file (implying that I've made a mistake somewhere.) -
5. Re: Injection of Simple Environment Entry into Session Bean Using @Resource Annotation
jaikiran Oct 18, 2013 1:46 AM (in response to bafco)1 of 1 people found this helpfulThis should have worked. Let me take a look. We have had recent changes in this area and it's possible that we might have introduced a bug.
-
6. Re: Re: Re: Injection of Simple Environment Entry into Session Bean Using @Resource Annotation
sfcoy Oct 18, 2013 1:49 AM (in response to bafco)I think this is missing a
<ejb-class>...</ejb-class>
element.
-
7. Re: Re: Re: Re: Injection of Simple Environment Entry into Session Bean Using @Resource Annotation
bafco Oct 18, 2013 8:57 AM (in response to sfcoy)Yep, I tried this trick before, but did not work either. I added
<ejb-class>org.jboss.cdi.tck.interceptors.tests.contract.interceptorLifeCycle.jndi.ejb.Bar</ejb-class>
under <ejb-name> element in ejb-jar.xml, as Bar's package is org.jboss.cdi.tck.interceptors.tests.contract.interceptorLifeCycle.jndi.ejb.
-
8. Re: Re: Re: Re: Re: Injection of Simple Environment Entry into Session Bean Using @Resource Annotation
sfcoy Oct 21, 2013 6:46 AM (in response to bafco)1 of 1 people found this helpfulI just tried this out on a WildFly 8.0 Beta1 SNAPSHOT and it works for me using the env-entry from the web.xml.
<env-entry> <env-entry-name>testRef</env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <env-entry-value>Foo bar</env-entry-value> </env-entry>
and
@Stateless public class MemberRegistration { @Inject private Logger log; @Inject private EntityManager em; @Inject private Event<Member> memberEventSrc; @Resource(name="testRef") private String sampleEnvRef; @PostConstruct void testEnvRef() { log.info("Installed env ref sampleEnvRef = '" + sampleEnvRef + "'"); } public void register(Member member) throws Exception { log.info("Registering " + member.getName()); em.persist(member); memberEventSrc.fire(member); } }
giving:
21:17:19,827 INFO [org.jboss.demos.forum2210.envrefdemo.service.MemberRegistration] (default task-15) Installed env ref sampleEnvRef = 'Foo bar'
I'll try it out with a beta2 snapshot shortly
-
9. Re: Injection of Simple Environment Entry into Session Bean Using @Resource Annotation
sfcoy Oct 21, 2013 6:47 AM (in response to sfcoy)This code also works in a wildfly-8.0.0.Beta2-SNAPSHOT fwiw.
In addition, I tried changing the env-entry-name to java:global/testRef and the @Resource name to "java:global/testRef" and that works too.
You must have something else going wrong.
-
10. Re: Injection of Simple Environment Entry into Session Bean Using @Resource Annotation
bafco Oct 22, 2013 6:15 AM (in response to sfcoy)How do you create a MemberRegistration instance?
-
11. Re: Injection of Simple Environment Entry into Session Bean Using @Resource Annotation
sfcoy Oct 22, 2013 7:35 AM (in response to bafco)The container creates it. Instances of it are injected into a number of other CDI beans.
It's (based on) sample code from https://github.com/jboss-jdf/jboss-as-quickstart/.
You will never get injection working on objects that you create yourself using "new".
-
12. Re: Injection of Simple Environment Entry into Session Bean Using @Resource Annotation
bafco Oct 24, 2013 5:02 AM (in response to bafco)Actually, the problem was that I was accessing session bean's field from outside the class. That is why I got bar.greeting == null;, injection works just fine.
Thank you both for your help and effort. Jaikiran's tests in WF testsuite helped me solve the problem