2011-12-15 45 views
9

Tôi cần phải làm một số xử lý cơ sở dữ liệu trong phiên bản nghe của hibernate-envers. Đối với điều đó tôi cần khả năng inejction của Spring Framework. Làm thế nào điều này có thể được thực hiện? Đây là mã đại diện cho nhu cầu nhưng CustomRevisionListener được khởi tạo bởi một hàm tạo trong mã Envers. Mùa xuân chỉ có SecurityContextHolder làm trình định vị dịch vụ tĩnh. Làm thế nào để tiêm đậu khác?hibernate-envers RevisionListener mùa xuân hội nhập như đậu mùa xuân

@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()); 
     } 
    } 

Trả lời

7

Kể từ CustomRevisionListener được khởi tạo bởi một constructor trong Envers, bạn phải tìm một cách khác để lấy một tay cầm một Spring bean được quản lý.

Bạn có thể tạo ra một lớp tiện ích để đạt được điều này:

/** 
* 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; 
    } 
} 

và trong CustomRevisionListener bạn

public class CustomRevisionListener { 

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

} 
+0

Cảm ơn, làm việc tuyệt vời. – realcnbs