Rakesh Kumar Cherukuri
Rakesh Kumar Cherukuri

Reputation: 1271

Dependency injection (@Inject and @PersistenceContext) failing for stateless bean

Env :

Wildfly 8.2.0 / Wildfly 9.0.1

My application structure is as follows

Servlet -> MyStateless1 -> MyStateless2 -> entity manager api

As long as both MyStateless1 and MyStateless2 are non EJBs, it is working flawlessly with pure CDI injection i.e. able to query DB successfully. Once I mark MyStateless1 as @Stateless, even though the @Inject of EJB MyStateless1 in servlet works fine, the injection inside MyStateless1 itself doesn't work. Note that both @Inject and @PersistenceContext fails to inject the concerned resources.

Here is the code

@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
    @Inject private MyStateless1 stateless1;

    protected void doGet(HttpServletRequest request, 
                         HttpServletResponse response) {
      stateless1.businessMethod();
      response.getWriter().append("Served at: ").append(request.getContextPath());
    }
}

First level Bean

//@Stateless
public class MyStateless1 {
    @Inject private MyStateless2 stateless2;

    @PersistenceContext(unitName = "cdi-rest")
    private EntityManager entityManager;

    public MyStateless1() {
      System.out.println("MyStateless1() constructed");
    }

    public final void businessMethod() {
      if (entityManager == null ) { System.out.println(" +++++ Entity Manager is null"); }
      if (stateless2 == null ) { System.out.println(" ===== stateless2 is null"); } else { stateless2.finder(); }
    }
}

Second level bean

public class MyStateless2 {

  public MyStateless2() {
    System.out.println("MyStateless2() constructed ");
  }

  @PersistenceContext(unitName = "cdi-rest")
  private EntityManager entityManager;

  public final List<MyEntity> finder() {
    TypedQuery<MyEntity> query = entityManager.createQuery("select o from MyEntity o", MyEntity.class);
    return query.getResultList();
  }
}

The moment i uncomment the @Stateless in first level bean, injection inside MyStsteless2 doesnt work i.e. both @Inject and @PersistenceContext

The folder structure is as follows

├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── rakesh
│   │   │           ├── model
│   │   │           │   └── MyEntity.java
│   │   │           ├── persistence
│   │   │           │   └── MyStateless2.java
│   │   │           ├── service
│   │   │           │   └── MyStateless1.java
│   │   │           └── servlet
│   │   │               └── MyServlet.java
│   │   ├── resources
│   │   │   └── META-INF
│   │   │       └── persistence.xml
│   │   └── webapp
│   │       └── WEB-INF
│   │           ├── beans.xml
│   │           └── web.xml
│   └── test
│       ├── java
│       └── resources

What i had tried:

Any thoughts/help is greatly appreciated.

Thanks in advance,

Rakesh

Upvotes: 0

Views: 486

Answers (1)

Rakesh Kumar Cherukuri
Rakesh Kumar Cherukuri

Reputation: 1271

Thanks to @JBNizet; it was 'final' modifier in methods that caused the issue.

Upvotes: 1

Related Questions