#441 - Polishing.

Reuse provided configuration for NamedParameterJdbcOperations. Use listeners and EntityCallbacks. Remove explicit dependency versions.
This commit is contained in:
Mark Paluch
2019-08-13 14:49:13 +02:00
parent db66b173b8
commit a44f93a9bc
4 changed files with 35 additions and 42 deletions

View File

@@ -1,5 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-data-jdbc-basics</artifactId>
@@ -13,20 +14,17 @@
<name>Spring Data JDBC - Basic usage examples</name>
<description>Sample project demonstrating Spring Data JDBC features</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
</project>

View File

@@ -16,28 +16,16 @@
package example.springdata.jdbc.basics.simpleentity;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.jdbc.core.DataAccessStrategy;
import org.springframework.data.jdbc.core.DefaultDataAccessStrategy;
import org.springframework.data.jdbc.core.JdbcAggregateOperations;
import org.springframework.data.jdbc.core.JdbcAggregateTemplate;
import org.springframework.data.jdbc.core.SqlGeneratorSource;
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
import org.springframework.data.jdbc.repository.config.JdbcConfiguration;
import org.springframework.data.relational.core.conversion.RelationalConverter;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.event.BeforeSaveEvent;
import org.springframework.data.relational.core.mapping.event.BeforeSaveCallback;
import org.springframework.data.relational.core.mapping.event.RelationalEvent;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
/**
* Contains infrastructure necessary for creating repositories and two listeners.
* Contains infrastructure necessary for creating repositories, listeners and EntityCallbacks.
* <p>
* Not that a listener may change an entity without any problem.
*
@@ -46,9 +34,11 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
*/
@Configuration
@EnableJdbcRepositories
@Import(JdbcConfiguration.class)
public class CategoryConfiguration {
public class CategoryConfiguration extends AbstractJdbcConfiguration {
/**
* @return {@link ApplicationListener} for {@link RelationalEvent}s.
*/
@Bean
public ApplicationListener<?> loggingListener() {
@@ -59,24 +49,17 @@ public class CategoryConfiguration {
};
}
/**
* @return {@link BeforeSaveCallback} for {@link Category}.
*/
@Bean
public ApplicationListener<BeforeSaveEvent> timeStampingSaveTime() {
public BeforeSaveCallback<Category> timeStampingSaveTime() {
return event -> {
return (entity, aggregateChange) -> {
Object entity = event.getEntity();
entity.timeStamp();
if (entity instanceof Category) {
Category category = (Category) entity;
category.timeStamp();
}
return entity;
};
}
// the following bean definitions are only necessary for providing JdbcAggregateOperations for injection into WithInsertImpl.
@Bean
public NamedParameterJdbcOperations namedParameterJdbcOperations(JdbcOperations operations) {
return new NamedParameterJdbcTemplate(operations);
}
}

View File

@@ -16,8 +16,17 @@
package example.springdata.jdbc.basics.simpleentity;
/**
* Fragment interface providing the {@link WithInsert#insert(Object)} signature.
*
* @author Jens Schauder
*/
public interface WithInsert<T> {
/**
* Custom insert method.
*
* @param t
* @return
*/
T insert(T t);
}

View File

@@ -15,17 +15,20 @@
*/
package example.springdata.jdbc.basics.simpleentity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jdbc.core.JdbcAggregateTemplate;
import org.springframework.data.relational.core.mapping.event.WithId;
/**
* Fragment implementation providing the {@link WithInsert#insert(Object)} implementation.
*
* @author Jens Schauder
*/
public class WithInsertImpl<T> implements WithInsert<T> {
@Autowired
JdbcAggregateTemplate template;
private final JdbcAggregateTemplate template;
public WithInsertImpl(JdbcAggregateTemplate template) {
this.template = template;
}
@Override
public T insert(T t) {