本文介绍了CDI在同一实例中调用拦截器注释方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的DAO实现,我将加载整个表并在内存中缓存一段时间

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);
   }

}

这是我的球衣客户

@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);
}

客户端中的

如果我调用dataAccessFacade.loadAllTranslation(),我会看到拦截器逻辑已被执行

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

如果我调用dataAccessFacade.getTranslation()在内部调用loadAllTranslation(),那么我没有看到拦截器被执行

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

这里有什么问题?

如何解决?

推荐答案

这是CDI规范中的正确行为。只有客户类调用的方法才被视为业务方法,因此被截获。

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

这篇关于CDI在同一实例中调用拦截器注释方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 11:38