DATAJDBC-204 - Polishing.
Extracted ApplicationContext and Repository construction into method in order to make the actual test stand out more. Split a test in two. JavaDoc. Code formatting. Removed access modifiers in tests.
This commit is contained in:
@@ -19,30 +19,22 @@ import org.springframework.beans.factory.ObjectFactory;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.data.auditing.AuditingHandler;
|
||||
import org.springframework.data.jdbc.mapping.event.BeforeSaveEvent;
|
||||
import org.springframework.data.jdbc.repository.config.EnableJdbcAuditing;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Spring JDBC event listener to capture auditing information on persisting and updating entities.
|
||||
* <p>
|
||||
* You can enable this class just a matter of activating auditing using {@link org.springframework.data.jdbc.repository.config.EnableJdbcAuditing} in your Spring config:
|
||||
*
|
||||
* <pre>
|
||||
* @Configuration
|
||||
* @EnableJdbcRepositories
|
||||
* @EnableJdbcAuditing
|
||||
* class JdbcRepositoryConfig {
|
||||
* }
|
||||
* </pre>
|
||||
* An instance of this class gets registered when you apply {@link EnableJdbcAuditing} to your Spring config.
|
||||
*
|
||||
* @author Kazuki Shimizu
|
||||
* @see org.springframework.data.jdbc.repository.config.EnableJdbcAuditing
|
||||
* @see EnableJdbcAuditing
|
||||
* @since 1.0
|
||||
*/
|
||||
public class JdbcAuditingEventListener implements ApplicationListener<BeforeSaveEvent> {
|
||||
|
||||
@Nullable
|
||||
private AuditingHandler handler;
|
||||
@Nullable private AuditingHandler handler;
|
||||
|
||||
/**
|
||||
* Configures the {@link AuditingHandler} to be used to set the current auditor on the domain types touched.
|
||||
@@ -50,18 +42,24 @@ public class JdbcAuditingEventListener implements ApplicationListener<BeforeSave
|
||||
* @param auditingHandler must not be {@literal null}.
|
||||
*/
|
||||
public void setAuditingHandler(ObjectFactory<AuditingHandler> auditingHandler) {
|
||||
|
||||
Assert.notNull(auditingHandler, "AuditingHandler must not be null!");
|
||||
|
||||
this.handler = auditingHandler.getObject();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @param event a notification event for indicating before save
|
||||
*/
|
||||
@Override
|
||||
public void onApplicationEvent(BeforeSaveEvent event) {
|
||||
|
||||
if (handler != null) {
|
||||
|
||||
event.getOptionalEntity().ifPresent(entity -> {
|
||||
|
||||
if (event.getId().getOptionalValue().isPresent()) {
|
||||
handler.markModified(entity);
|
||||
} else {
|
||||
@@ -70,5 +68,4 @@ public class JdbcAuditingEventListener implements ApplicationListener<BeforeSave
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -29,22 +29,6 @@ import org.springframework.data.domain.AuditorAware;
|
||||
/**
|
||||
* Annotation to enable auditing in JDBC via annotation configuration.
|
||||
*
|
||||
* If you use the auditing feature, you should be configures beans of Spring Data JDBC
|
||||
* using {@link org.springframework.data.jdbc.repository.config.EnableJdbcRepositories} in your Spring config:
|
||||
*
|
||||
* <pre>
|
||||
* @Configuration
|
||||
* @EnableJdbcRepositories
|
||||
* @EnableJdbcAuditing
|
||||
* class JdbcRepositoryConfig {
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* <p>
|
||||
* Note: This feature cannot use to a entity that implements {@link org.springframework.data.domain.Auditable}
|
||||
* because the Spring Data JDBC does not support an {@link java.util.Optional} property yet.
|
||||
* </p>
|
||||
*
|
||||
* @see EnableJdbcRepositories
|
||||
* @author Kazuki Shimizu
|
||||
* @since 1.0
|
||||
|
||||
@@ -27,7 +27,8 @@ import org.springframework.data.config.ParsingUtils;
|
||||
import org.springframework.data.jdbc.domain.support.JdbcAuditingEventListener;
|
||||
|
||||
/**
|
||||
* {@link ImportBeanDefinitionRegistrar} to enable {@link EnableJdbcAuditing} annotation.
|
||||
* {@link ImportBeanDefinitionRegistrar} which registers additional beans in order to enable auditing via the
|
||||
* {@link EnableJdbcAuditing} annotation.
|
||||
*
|
||||
* @see EnableJdbcAuditing
|
||||
* @author Kazuki Shimizu
|
||||
@@ -37,8 +38,9 @@ class JdbcAuditingRegistrar extends AuditingBeanDefinitionRegistrarSupport {
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @return return the {@link EnableJdbcAuditing}
|
||||
* @see org.springframework.data.auditing.config.AuditingBeanDefinitionRegistrarSupport#getAnnotation()
|
||||
* @see AuditingBeanDefinitionRegistrarSupport#getAnnotation()
|
||||
*/
|
||||
@Override
|
||||
protected Class<? extends Annotation> getAnnotation() {
|
||||
@@ -47,8 +49,9 @@ class JdbcAuditingRegistrar extends AuditingBeanDefinitionRegistrarSupport {
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @return return "{@literal jdbcAuditingHandler}"
|
||||
* @see org.springframework.data.auditing.config.AuditingBeanDefinitionRegistrarSupport#getAuditingHandlerBeanName()
|
||||
* @see AuditingBeanDefinitionRegistrarSupport#getAuditingHandlerBeanName()
|
||||
*/
|
||||
@Override
|
||||
protected String getAuditingHandlerBeanName() {
|
||||
@@ -61,22 +64,25 @@ class JdbcAuditingRegistrar extends AuditingBeanDefinitionRegistrarSupport {
|
||||
*/
|
||||
@Override
|
||||
protected BeanDefinitionBuilder getAuditHandlerBeanDefinitionBuilder(AuditingConfiguration configuration) {
|
||||
|
||||
BeanDefinitionBuilder builder = super.getAuditHandlerBeanDefinitionBuilder(configuration);
|
||||
return builder.addConstructorArgReference("jdbcMappingContext");
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the bean definition of {@link JdbcAuditingEventListener}.
|
||||
* {@inheritDoc}
|
||||
* @see org.springframework.data.auditing.config.AuditingBeanDefinitionRegistrarSupport#registerAuditListenerBeanDefinition(BeanDefinition, BeanDefinitionRegistry)
|
||||
* Register the bean definition of {@link JdbcAuditingEventListener}. {@inheritDoc}
|
||||
*
|
||||
* @see AuditingBeanDefinitionRegistrarSupport#registerAuditListenerBeanDefinition(BeanDefinition,
|
||||
* BeanDefinitionRegistry)
|
||||
*/
|
||||
@Override
|
||||
protected void registerAuditListenerBeanDefinition(BeanDefinition auditingHandlerDefinition,
|
||||
BeanDefinitionRegistry registry) {
|
||||
BeanDefinitionRegistry registry) {
|
||||
|
||||
Class<?> listenerClass = JdbcAuditingEventListener.class;
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(listenerClass);
|
||||
builder.addPropertyValue("auditingHandler",
|
||||
ParsingUtils.getObjectFactoryBeanDefinition(getAuditingHandlerBeanName(), null));
|
||||
ParsingUtils.getObjectFactoryBeanDefinition(getAuditingHandlerBeanName(), null));
|
||||
registerInfrastructureBeanWithId(builder.getRawBeanDefinition(), listenerClass.getName(), registry);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user