@Aspect 类获取 null EntityManagerFactory

发布于 2024-11-10 12:37:16 字数 2023 浏览 1 评论 0原文

我已经声明了如下所示的方面


@Aspect
public class CacheMonitorImpl {
    private final static Logger LOG = LoggerFactory
            .getLogger(CacheMonitorImpl.class);

    private final static NumberFormat NF = new DecimalFormat("0.0###");

    @Autowired
    private EntityManagerFactory entityManagerFactory;

    @Around("execution(* aop.web.teacher.service..*.*(..))")
    public Object log(ProceedingJoinPoint pjp) throws Throwable {

        LOG.info("$$ Test Property :: " + testprop);

        if (!LOG.isDebugEnabled()) {
            LOG.info("####### Logger is not debug enabled"
                    + entityManagerFactory);
            return pjp.proceed();
        }

        HibernateEntityManagerFactory hbmanagerfactory = (HibernateEntityManagerFactory) entityManagerFactory;
        SessionFactory sessionFactory = hbmanagerfactory.getSessionFactory();

        Statistics statistics = sessionFactory.getStatistics();
        statistics.setStatisticsEnabled(true);

        long hit0 = statistics.getQueryCacheHitCount();
        long miss0 = statistics.getSecondLevelCacheMissCount();

        Object result = pjp.proceed();

        long hit1 = statistics.getQueryCacheHitCount();
        long miss1 = statistics.getQueryCacheMissCount();

        double ratio = (double) hit1 / (hit1 + miss1);

        if (hit1 > hit0) {
            LOG.debug(String.format("CACHE HIT; Ratio=%s; Signature=%s#%s()",
                    NF.format(ratio), pjp.getTarget().getClass().getName(), pjp
                            .getSignature().toShortString()));
        } else if (miss1 > miss0) {
            LOG.debug(String.format("CACHE MISS; Ratio=%s; Signature=%s#%s()",
                    NF.format(ratio), pjp.getTarget().getClass().getName(), pjp
                            .getSignature().toShortString()));
        } else {
            LOG.debug("query cache not used");
        }

        return null;
    }

}

现在正在调用方面方法,但我得到的是 null EntityManagerFactory。请指出我这样做的正确性! 提前致谢。

I have declared an aspect like the following


@Aspect
public class CacheMonitorImpl {
    private final static Logger LOG = LoggerFactory
            .getLogger(CacheMonitorImpl.class);

    private final static NumberFormat NF = new DecimalFormat("0.0###");

    @Autowired
    private EntityManagerFactory entityManagerFactory;

    @Around("execution(* aop.web.teacher.service..*.*(..))")
    public Object log(ProceedingJoinPoint pjp) throws Throwable {

        LOG.info("$ Test Property :: " + testprop);

        if (!LOG.isDebugEnabled()) {
            LOG.info("####### Logger is not debug enabled"
                    + entityManagerFactory);
            return pjp.proceed();
        }

        HibernateEntityManagerFactory hbmanagerfactory = (HibernateEntityManagerFactory) entityManagerFactory;
        SessionFactory sessionFactory = hbmanagerfactory.getSessionFactory();

        Statistics statistics = sessionFactory.getStatistics();
        statistics.setStatisticsEnabled(true);

        long hit0 = statistics.getQueryCacheHitCount();
        long miss0 = statistics.getSecondLevelCacheMissCount();

        Object result = pjp.proceed();

        long hit1 = statistics.getQueryCacheHitCount();
        long miss1 = statistics.getQueryCacheMissCount();

        double ratio = (double) hit1 / (hit1 + miss1);

        if (hit1 > hit0) {
            LOG.debug(String.format("CACHE HIT; Ratio=%s; Signature=%s#%s()",
                    NF.format(ratio), pjp.getTarget().getClass().getName(), pjp
                            .getSignature().toShortString()));
        } else if (miss1 > miss0) {
            LOG.debug(String.format("CACHE MISS; Ratio=%s; Signature=%s#%s()",
                    NF.format(ratio), pjp.getTarget().getClass().getName(), pjp
                            .getSignature().toShortString()));
        } else {
            LOG.debug("query cache not used");
        }

        return null;
    }

}

Now the aspect method is getting invoked but I am getting null EntityManagerFactory. Please point me to the correct of doing this!
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文