2016-09-05 15 views
1

Я пытаюсь использовать entityInterceptor для регистрации изменений на определенных объектах для целей аудита.Spring @Resource injection возвращает NullPointerException

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="entityInterceptor">  
      <bean class="x.y.z.PropertyChangeInterceptor"/> 
     </property>   
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
       ------ ------ 
      </props> 
     </property> 
     <property name="packagesToScan"> 
      <array> 
       <value>x.y.z</value> 
      </array> 
     </property> 
    </bean> 

я имел пользовательские слушатель изменений для объекта Я хочу, чтобы контролировать, чтобы PropertyChangeInterceptor конструктор и имеет переопределить методы onFlushDirty, OnSave и postFlush.

public PropertyChangeInterceptor() { 
    listeners = new HashMap<>(); 
     listeners.put(Customer.class, new CustomerChangeListener()); 
    } 

Теперь проблема возникает в моем классе CustomerChangeListener. Я ввожу службу, которую я намерен использовать для сохранения журналов аудита в моей базе данных. Служба запускает исключение nullpointer, когда я пытаюсь его использовать. Интересно, что эта услуга хорошо работает на всех моих других контроллерах, кроме этого класса. Класс реализует PropertyChangeListener.

@Component 
public class CustomerChangeListener implements PropertyChangeListener<Customer> { 

    Logger log = LoggerFactory.getLogger(CustomerChangeListener.this.getClass()); 

    @Resource(name = "audittrailService") 
    private ServiceInterface audittrailService; 
     ...... 
     ...... 
@Override 
    public void logIt(String action, Object entity) { 
     try { 
      audittrailService.findById(1); **<==Error Occurs here** 
      .......... 
      audittrailService.save(trail); 
     } catch (UnknownHostException uhe) { 
      log.error("Error Saving audit trail -UnknownHostException", uhe); 
     } catch (Exception e) { 
      log.error("Error Saving audit trail", e); 
     } 
    } 

Я считаю, что мои конфигурации Spring Anotation в порядке, поскольку они работают с другими классами.

<!-- Enable Spring Annotation Configuration --> 
    <context:annotation-config/> 

    <aop:aspectj-autoproxy proxy-target-class="true" /> 

    <!-- Necessary to get the entity manager injected into the factory bean --> 
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 

    <!--Enable Spring's Exception Translation Mechanism--> 
    <!-- Enable Spring Annotation Configuration --> 
    <context:annotation-config/> 

    <aop:aspectj-autoproxy proxy-target-class="true" /> 

    <!-- Necessary to get the entity manager injected into the factory bean --> 
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 

    <!--Enable Spring's Exception Translation Mechanism--> 
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> 

    <context:component-scan base-package="com.anchor.anchorfused4.Service"/> 
    <context:component-scan base-package="..."/> 
    <context:component-scan base-package="..."/> 
    <context:component-scan base-package="com.anchor.anchorfused4.Listeners"/> 

Вот журнал ошибок.

Severe: java.lang.NullPointerException 
at com.anchor.anchorfused4.Listeners.CustomerChangeListener.logIt(CustomerChangeListener.java:59) 
at com.anchor.anchorfused4.Listeners.PropertyChangeInterceptor.postFlush(PropertyChangeInterceptor.java:107) 
at org.hibernate.event.internal.AbstractFlushingEventListener.postFlush(AbstractFlushingEventListener.java:388) 
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:53) 
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1234) 
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404) 
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) 
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175) 
at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:558) 
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757) 
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726) 
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:496) 
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:276) 
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653) 
at com.anchor.anchorfused4.Service.Impl.CustomerService$$EnhancerBySpringCGLIB$$3bf59009.update(<generated>) 
at com.anchor.anchorfused4.Controller.CustomerController.updateCustomer(CustomerController.java:280) 

журнала запуска в соответствии с просьбой @dit

Info: GlassFish Server Open Source Edition 4.1 (13) startup time : Felix (6,084ms), startup services(1,620ms), total(7,704ms) 
Info: Grizzly Framework 2.3.15 started in: 2ms - bound to [/0.0.0.0:7676] 
Info: HV000001: Hibernate Validator 5.0.0.Final 
Info: JMXStartupService has started JMXConnector on JMXService URL service:jmx:rmi://user-PC:8686/jndi/rmi://user-PC:8686/jmxrmi 
Info: Registered com.su[email protected]55b89494 as OSGi service registration: [email protected] 
Info: Grizzly Framework 2.3.15 started in: 8ms - bound to [/0.0.0.0:8080] 
Info: Grizzly Framework 2.3.15 started in: 15ms - bound to [/0.0.0.0:8181] 
Info: visiting unvisited references 
Info: Java security manager is disabled. 
Info: Entering Security Startup Service. 
Info: Loading policy provider com.sun.enterprise.security.provider.PolicyWrapper. 
Info: Security Service(s) started successfully. 
Info: Created HTTP listener http-listener-1 on host/port 0.0.0.0:8080 
Info: Created HTTP listener http-listener-2 on host/port 0.0.0.0:8181 
Info: Created HTTP listener admin-listener on host/port 0.0.0.0:4848 
Info: Created virtual server server 
Info: Created virtual server __asadmin 
Info: Setting JAAS app name glassfish-web 
Info: Virtual server server loaded default web module 
Info: Initializing Mojarra 2.2.7 (20140610-1547 https://svn.java.net/svn/mojarra~svn/tags/[email protected]) for context '/MinjaAnchorERP' 
Info: SecretKey: J8WnmCmANrta3jQL3rPITA== 
Info: Running on PrimeFaces 5.3 
Info: Running on PrimeFaces Extensions 4.0.0 
Info: WebModule[null] ServletContext.log():Initializing Spring root WebApplicationContext 
Severe: [admin-listener(5)] INFO org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started 
Severe: [admin-listener(5)] INFO org.springframework.web.context.support.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Mon Sep 05 12:37:49 EAT 2016]; root of context hierarchy 
Severe: [admin-listener(5)] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/config/web-application-config.xml] 
Severe: [admin-listener(5)] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/config/data-access-config.xml] 
Severe: [admin-listener(5)] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/config/webflow-config.xml] 
Severe: [admin-listener(5)] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/config/webmvc-config.xml] 
Severe: [admin-listener(5)] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/config/security-config.xml] 
Severe: [admin-listener(5)] INFO org.springframework.security.core.SpringSecurityCoreVersion - You are running with Spring Security Core 3.2.5.RELEASE 
Severe: [admin-listener(5)] INFO org.springframework.security.config.SecurityNamespaceHandler - Spring Security 'config' module version is 3.2.5.RELEASE 
....... 
....... 
Severe: [admin-listener(5)] INFO com.anchor.anchorfused4.Listeners.PropertyChangeInterceptor - Init 
Info: HCANN000001: Hibernate Commons Annotations {4.0.2.Final} 
Info: HHH000412: Hibernate Core {4.2.4.Final} 
Info: headless=true 
Severe: [admin-listener(5)] INFO com.anchor.anchorfused4.Listeners.CustomerChangeListener - init CustomerChangeListener 
Severe: [admin-listener(5)] INFO com.anchor.anchorfused4.Listeners.PropertyChangeInterceptor - Init 
Severe: [admin-listener(5)] INFO com.anchor.anchorfused4.Controller.PermissionMapController - Init 
Severe: [admin-listener(5)] INFO org.springframework.cache.ehcache.EhCacheManagerFactoryBean - Initializing EhCache CacheManager 
+0

Использование '@ Autowire' вместо' @ Resource' – dit

+0

@dit Я просто попытался с '@Autowired 'все равно получить исключение нулевого указателя –

+0

PLS опубликовать журнал автозагрузки – dit

ответ

1

мне удалось решить проблему.

Я понял, что мне нужно было Autowire пользовательского класса CustomerChangeListener вместо его создания с помощью new CustomerChangeListener(), но Autowiring этого класса отказался работать. Проект не мог даже начаться в отличие от ранее, когда он исключил исключение.

Небольшое исследование, основанное на этой новой ошибке, и я нашел, что причина для нулевого указателя на классы, введенные с помощью весенней аннотации, состоит в том, что entityInterceptor управляется спящим режимом, а не управляется пружиной.

Ответ здесь https://stackoverflow.com/a/25275333/2576937 помог много.

В дополнение к созданию DelegateInterceptor мне пришлось перенести слушателей из PropertyChangeInterceptor конструктора его @PostConstruct

public PropertyChangeInterceptor() { 
    //listeners = new HashMap<>(); 
    //listeners.put(Customer.class, new CustomerChangeListener()); 
} 

@PostConstruct 
public void init() { 
    StaticDelegateInterceptor.setInterceptor(this); 
    listeners = new HashMap<>(); //move to here 
    listeners.put(Customer.class, customerChangeListener); 
} 
+0

Кроме того, мы не должны использовать аннотацию @Resource на уровне сервиса, вместо этого она предназначена для уровня dao – Sandeep