Skip to content

Add example for Spring Modulith #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Apr 6, 2025
Merged

Add example for Spring Modulith #222

merged 11 commits into from
Apr 6, 2025

Conversation

rashidi
Copy link
Owner

@rashidi rashidi commented Apr 6, 2025

No description provided.

@rashidi rashidi added documentation Improvements or additions to documentation enhancement New feature or request labels Apr 6, 2025
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @rashidi, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request introduces an example project demonstrating the use of Spring Modulith for building modular monolithic applications. The example is a simple student course management system divided into three modules: Course, Student, and Subscription. The modules interact through events to ensure loose coupling. The PR also includes tests to verify the architecture and generate documentation.

Highlights

  • Project Structure: The project is structured into three modules: Course, Student, and Subscription, each with its own domain entities, repositories, and services.
  • Module Interactions: Modules communicate through events such as CourseEnded and StudentInactivated, which are handled by the Subscription module to cancel relevant subscriptions.
  • Testing: Includes ModuleTests to verify the modulith architecture and generate documentation, as well as module-specific tests for event publishing and consumption.
  • Documentation: Adds documentation in AsciiDoc format, including PlantUML diagrams, to describe the module structure and interactions.

Changelog

Click here to see the changelog
  • modulith/.gitattributes
    • Added file attributes for gradlew, bat, and jar files to handle line endings.
  • modulith/.gitignore
    • Added common IDE and build system files to the .gitignore.
  • modulith/README.adoc
    • Created a comprehensive README file explaining the Spring Modulith example, including project structure, module interactions, key components, testing strategies, and generated documentation.
  • modulith/build.gradle
    • Configured the Gradle build file with Spring Boot and Spring Modulith dependencies, including testcontainers for integration tests. Spring Modulith version is set to 1.3.4 and Spring Boot to 3.4.4.
  • modulith/docs/all-docs.adoc
    • Created the main documentation file that includes module-specific documentation and PlantUML diagrams.
  • modulith/docs/components.puml
    • Added a PlantUML component diagram illustrating the modules within the ModulithApplication.
  • modulith/docs/module-course.adoc
    • Created documentation for the Course module, detailing its base package, Spring components (services, repositories, event listeners), and published events.
  • modulith/docs/module-course.puml
    • Added a PlantUML diagram for the Course module.
  • modulith/docs/module-student.adoc
    • Created documentation for the Student module, detailing its base package, Spring components, and published events.
  • modulith/docs/module-student.puml
    • Added a PlantUML diagram for the Student module.
  • modulith/docs/module-subscription.adoc
    • Created documentation for the Subscription module, detailing its base package, Spring components, and the events it listens to.
  • modulith/docs/module-subscription.puml
    • Added a PlantUML diagram for the Subscription module, showing its relationship with the Course and Student modules.
  • modulith/settings.gradle
    • Included the 'modulith' project in the settings.
  • modulith/src/main/java/zin/rashidi/boot/modulith/ModulithApplication.java
    • Created the main application class for the Spring Modulith example.
  • modulith/src/main/java/zin/rashidi/boot/modulith/course/Course.java
    • Defined the Course entity with fields for id, name, and status.
  • modulith/src/main/java/zin/rashidi/boot/modulith/course/CourseEnded.java
    • Defined the CourseEnded domain event.
  • modulith/src/main/java/zin/rashidi/boot/modulith/course/CourseEventsConfiguration.java
    • Configured the event listener for the Course module to publish a CourseEnded event when a course's status is set to ENDED.
  • modulith/src/main/java/zin/rashidi/boot/modulith/course/CourseManagement.java
    • Created the CourseManagement service to handle course updates.
  • modulith/src/main/java/zin/rashidi/boot/modulith/course/CourseRepository.java
    • Defined the CourseRepository interface for CRUD operations on Course entities.
  • modulith/src/main/java/zin/rashidi/boot/modulith/student/Student.java
    • Defined the Student entity with fields for id, name, and status.
  • modulith/src/main/java/zin/rashidi/boot/modulith/student/StudentEventsConfiguration.java
    • Configured the event listener for the Student module to publish a StudentInactivated event when a student's status is set to INACTIVE.
  • modulith/src/main/java/zin/rashidi/boot/modulith/student/StudentInactivated.java
    • Defined the StudentInactivated domain event.
  • modulith/src/main/java/zin/rashidi/boot/modulith/student/StudentManagement.java
    • Created the StudentManagement service to handle student status updates.
  • modulith/src/main/java/zin/rashidi/boot/modulith/student/StudentRepository.java
    • Defined the StudentRepository interface for CRUD operations on Student entities.
  • modulith/src/main/java/zin/rashidi/boot/modulith/subscription/Subscription.java
    • Defined the Subscription entity with fields for studentId, courseId, and status.
  • modulith/src/main/java/zin/rashidi/boot/modulith/subscription/SubscriptionManagement.java
    • Created the SubscriptionManagement component to listen for CourseEnded and StudentInactivated events and cancel subscriptions accordingly.
  • modulith/src/main/java/zin/rashidi/boot/modulith/subscription/SubscriptionRepository.java
    • Defined the SubscriptionRepository interface with custom methods to cancel subscriptions by courseId and studentId.
  • modulith/src/main/resources/application.properties
    • Configured the application name and enabled schema initialization for Spring Modulith events.
  • modulith/src/test/java/zin/rashidi/boot/modulith/ModuleTests.java
    • Created tests to verify the modulith architecture and generate documentation.
  • modulith/src/test/java/zin/rashidi/boot/modulith/TestModulithApplication.java
    • Created a test application to run the ModulithApplication with Testcontainers.
  • modulith/src/test/java/zin/rashidi/boot/modulith/TestcontainersConfiguration.java
    • Configured Testcontainers for PostgreSQL integration tests.
  • modulith/src/test/java/zin/rashidi/boot/modulith/course/CourseEndedTests.java
    • Added integration tests to verify that subscriptions are cancelled when a course ends.
  • modulith/src/test/java/zin/rashidi/boot/modulith/course/CourseManagementTests.java
    • Added application module tests to verify that a CourseEnded event is triggered when a course is ended.
  • modulith/src/test/java/zin/rashidi/boot/modulith/student/StudentInactivatedTests.java
    • Added integration tests to verify that subscriptions are cancelled when a student is inactivated.
  • modulith/src/test/java/zin/rashidi/boot/modulith/student/StudentManagementTests.java
    • Added application module tests to verify that a StudentInactivated event is triggered when a student is inactivated.
  • modulith/src/test/java/zin/rashidi/boot/modulith/subscription/SubscriptionManagementTests.java
    • Added application module tests to verify that subscriptions are cancelled when CourseEnded or StudentInactivated events are published.
  • modulith/src/test/resources/schema-data.sql
    • Added initial data for courses, students, and subscriptions for testing purposes.
  • modulith/src/test/resources/schema.sql
    • Created the database schema for courses, students, and subscriptions.
  • settings.gradle
    • The settings.gradle file was modified to include the modulith project.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


A module's tale,
Events whisper, boundaries frail,
Loose coupling thrives.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a Spring Modulith example, demonstrating how to build modular monolithic applications. The example covers project structure, module interactions, key components, and testing. Overall, the example is well-structured and provides a good starting point for understanding Spring Modulith. However, there are a few areas that could be improved for clarity and completeness.

Summary of Findings

  • README: Missing diagram in documentation generation section: The README mentions generating PlantUML diagrams, but it doesn't explicitly state where these diagrams are used or how they enhance the documentation. Clarifying this would improve the user's understanding of the documentation generation process.
  • README: Add description of schema.sql and schema-data.sql: The README does not describe the purpose of the schema.sql and schema-data.sql files used in the test configuration. Adding a brief explanation would help users understand the test setup.
  • README: Clarify the purpose of TestModulithApplication: The README does not explain the purpose of the TestModulithApplication class. Adding a brief description would help users understand its role in the testing setup.

Merge Readiness

The pull request is a valuable addition to the repository, providing a practical example of Spring Modulith. Addressing the comments would further enhance the example's clarity and completeness. I am unable to approve this pull request, and recommend that others review and approve this code before merging.

Comment on lines +20 to +23
@Transactional
public Student inactive(Student student) {
return students.save(student.status(INACTIVE));
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The @Transactional annotation is missing from the class level. It is recommended to add it to ensure transactional behavior for all methods in the class.

Also, the method inactive is already transactional, so the return value does not need to be returned.

Suggested change
@Transactional
public Student inactive(Student student) {
return students.save(student.status(INACTIVE));
}
@Transactional
public void inactive(Student student) {
students.save(student.status(INACTIVE));
}

@rashidi rashidi merged commit 24fc7e0 into master Apr 6, 2025
4 checks passed
@rashidi rashidi deleted the modulith branch April 6, 2025 08:29
Copy link

sonarqubecloud bot commented Apr 6, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant