2 Replies Latest reply: Dec 15, 2011 10:00 AM by Vyacheslav Sakhno RSS

RevisionListener spring integration as spring bean

Vyacheslav Sakhno Newbie

I need to do some database processing in revision listener. For that i need inejction capabilities of Spring Framework. How can this be implemented? Here is the code representing the need but CustomRevisionListener is instanciated by 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());

          }

}