Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CDI call interceptor annotated method within same instance

here is my DAO implementation, i will load the whole table and cached in memory for a certain period of time

@ApplicationScoped
public class DataAccessFacade {

   @Inject
   private EntityManager em;

   @CacheOutput
   public Map<String, String> loadAllTranslation() {
      List<Translation> list = em.createQuery("select t from Translation t").getResultList();    
      Map<String, String> result = new HashMap<String, String>();
      // do more processing here, omitted for clarity     
      return result;
   }

   public String getTranslation(String key) {
      return loadAllTranslation().get(key);
   }

}

here is my jersey client

@Inject
DataAccessFacade dataAccessFacade;

@Path("/5")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String t5(@QueryParam("id") String key) {
  // load the data from dataAccessFacade
  String text = dataAccessFacade.getTranslation(key); 
  String text2 = dataAccessFacade.loadAllTranslation().get(key); 
}

in the client if i call the dataAccessFacade.loadAllTranslation(), i will see the interceptor logic been executed

if i call the dataAccessFacade.getTranslation() which internally call the loadAllTranslation(), then i didn't see the interceptor been executed

what is the problem here?

how to solve it?

like image 483
Dapeng Avatar asked Sep 18 '25 00:09

Dapeng


2 Answers

This is the correct behavior as in the CDI spec. Only methods called by "client" classes are considered "business methods" and, thus, are intercepted.

like image 55
Hendy Irawan Avatar answered Sep 22 '25 21:09

Hendy Irawan


just do the following in your DataAccessFacade:

@Inject
private Provider<DataAccessFacade> self;

public String getTranslation(String key) {
  return self.get().loadAllTranslation().get(key);
}
like image 32
Xavier Dury Avatar answered Sep 22 '25 21:09

Xavier Dury