0

In an Interceptor class for automatically audit Hibernate CRUD operations, @Autowired annotation from Spring is not working.

I supose that it is due to, at configuration time, Spring doesn't have had time to configure autowiring stuff yet. In other classes everything is doing well. Is there a workaround or a "right-to-do" here? Thanks.

Class PersistenteConfig

//...
@Configuration
@EnableTransactionManagement
@PropertySource({"classpath:/foo/bar/persistence-mysql.properties"})
@ComponentScan({"foo.bar" })

        //...
               @Bean
               @Autowired
               public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
                  HibernateTransactionManager txManager = new HibernateTransactionManager();
                  txManager.setSessionFactory(sessionFactory);

                  AuditLogInterceptorImpl interceptor = new AuditLogInterceptorImpl();
                  txManager.setEntityInterceptor(interceptor);
                  return txManager;
               }
        //...

Class AuditLogInterceptorImpl

//...
    @Service
    @Transactional
    public class AuditLogInterceptorImpl extends EmptyInterceptor {

        private static final long serialVersionUID = 1L;

        @Autowired // <== NOT WORKING HERE... :o (object is null at runtime)
        EventsService eventsService;

//...
Alex
  • 3,325
  • 11
  • 52
  • 80
  • I think this is what you are looking for http://stackoverflow.com/questions/25274356/spring-jpa-hibernate-emptyinterceptor-not-injecting-entitymanager-spring-beans – jozzy Aug 20 '15 at 16:19
  • Indeed there were a similar case, but in that question's solution is said that you are not able to inject dependencies through Spring, what is not true if you manage to create a Spring bean with this interceptor, according to what is shown in this question answer... Nevertheless, that is another solution for this situation. On the other hand, if you'd like to manage the Interceptor as a Spring bean, this question shows how to do so. Thanks for advicing!! – Alex Aug 21 '15 at 14:04

1 Answers1

0

Your interceptor is not configured as a managed Spring bean when instantiated this way. Try the following, making the interceptor a managed (and thus injected) bean:

Class PersistenteConfig

//...
@Configuration
@EnableTransactionManagement
@PropertySource({"classpath:/foo/bar/persistence-mysql.properties"})
@ComponentScan({"foo.bar" })

        //...
               @Bean
               public AuditLogInterceptor auditLogInterceptor() {
                   return new AuditLogInterceptorImpl();
               }

               @Bean
               @Autowired
               public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
                  HibernateTransactionManager txManager = new HibernateTransactionManager();
                  txManager.setSessionFactory(sessionFactory);

                  txManager.setEntityInterceptor(auditLogInterceptor());
                  return txManager;
               }
        //...
rgielen
  • 176
  • 7
  • That's fine. Actually it worked, but I'd like to ask you to update your answer before accepting it to make clear Interface and Implementation. If you try to instantiate AuditLogInterceptorImpl directly, without an Interface, an error from java proxy is going to be thrown. So, the way is create a bean like `@Bean public AuditLogInterceptor auditLogInterceptor() { return new AuditLogInterceptorImpl(); } ` and thus casting it to txManager: `txManager.setEntityInterceptor((Interceptor) auditLogInterceptor());`. I think this is necessary to register solution for further questions ;) – Alex Aug 21 '15 at 13:54