This is an example of how to implement multiple chains of responsibility with COR Bean Processor Spring Boot Starter.
The main objective of this project is to retrieve configuration files of different types - XML, JSON or YAML - from different sources: File system, Java classpath or HTTP server.
Even though both chains extends ChainLink
interface, the library knows that there are two different chains to be assembled due to the class informed on the interface parameter.
ContentVerifier
interface extends COR Bean Processor Spring Boot Starter ChainLink<T>
interface passing a self-reference as parameter. It also contains the entry method used to verify the content type.
public interface ContentVerifier extends ChainLink<ContentVerifier> {
ContentType verify(String content);
}
AbstractContentVerifier
is an abstract class that implements ContentVerifier
interface. It also implements verify
demanded by ContentVerifier
and setNext
demanded by ChainLink
. The latter is implemented using Lombok @Setter
annotation.
public abstract class AbstractContentVerifier implements ContentVerifier {
@Setter
private ContentVerifier next;
@Override
public ContentType verify(String content) { /* Full logic can be checked in the code implementation. */ }
}
ContentVerifierService
encapsulates the components required to verify a content type.
It contains a verify
method which invokes the first link verify
method, starting the chain analysis.
public class ContentVerifierService {
private final ContentVerifier firstLink;
public ContentType verify(String content) {
return contentVerifier.verify(content);
}
}
COR Bean Processor Spring Boot Starter will be responsible to chain all ContentVerifier
implementations and inject the first chain link on firstLink
field.
ConfigurationRetriever
interface extends COR Bean Processor Spring Boot Starter ChainLink<T>
interface passing a self-reference as parameter. It also contains the entry method definition to retrieve the configuration.
public interface ConfigurationRetriever extends ChainLink<ConfigurationRetriever> {
Map<String, String> retrieve(URI uri);
}
AbstractConfigurationRetriever
is an abstract class that implements ConfigurationRetriever
interface. It also implements retrieve
demanded by ConfigurationRetriever
and setNext
demanded by ChainLink
. The latter is implemented using Lombok @Setter
annotation.
public abstract class AbstractConfigurationRetriever implements ConfigurationRetriever {
@Setter
private ConfigurationRetriever next;
@Override
public Map<String, String> retrieve(URI uri) { /* Full logic can be checked in the code implementation. */ }
}
ConfigurationService
encapsulates the components required to retrieve configuration.
It contains a retrieve
method which invokes the first link retrieve
method, starting the chain analysis.
public class ConfigurationService {
private final ConfigurationRetriever firstLink;
public Map<String, String> retrieve(URI uri) {
return configurationRetriever.retrieve(uri);
}
}
COR Bean Processor Spring Boot Starter will be responsible to chain all ConfigurationRetriever
implementations and inject the first chain link on firstLink
field.
The complete integration with Spring Boot can be checked on ContentVerifierServiceIT
and ConfigurationServiceIT
classes. They are integration tests and can be executed through an IDE like IntelliJ IDEA, Eclipse or Microsoft Visual Studio Code.
Alternatively, the tests can also be executed through a terminal running the following command on this module root directory.
mvn test-compile failsafe:integration-test