Bael 6361: Introduction to Spring Modulith (#13891)

This commit is contained in:
Hamid Reza Sharifi 2023-04-22 23:33:54 +03:30 committed by GitHub
parent b9dbdd954f
commit ad6ed2cb14
9 changed files with 256 additions and 0 deletions

View File

@ -11,6 +11,18 @@
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-modulith-bom</artifactId>
<version>0.5.1</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
@ -87,6 +99,15 @@
<version>2.34.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-modulith-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-modulith-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,21 @@
package com.baeldung.modulith;
import com.baeldung.modulith.product.ProductService;
import com.baeldung.modulith.product.internal.Product;
import org.jobrunr.autoconfigure.JobRunrAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@EnableAsync
@SpringBootApplication
@EnableAutoConfiguration(exclude = { JobRunrAutoConfiguration.class})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args)
.getBean(ProductService.class)
.create(new Product("baeldung", "course", 10));
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.modulith.notification;
import java.util.Date;
public class NotificationDTO {
private Date date;
private String format;
private String productName;
public NotificationDTO(Date date, String format, String productName) {
this.date = date;
this.format = format;
this.productName = productName;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.modulith.notification;
import com.baeldung.modulith.notification.internal.Notification;
import com.baeldung.modulith.notification.internal.NotificationType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.modulith.ApplicationModuleListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class NotificationService {
private static final Logger LOG = LoggerFactory.getLogger(NotificationService.class);
public void createNotification(NotificationDTO notificationDTO) {
Notification notification = toEntity(notificationDTO);
LOG.info("Received notification by module dependency for product {} in date {} by {}.", notification.getProductName()
, notification.getDate(), notification.getFormat());
}
@Async
@ApplicationModuleListener
public void notificationEvent(NotificationDTO event) {
Notification notification = toEntity(event);
LOG.info("Received notification by event for product {} in date {} by {}.", notification.getProductName()
, notification.getDate(), notification.getFormat());
}
private Notification toEntity(NotificationDTO notificationDTO) {
Notification notification = new Notification();
notification.setDate(notificationDTO.getDate());
if (notificationDTO.getFormat().equals("SMS")) {
notification.setFormat(NotificationType.SMS);
}
if (notificationDTO.getFormat().equals("EMAIL")) {
notification.setFormat(NotificationType.EMAIL);
}
notification.setProductName(notificationDTO.getProductName());
return notification;
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.modulith.notification.internal;
import java.util.Date;
public class Notification {
private Date date;
private NotificationType format;
private String productName;
public Notification() {
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public NotificationType getFormat() {
return format;
}
public void setFormat(NotificationType format) {
this.format = format;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.modulith.notification.internal;
public enum NotificationType {
EMAIL, SMS
}

View File

@ -0,0 +1,26 @@
package com.baeldung.modulith.product;
import com.baeldung.modulith.notification.NotificationDTO;
import com.baeldung.modulith.notification.NotificationService;
import com.baeldung.modulith.product.internal.Product;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service
public class ProductService {
private final ApplicationEventPublisher events;
private final NotificationService notificationService;
public ProductService(ApplicationEventPublisher events, NotificationService notificationService) {
this.events = events;
this.notificationService = notificationService;
}
public void create(Product product) {
notificationService.createNotification(new NotificationDTO(new Date(), "SMS", product.getName()));
events.publishEvent(new NotificationDTO(new Date(), "SMS", product.getName()));
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.modulith.product.internal;
public class Product {
private String name;
private String description;
private int price;
public Product(String name, String description, int price) {
this.name = name;
this.description = description;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.modulith;
import org.junit.jupiter.api.Test;
import org.springframework.modulith.core.ApplicationModules;
import org.springframework.modulith.docs.Documenter;
class ApplicationModularityUnitTest {
ApplicationModules modules = ApplicationModules.of(Application.class);
@Test
void verifiesModularStructure() {
modules.verify();
}
@Test
void createModuleDocumentation() {
new Documenter(modules)
.writeDocumentation()
.writeIndividualModulesAsPlantUml();
}
@Test
void createApplicationModuleModel() {
modules.forEach(System.out::println);
}
}