initial commit of Hexagonal Architecture

This commit is contained in:
ajay74984 2020-02-12 16:06:47 +01:00
parent 63d394d697
commit 91da58e880
12 changed files with 218 additions and 0 deletions

View File

@ -49,6 +49,7 @@
<module>spring-boot-springdoc</module>
<module>spring-boot-testing</module>
<module>spring-boot-vue</module>
<module>spring-boot-hexagonal</module> <!-- Need to find a better place for this -->
</modules>
</project>

View File

@ -0,0 +1,4 @@
## HEXAGONAL Architecture
This module contains articles about Hexagonal Architecture implemented via Spring Boot.

View File

@ -0,0 +1,76 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-hexagonal</artifactId>
<name>spring-boot-hexagonal</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
</dependencies>
<build>
<finalName>spring-boot-hexagonal</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -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);
}
}

View File

@ -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<String, Document> documentMap = new HashMap<>();
@Override
public void storeDocument(Document document) {
documentMap.put(document.getId(), document);
}
@Override
public Document findDocumentById(String id) {
return documentMap.get(id);
}
}

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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() {
}
}