有条件的Envers审计

问题描述 投票:8回答:5

我有一个要求,我只想在更改状态字段时审核记录。我遵循了文档章节教程“15.8。条件审计”。

第1步:关闭自动Envers事件侦听器注册。我有以下内容:

<prop key="hibernate.listeners.envers.autoRegister">false</prop>

第2步:为适当的事件侦听器创建子类。

public class DeleteEnversListener extends EnversPostDeleteEventListenerImpl {   
    private static final long serialVersionUID = 5906427978349712224L;
    private static Log log = LogFactory.getLog(DeleteEnversListener.class);

    public DeleteEnversListener(AuditConfiguration enversConfiguration) {
        super(enversConfiguration);
    }

    @Override
    public void onPostDelete(PostDeleteEvent event) {
        log.info("!!! just logging entity !! "+ event.getEntity());
        super.onPostDelete(event);
    }   
}

以类似的方式,我有

  • InsertEnversListener
  • UpdateEnversListener
  • DeleteEnversListener
  • CollectionRecreateEnversListener
  • PreCollectionRemoveEnversListener
  • PreCollectionUpdateEnversListener

第3步:创建自己的org.hibernate.integrator.spi.Integrator实现

public class CustomEnversIntegrator extends EnversIntegrator   {

    private static Log log = LogFactory.getLog(CustomEnversIntegrator.class);

    @Override
    public void integrate(Configuration configuration,
            SessionFactoryImplementor sessionFactory,
            SessionFactoryServiceRegistry serviceRegistry) {

        super.integrate(configuration, sessionFactory, serviceRegistry);
        final AuditConfiguration enversConfiguration = AuditConfiguration.getFor( configuration, serviceRegistry.getService( ClassLoaderService.class ) );
        EventListenerRegistry listenerRegistry = serviceRegistry.getService( EventListenerRegistry.class );

        System.out.println("Registering event listeners");
        if (enversConfiguration.getEntCfg().hasAuditedEntities()) {
            listenerRegistry.appendListeners(EventType.POST_INSERT, new InsertEnversListener(enversConfiguration));
            listenerRegistry.appendListeners(EventType.POST_UPDATE, new UpdateEnversListener(enversConfiguration));
            listenerRegistry.appendListeners(EventType.POST_DELETE, new DeleteEnversListener(enversConfiguration ) );
            listenerRegistry.appendListeners(EventType.POST_COLLECTION_RECREATE, new CollectionRecreateEnversListener(enversConfiguration ) );
            listenerRegistry.appendListeners(EventType.PRE_COLLECTION_REMOVE, new PreCollectionRemoveEnversListener(enversConfiguration ) );
            listenerRegistry.appendListeners(EventType.PRE_COLLECTION_UPDATE, new PreCollectionUpdateEnversListener(enversConfiguration ) );
        }

    }
}

步骤4:为了在Hibernate启动时自动使用积分器,您需要添加一个META-INF/services/org.hibernate.integrator.spi.Integrator文件。这是org.hibernate.integrator.spi.Integrator文件的内容

com.hib.sample.listener.CustomEnversIntegrator

如果我遗失任何东西,我不确定。我正在使用JBOSS AS 7.0和Hibernate 4.1.8

hibernate jboss7.x hibernate-envers
5个回答
2
投票

这是一个仅限Spring的解决方案,用于条件Envers审计,没有丑陋的META-INF文件夹等。您需要的只是配置类中的bean和CustomEnversEventListener

@Bean
public EventListenerRegistry listenerRegistry(EntityManagerFactory entityManagerFactory) {
    ServiceRegistryImplementor serviceRegistry = entityManagerFactory.unwrap(SessionFactoryImpl.class).getServiceRegistry();

    final EnversService enversService = serviceRegistry.getService(EnversService.class);
    EventListenerRegistry listenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);

    listenerRegistry.setListeners(EventType.POST_UPDATE, new CustomEnversEventListener(enversService));
    return listenerRegistry;
  }

  public class CustomEnversEventListener extends EnversPostUpdateEventListenerImpl {

    CustomEnversEventListener(EnversService enversService) {
      super(enversService);
    }

    @Override
    public void onPostUpdate(PostUpdateEvent event) {

      // custom conditional stuff

      super.onPostUpdate(event);
    }
  }

如果只想自定义一个侦听器,即EnversPostUpdateEventListener,则不需要禁用hibernate.listeners.envers.autoRegister以便让Envers注册另一个侦听器。

然后你可以通过Envers覆盖listenerRegistry.setListeners监听器或者通过listenerRegistry.appendListeners追加


1
投票

尝试将积分器文件放入:

sample.war\WEB-INF\classes\META-INF\services\...

0
投票

也许...

在我的情况下,我使用Maven,我必须在pom.xml中包含以下行:<include>**/*.Integrator</include>,因为该文件未包装在.ear中。

我的pom.xml

<resources>
       <resource>
           <directory>src/main/resources</directory>
           <filtering>true</filtering>
           <includes>
                <include>**/*.xml</include>
               <include>**/*.Integrator</include>
           </includes>
       </resource>
       ...

0
投票

在我的maven项目的文件夹META-INF / services / src / main / resources下创建一个文件org.hibernate.integrator.spi.Integrator(包含我的自定义集成器的限定名称)使我的自定义集成器代码被调用。


0
投票

@ComponentScan(basePackages = {“com.example.demo”},lazyInit = true)

添加lazyInit = true,为我触发了自定义集成器。

© www.soinside.com 2019 - 2024. All rights reserved.