Announcement Announcement Module
Collapse
No announcement yet.
@Aspect class getting null EntityManagerFactory Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Aspect class getting null EntityManagerFactory

    Hi all, I have declared an aspect like the following.

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

    My EntityManagerFactory is wired as the following

    Code:
    	<tx:annotation-driven transaction-manager="transactionManager" />
    
    	<context:component-scan base-package="aop.web.teacher.rdaos" />
    
    
    
    
    			<bean id="pgDS" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    				destroy-method="close" p:driverClass="org.postgresql.Driver"
    				p:jdbcUrl="${jdbc.url}" p:user="${jdbc.username}" p:password="${jdbc.password}"
    				p:minPoolSize="2" p:acquireIncrement="1" p:maxPoolSize="20" />
    
    			<bean id="dataSource"
    				class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    				p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
    				p:username="${jdbc.username}" p:password="${jdbc.password}" />
    
    			<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
    				p:entityManagerFactory-ref="entityManagerFactory" />
    
    			<bean id="entityManagerFactory"
    				class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    				p:dataSource-ref="pgDS" p:jpaVendorAdapter-ref="jpaAdapter">
    				<property name="loadTimeWeaver">
    					<bean
    						class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    				</property>
    				<property name="persistenceUnitName" value="wctemplatePU"></property>
    			</bean>
    
    			<bean id="jpaAdapter"
    				class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />

    The aspect executes fine but with null EntityManagerFactory .
    Where am I going wrong?

  • #2
    Since you're using JPA and Spring is managing the EntityManager, why not just use @PersistenceContext, as such:

    Code:
    @Aspect
    public class CacheMonitorImpl {
        @PersistenceContext
        private EntityManager em;
    }

    If you think you really need access the the EntityManagerFactory, then you could do this:

    Code:
    @Aspect
    public class CacheMonitorImpl {
        private EntityManagerFactory emf;
    
        @PersistenceUnit
        public void setEntityManagerFactory(EntityManagerFactory emf) {
            this.emf = emf;
        }
    }

    Read the Spring Reference guide for further details on implementing DAOs using JPA.

    Comment


    • #3
      Hi, did you manage to get this working? I have tried ising the @PersistenceContext and @Autowired, both result in null entity manager. Any advice would be great.

      Thanks.

      Comment

      Working...
      X