Avatar billede fredand Forsker
18. december 2022 - 17:54 Der er 6 kommentarer

EJB-annotation does not work, why is that?

Hello,

My @EJB-annotation does not work, why is that?

I need help to understand why my bean is null at the breakpoint below.

I have tried some different usage of the @EJB but none seems to get the bean injected/initiated.

To get it initiated I need to do a manually "InitialContext.doLookup".

(My environmen is Java 8 and Weblogic 14)

I got a interface like this:

[/code]
    package com.mycompany.cruds.interfaces;

    import java.io.Serializable;
    import java.sql.SQLException;
    import java.util.List;

    import javax.naming.NamingException;

    import com.mycompany.cruds.domain.Domain;
    import com.mycompany.cruds.domain.DomainSearchCriteria;

    public interface CRUDS {
       
        public Serializable create(Domain domain) throws NamingException, SQLException;
       
    }
[/code]

...and an other interface like this:

[code]
    package com.mycompany.cruds.interfaces;

    import javax.ejb.Local;

    @Local
    public interface CrudSessionLocal extends CRUDS {
       
        public static final String JNDI_NAME = "java:global.cruds-app.server.CrudsSessionBean!com.mycompany.cruds.interfaces.CrudSessionLocal";

    }
[/code]

...these are packed in a interfaces.jar

I got a stateless bean like this:

[code]
    package com.mycompany.cruds.server;

    import java.io.Serializable;
    import java.sql.SQLException;
    import java.util.List;

    import javax.ejb.Stateless;
    import javax.naming.NamingException;

    import com.mycompany.cruds.domain.Domain;
    import com.mycompany.cruds.domain.DomainSearchCriteria;
    import com.mycompany.cruds.interfaces.CrudSessionLocal;
    import com.mycompany.cruds.server.dao.DomainDaoBean;

    @Stateless
    public class CrudsSessionBean implements CrudSessionLocal {

        private DomainDaoBean domainDaoBean = new DomainDaoBean();
       
        public CrudsSessionBean() {
            super();
        }
       
        @Override
        public Serializable create(Domain domain) throws NamingException, SQLException {
           
            Serializable serializable = domainDaoBean.create(domain);
            return serializable;
        }

    }
[/code]

...that is packed in a server.jar

I got a servlet like this:

[code]
    package com.mycompany.cruds.web_client;

    import java.io.IOException;
    import java.sql.SQLException;

    import javax.ejb.EJB;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import com.mycompany.cruds.domain.LogItem;
    import com.mycompany.cruds.domain.LogItem.Level;
    import com.mycompany.cruds.interfaces.CrudSessionLocal;

    public class CreateServlet extends HttpServlet {

        private static final long serialVersionUID = 1L;

        //@EJB   
        //@EJB(beanName = "CrudsSessionBean")
        @EJB(lookup = "java:global.cruds-app.server.CrudsSessionBean!com.mycompany.cruds.interfaces.CrudSessionLocal")
        private CrudSessionLocal crudSessionLocal;
       
        public CreateServlet() {
            super();
        }

        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {


        //BREAKPOINT
            if (crudSessionLocal == null) {
                try {

                    crudSessionLocal = (CrudSessionLocal) InitialContext.doLookup(CrudSessionLocal.JNDI_NAME);

                } catch (NamingException e) {

                }
            }

            ...
[/code]
...that is packed in a web-client.war

All three artifacts are packed in a ear with a application.xml:
[code]
      <display-name>app</display-name>
      <module>
        <ejb>server.jar</ejb>
      </module>
      <module>
        <web>
          <web-uri>web-client.war</web-uri>
          <context-root>/web-client</context-root>
        </web>
      </module>
      <library-directory>lib</library-directory>
    </application>
[/code]

...inside lib/ is the interfaces.jar of course.
Do you guys see why the @EJB-annotation does not work?

Best regards
Fredrik
Avatar billede arne_v Ekspert
18. december 2022 - 19:48 #1
@EJB without JNDI name should be suffcient.

Does dependency injection in general work? (I expect it to in WebLogic, but it can be tricky in a standalone Tomcat)

Can you provide output from "jar tvf" on ear, war and jar files? (it seems most likely to be a packaging problem)
Avatar billede fredand Forsker
18. december 2022 - 20:36 #2
Hello Arne!
It seems you were on the right track.
The problem was:

<web-app id="weblogic-web-app"
    version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
            http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
            version="2.5">

It was mentioned on: https://community.oracle.com/tech/developers/discussion/2186747/need-help-with-ejb-dependency-injection-null-pointer

Best regards
/Fredrik
Avatar billede arne_v Ekspert
18. december 2022 - 20:59 #3
So DI requires servlet 2.5 instead of 2.4?
Avatar billede fredand Forsker
18. december 2022 - 22:46 #4
Hello,
I must admitt that I really have not study the versions of what combo of versions is needed for the xml-descriptors there is for different capacita.
I found this comment on sof that might be interesting. https://stackoverflow.com/a/15987198/2902165
I guess I will see if I could use DI whithin the server.jar with 2.4 in the war. (Sounds irrelevant though)
I will also try out 3.0 as well.
Perhaps there is even later versions?
Best regards
Fredrik
Avatar billede arne_v Ekspert
19. december 2022 - 00:54 #5
servlet 2.4 part of J2EE 1.4 (2003)
servlet 2.5 part of Java EE 5 (2006)
servlet 3.0 part of Java EE 6 (2009)
servlet 3.1 part of Java EE 7 (2013)
servlet 4.0 part of Java EE 8 (2017)
servlet 5.0 part of Jakarta EE 9 (2020)
servlet 6.0 part of Jakarta EE 10 (2022)

WL 14 should support at least Java EE 8
Avatar billede arne_v Ekspert
19. december 2022 - 01:07 #6
I think it has to be fixed in web.xml - the DI happens when the servlet container initializes the servlet from the war. If the servlet container does not scan for that annotation nothing in the jar file with the EJB can  do anything to fix that.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester