From 91da58e880b411d0a99000d555359c41d5af59f2 Mon Sep 17 00:00:00 2001 From: ajay74984 <51405689+ajay74984@users.noreply.github.com> Date: Wed, 12 Feb 2020 16:06:47 +0100 Subject: [PATCH] initial commit of Hexagonal Architecture --- spring-boot-modules/pom.xml | 1 + .../spring-boot-hexagonal/README.md | 4 + .../spring-boot-hexagonal/pom.xml | 76 +++++++++++++++++++ .../main/java/com/baeldung/Application.java | 13 ++++ .../adapter/DocumentRepositoryImpl.java | 24 ++++++ .../baeldung/adapter/DocumentRestAdapter.java | 24 ++++++ .../java/com/baeldung/domain/Document.java | 13 ++++ .../java/com/baeldung/port/DocumentRepo.java | 11 +++ .../com/baeldung/port/DocumentService.java | 11 +++ .../port/impl/DocumentServiceImpl.java | 24 ++++++ .../src/main/resources/application.properties | 1 + .../java/com/baeldung/ApplicationTests.java | 16 ++++ 12 files changed, 218 insertions(+) create mode 100644 spring-boot-modules/spring-boot-hexagonal/README.md create mode 100644 spring-boot-modules/spring-boot-hexagonal/pom.xml create mode 100644 spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/Application.java create mode 100644 spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/adapter/DocumentRepositoryImpl.java create mode 100644 spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/adapter/DocumentRestAdapter.java create mode 100644 spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/domain/Document.java create mode 100644 spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/port/DocumentRepo.java create mode 100644 spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/port/DocumentService.java create mode 100644 spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/port/impl/DocumentServiceImpl.java create mode 100644 spring-boot-modules/spring-boot-hexagonal/src/main/resources/application.properties create mode 100644 spring-boot-modules/spring-boot-hexagonal/src/test/java/com/baeldung/ApplicationTests.java diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index 234c52f638..3a503bf837 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -49,6 +49,7 @@ spring-boot-springdoc spring-boot-testing spring-boot-vue + spring-boot-hexagonal diff --git a/spring-boot-modules/spring-boot-hexagonal/README.md b/spring-boot-modules/spring-boot-hexagonal/README.md new file mode 100644 index 0000000000..44728b185e --- /dev/null +++ b/spring-boot-modules/spring-boot-hexagonal/README.md @@ -0,0 +1,4 @@ +## HEXAGONAL Architecture + +This module contains articles about Hexagonal Architecture implemented via Spring Boot. + diff --git a/spring-boot-modules/spring-boot-hexagonal/pom.xml b/spring-boot-modules/spring-boot-hexagonal/pom.xml new file mode 100644 index 0000000000..4d6109d24f --- /dev/null +++ b/spring-boot-modules/spring-boot-hexagonal/pom.xml @@ -0,0 +1,76 @@ + + + 4.0.0 + spring-boot-hexagonal + spring-boot-hexagonal + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + + + + org.projectlombok + lombok + 1.18.12 + + + + + + spring-boot-hexagonal + + + src/main/resources + true + + + + + org.springframework.boot + spring-boot-maven-plugin + + exec + + + + org.apache.maven.plugins + maven-assembly-plugin + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + + + + + + UTF-8 + + + diff --git a/spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/Application.java b/spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/Application.java new file mode 100644 index 0000000000..17be2b0d79 --- /dev/null +++ b/spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/Application.java @@ -0,0 +1,13 @@ +package com.ajay.documentservice; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DocumentserviceApplication { + + public static void main(String[] args) { + SpringApplication.run(DocumentserviceApplication.class, args); + } + +} diff --git a/spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/adapter/DocumentRepositoryImpl.java b/spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/adapter/DocumentRepositoryImpl.java new file mode 100644 index 0000000000..e22ccb5cbf --- /dev/null +++ b/spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/adapter/DocumentRepositoryImpl.java @@ -0,0 +1,24 @@ +package com.ajay.documentservice.adapter; + +import com.ajay.documentservice.domain.Document; +import com.ajay.documentservice.port.DocumentRepo; +import org.springframework.stereotype.Repository; + +import java.util.HashMap; +import java.util.Map; + +@Repository +public class DocumentRepositoryImpl implements DocumentRepo { + + private Map documentMap = new HashMap<>(); + + @Override + public void storeDocument(Document document) { + documentMap.put(document.getId(), document); + } + + @Override + public Document findDocumentById(String id) { + return documentMap.get(id); + } +} diff --git a/spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/adapter/DocumentRestAdapter.java b/spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/adapter/DocumentRestAdapter.java new file mode 100644 index 0000000000..2a3cffb0e8 --- /dev/null +++ b/spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/adapter/DocumentRestAdapter.java @@ -0,0 +1,24 @@ +package com.ajay.documentservice.adapter; + +import com.ajay.documentservice.domain.Document; +import com.ajay.documentservice.port.DocumentService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/doc") +public class DocumentRestAdapter { + @Autowired + private DocumentService documentService; + + @PostMapping + public void createDocument(@RequestBody Document document) { + documentService.createDocument(document); + } + + @GetMapping("/{id}") + public Document findById(@PathVariable String id) { + return documentService.findById(id); + } + +} diff --git a/spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/domain/Document.java b/spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/domain/Document.java new file mode 100644 index 0000000000..14ab4900e2 --- /dev/null +++ b/spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/domain/Document.java @@ -0,0 +1,13 @@ +package com.ajay.documentservice.domain; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; + +@Data +@AllArgsConstructor +@Builder +public class Document { + private String id; + private String data; +} diff --git a/spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/port/DocumentRepo.java b/spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/port/DocumentRepo.java new file mode 100644 index 0000000000..2e18dd92ee --- /dev/null +++ b/spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/port/DocumentRepo.java @@ -0,0 +1,11 @@ +package com.ajay.documentservice.port; + + +import com.ajay.documentservice.domain.Document; + +public interface DocumentRepo { + void storeDocument(Document document); + + Document findDocumentById(String id); +} + diff --git a/spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/port/DocumentService.java b/spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/port/DocumentService.java new file mode 100644 index 0000000000..6ed9edaf2c --- /dev/null +++ b/spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/port/DocumentService.java @@ -0,0 +1,11 @@ +package com.ajay.documentservice.port; + +import com.ajay.documentservice.domain.Document; + +public interface DocumentService { + + void createDocument(Document document); + + Document findById(String id); + +} diff --git a/spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/port/impl/DocumentServiceImpl.java b/spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/port/impl/DocumentServiceImpl.java new file mode 100644 index 0000000000..2549ca834c --- /dev/null +++ b/spring-boot-modules/spring-boot-hexagonal/src/main/java/com/baeldung/port/impl/DocumentServiceImpl.java @@ -0,0 +1,24 @@ +package com.ajay.documentservice.port.impl; + +import com.ajay.documentservice.domain.Document; +import com.ajay.documentservice.port.DocumentRepo; +import com.ajay.documentservice.port.DocumentService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class DocumentServiceImpl implements DocumentService { + + @Autowired + private DocumentRepo documentRepo; + + @Override + public void createDocument(Document document) { + documentRepo.storeDocument(document); + } + + @Override + public Document findById(String id) { + return documentRepo.findDocumentById(id); + } +} diff --git a/spring-boot-modules/spring-boot-hexagonal/src/main/resources/application.properties b/spring-boot-modules/spring-boot-hexagonal/src/main/resources/application.properties new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/spring-boot-modules/spring-boot-hexagonal/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/spring-boot-modules/spring-boot-hexagonal/src/test/java/com/baeldung/ApplicationTests.java b/spring-boot-modules/spring-boot-hexagonal/src/test/java/com/baeldung/ApplicationTests.java new file mode 100644 index 0000000000..ce3b754df3 --- /dev/null +++ b/spring-boot-modules/spring-boot-hexagonal/src/test/java/com/baeldung/ApplicationTests.java @@ -0,0 +1,16 @@ +package com.ajay.documentservice.documentservice; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@SpringBootTest +@RunWith(SpringRunner.class) +class ApplicationTests { + + @Test + public void contextLoads() { + } + +}