-
Notifications
You must be signed in to change notification settings - Fork 141
Custom repository implementations
Spring-Data provides hooks to add customized methods to a repository bean to allow for any kind of custom handling that can not be achieved via the regular find
methods: Custom implementations for Spring Data repositories
spring-data-dynamodb
is not different to any other provider - thus here is an example:
First we need to declare the custom methods that needs to be implemented:
public interface DeviceValueAdditionRepository {
String fancyCustomMethod();
}
Next add the 'custom interface' to the regular interface (the one which is implemented by spring-data-dynamodb
magic).
Everything that applies to regular DynamoDB repositories is still true & valid here.
@EnableScan
public interface DeviceValueRepository
extends CrudRepository<DeviceValue, DeviceValueKey>,
DeviceValueAdditionRepository {
List<DeviceValue> findAll();
}
As last step the custom methods needs to be implemented.
It is important to name the class implementing the custom interface the same as the interface with a Impl
suffix
// Most important this class has to be named like the interface with an 'Impl' suffix
// https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.single-repository-behavior
public class DeviceValueRepositoryImpl implements DeviceValueAdditionRepository {
// Inject everything you want as this is created like a normal bean
@Autowired
DynamoDBTemplate dynamoDBTemplate;
@Override
public String fancyCustomMethod() {
// custom code here
DeviceValue dv = dynamoDBTemplate.load(DeviceValue.class, "42");
if (dv == null) {
return "Not found";
} else {
return dv.getTag();
}
}
}
Afterwards the new, extended repository can be used as any other repository:
@Autowired
private DeviceValueRepository repository;
public void callingMethod() {
// Call a standard method from the repository
repository.findAll();
// Call the custom-implemented method
repository.fancyCustomMethod();
}
Spring takes care of blending all implementations properly together and exposing everything under the one repository interface.