@Aspect类获取null EntityManagerFactory

时间:2011-05-28 12:53:35

标签: java aspectj entitymanager

我已声明了以下

之类的方面

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

}

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

0 个答案:

没有答案