Search code examples
springdependency-injectionhibernate-envers

hibernate-envers RevisionListener spring integration as spring bean


I need to do some database processing in revision listener of hibernate-envers. For that I need inejction capabilities of Spring Framework. How can this be implemented? Here is the code representing the need but CustomRevisionListener is instantiated by a constructor in Envers code. Spring has only SecurityContextHolder as static service locator. How to inject other beans?

@Service
public class CustomRevisionListener implements EntityTrackingRevisionListener {

      @Resource
      private PersistenceManagerHibernate persistenceManagerHibernate;

      public void newRevision(Object revisionEntity) {
                CustomRevisionEntity revision = (CustomRevisionEntity) revisionEntity;
    revision.setUsername(getUsername());
      }


      public String getUsername() {
    final SecurityContext context = SecurityContextHolder.getContext();
    if (context != null) {
        if (context.getAuthentication() != null) {
                  return context.getAuthentication().getName();
        } else {
                  return "anonymous";
        }
    }
    return "anonymous";
      }

      @Override
      public void entityChanged(@SuppressWarnings("rawtypes") Class entityClass, String entityName, Serializable entityId, RevisionType revisionType, Object revisionEntity) {
                CustomRevisionEntity revision = (CustomRevisionEntity) revisionEntity;
                revision.setEntityId(entityId.toString());
                revision.setEntityName(entityName);
                revision.setRevisionType((int)revisionType.getRepresentation());
                Auditable auditable = null;
                if (entityId instanceof Long) {
                          auditable = persistenceManagerHibernate.findById(entityClass, (Long)entityId);
                }
                revision.setGroupName(auditable.getAuditGroupName());
                revision.setGroupEntityId(auditable.getAuditGroupId());
      }
  }

Solution

  • Since CustomRevisionListener is instantiated by a constructor in Envers, you have to find another way to retrieve a handle to a Spring managed bean.

    You could create a utility class to achieve this:

    /**
     * Utility class which provides lookup services for Spring managed beans from 
     *  within unmanaged beans.
     */
    @Component
    public class ContextLookup implements ApplicationContextAware {
    
       private static ApplicationContext sApplicationContext;
    
       @Override
       public void setApplicationContext( ApplicationContext aApplicationContext ) 
                                                             throws BeansException {
          setContext( aApplicationContext );
       }
    
       public static void setContext( ApplicationContext aApplicationContext ) {
           sApplicationContext = aApplicationContext;
       }
    
       protected static ApplicationContext getApplicationContext() {
          return sApplicationContext;
       }
    
       public static Object getBean( String aName ) {
         if ( sApplicationContext != null ) {
           return sApplicationContext.getBean( aName );
         }
         return null;
       }
    
       public static <T> T getBean( Class<T> aClass ) {
          if ( sApplicationContext != null ) {
             return sApplicationContext.getBean( aClass );
          }
          return null;
       }
     }
    

    and in your CustomRevisionListener

    public class CustomRevisionListener {
    
       public void myMethod() {
          ..
          // get a handle to your spring managed bean
          Foo foo = (Foo)ContextLookup.getBean( "mySpringManagedBean" );
          ..
       }
    
     }