<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rafaëla Phaf on Medium]]></title>
        <description><![CDATA[Stories by Rafaëla Phaf on Medium]]></description>
        <link>https://medium.com/@raffeltjuh?source=rss-2b2cbbf4de60------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*DjhDt2SyGK0iheQ5uyrq3Q@2x.jpeg</url>
            <title>Stories by Rafaëla Phaf on Medium</title>
            <link>https://medium.com/@raffeltjuh?source=rss-2b2cbbf4de60------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 01:25:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@raffeltjuh/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Spring Profiles for the win in your Spring Boot Modulith]]></title>
            <link>https://medium.com/@raffeltjuh/spring-profiles-for-the-win-in-your-spring-boot-modulith-cf2ab78cdb05?source=rss-2b2cbbf4de60------2</link>
            <guid isPermaLink="false">https://medium.com/p/cf2ab78cdb05</guid>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[monolith]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Rafaëla Phaf]]></dc:creator>
            <pubDate>Mon, 30 Sep 2024 18:01:31 GMT</pubDate>
            <atom:updated>2024-09-30T18:01:31.944Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rs6iUYjgyLghdR_g.jpg" /><figcaption>Spring Profiles can help build the modular application you love ❤️</figcaption></figure><h3>Initial Situation</h3><p><a href="https://blog.raffeltje.nl/gradle-modules-with-a-spring-boot-application-a622a66ce90a">After we split our Spring Boot application into Gradle modules</a>, the next step is to optimize how these modules can interact. In the current situation, we have a main module with a main class annotated with @SpringBootApplication, which includes other submodules as a dependency — with other (Spring) components.</p><p>Thanks to Spring Boot, components which need to be autowired, are often already scanned automatically. However, there are some pitfalls there:</p><ul><li>If the annotation @SpringBootApplication is in another root package then the components (to be autowired) of another module, they will not be picked up.</li><li>When you add another submodule, but forget to add it to the main module, it will — of course — not be picked up.</li><li>There is now one main application.yml. If you would like to have separate configuration files for each submodule, you cannot “just” create another application.yml or application.properties in the submodule. Those will not be picked up, because there is already one in the main module.</li></ul><p>With the help of Spring Profiles and adding additional @ComponentScan, we can overcome these pitfalls, so that configurations are isolated and each module operates independently, while still included in the main module.</p><h3>Insight</h3><p>Spring profiles can help to specifically enable or disable specific submodules, based on the defined active profile(s). This can help in building a flexible, modular monolith with features persisting in isolated modules. This approach is interesting for several reasons:</p><ul><li>You can disable specific modules on certain instances of the application, such as resource-intensive processes.</li><li>If you decide to migrate a feature to a microservice (containerized or serverless), the clear isolation of modules makes this transition easier.</li></ul><h3>Implementation by example: location based application</h3><p>We will use an example application to further implement Spring Profiles:</p><blockquote>Imagine a location-based application where users can retrieve information such as news, weather, and location data based on their IP address. Each of these features — location lookup, news, and weather — can be split into separate modules. We also have a module which loads and stores actual news info for the most common countries, so that we do not directly have to call the underlying API.</blockquote><p>With Spring profiles, you can activate independent modules conditionally, ensuring that resource-heavy services like news updates run only when necessary, while lighter services like location lookups remain available.</p><p>Below is a simple diagram of how the modules will be structured for this application:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z-c-q-Em3jWYtOuac8JAHA.png" /><figcaption>Example of how we would like to structure the main module and submodules</figcaption></figure><p><a href="https://github.com/RDBreed/spring-modulith-demo/tree/main/2-spring-profiles">The example project itself is written on Github.</a></p><h3>Implementation</h3><p><strong>1. Create a Main Module</strong></p><p>First, ensure you have a <strong>main module</strong> where the primary Spring Boot application class resides. This module acts as the primary entry point for the entire application and manages which modules should be active based on profiles. It should be kept as much feature logic agnostic as possible, only ensuring features are included for their relevant profile.</p><p><strong>2. Disable automatic component scanning for the submodules</strong></p><p>Ensure if you use @SpringBootApplication above your main class, it is not residing in a root package which is the same as for the other submodules, else all other projects will automatically be scanned (see <a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/ComponentScan.html">@ComponentScan</a>). Another option is to use @SpringBootConfiguration &amp; @EnableAutoConfiguration instead.</p><p><strong>3. Ensure you have all submodules included in your main module</strong></p><p>Include the feature modules as Gradle dependencies in your main module:</p><pre>implementation(project(&quot;:news&quot;))<br>implementation(project(&quot;:location&quot;))<br>implementation(project(&quot;:weather&quot;))<br>implementation(project(&quot;:news-import&quot;))<br>implementation(project(&quot;:user&quot;))</pre><p><strong>4. Define Profiles for Each Module</strong></p><p>Assign a Spring profile for each feature or module. For example:</p><ul><li>location profile for user location services</li><li>news profile for delivering news</li><li>weather profile for weather updates</li><li>news-import profile for news import of updates</li><li>user profile for an aggregation of all location based information available</li></ul><p><strong>5. Add application properties or yaml files for each submodule</strong></p><p>Add application files for each submodule, ending with the profile you chose for that module. When it is environment-specific, add the environment to the name. For our application example, we could have the following files:</p><ul><li><em>location/src/main/resources/application-location-local.yml</em></li><li><em>location/src/main/resources/application-location.yml</em></li><li><em>news/src/main/resources/application-news-local.yml</em></li><li><em>news/src/main/resources/application-news.yml</em></li><li><em>news-import/src/main/resources/application-news-import-local.yml</em></li><li><em>news-import/src/main/resources/application-news-import.yml</em></li><li><em>user/src/main/resources/application-user-local.yml</em></li><li><em>user/src/main/resources/application-user.yml</em></li><li><em>weather/src/main/resources/application-weather-local.yml</em></li><li><em>weather/src/main/resources/application-weather.yml</em></li></ul><p><strong>6. Move properties specific for a module to the new configuration file</strong></p><p>Now you can start moving specific properties from the main application.yml or application.properties to the newly created module configuration files. In doubt whether other modules use the config property as well? Keep it in the main file — at least for now!</p><p><strong>7. Add a configuration class for each defined profile (or module) in your main module</strong></p><p>Use these profiles in combination with a specific ComponentScan, so that the classes of the modules are only scanned when the profile is enabled. By using an existing class as basePackageClass (instead of using a String reference by using basePackage), we ensure the module has indeed been included in the main module.</p><p>Example configurations for each module:</p><pre>@Configuration<br>@Profile(&quot;location&quot;)<br>@ComponentScan(basePackageClasses = LocationService.class)<br>public static class LocationConfiguration {<br>}<br><br>@Configuration<br>@Profile(&quot;news&quot;)<br>@ComponentScan(basePackageClasses = NewsService.class)<br>public static class NewsConfiguration {<br>}<br><br>@Configuration<br>@Profile(&quot;news-import&quot;)<br>@ComponentScan(basePackageClasses = NewsBatchJob.class)<br>public class NewsImportConfiguration {<br>}<br><br>@Configuration<br>@Profile(&quot;user&quot;)<br>@ComponentScan(basePackageClasses = UserService.class)<br>public class UserConfiguration {<br>}<br><br>@Configuration<br>@Profile(&quot;weather&quot;)<br>@ComponentScan(basePackageClasses = WeatherService.class)<br>public class WeatherConfiguration {<br>}</pre><p><strong>8. Activate multiple profiles simultaneously</strong></p><p>You can use profile groupings to activate multiple profiles at once. This can be useful to enable a set of related features based on a specific use case or environment.</p><p>Example configuration in application.yml:</p><pre>spring:<br>  profiles:<br>    group:<br>    # default profile is loaded when no profile is defined during the start of spring boot<br>      default:<br>        - user<br>        - location<br>        - news<br>        - news-import<br>        - weather<br>      local:<br>      # will enable the default and all others defined...<br>        - default<br>        - user-local<br>        - location-local<br>        - news-local<br>        - news-import-local<br>        - weather-local<br>      api-only:<br>        - user<br>        - location<br>        - news<br>        - weather</pre><p>In this example, activating the default profile group enables all modules: user, location, news, news-import, and weather. localwill activate the default &amp; override all locally used settings. Alternatively, the api-only group activates only the api modules and will ignore the news-import module.</p><p><strong>9. Run the application…</strong></p><p>…and use the group profile name of your choice as active profile. “Default” is used when you do not use any active profile.</p><h3>Advantages of this approach</h3><p>By using this approach, there are several advantages:</p><ol><li><strong>Flexibility</strong>: Modules can be toggled dynamically</li><li><strong>Scalability</strong>: Easily add or remove feature without disturbing the core application.</li><li><strong>Resource Management</strong>: Deactivate resource-heavy modules on certain instances to reduce CPU and memory usage, while keeping critical services running.</li><li><strong>Isolation</strong>: Each feature is isolated in its own module, improving maintainability and avoiding configuration conflicts.</li><li><strong>Reusability</strong>: Any module can be extracted and set up as a standalone project if necessary, making it a future-proof design.</li><li><strong>No Configuration File Conflicts</strong>: Profiles ensure that the configuration files for each module do not conflict, since each profile has its own isolated configuration.</li></ol><h3>Considerations</h3><p>While using Spring Profiles per module has its advantages, there are a few important considerations to keep in mind:</p><ul><li><strong>Risk of overriding property values</strong>: If the same configuration property (e.g., server.port) is used across multiple submodules with different values, one may override the other. To prevent this, keep such properties in the main module, consider creating a Gradle Plugin to check for duplicate keys or handle it during application startup. However, that goes beyond the scope of this post.</li><li><strong>ComponentScan limitations</strong>: The @ComponentScan configuration only controls the components within the module itself, <strong>not its (auto-configured) dependencies</strong>. For instance, if submodule A depends on a datasource (like <em>spring-boot-starter-data-jpa</em>) but its profile is disabled, it may still try to auto-configure the datasource. To manage this, there are several ways:<br> – you could use @ImportAutoConfiguration and disable @EnableAutoConfiguration - it will however influence all your autoconfiguration and could lead to many manual steps you need to take<br> – explicitly exclude certain auto-configurations.<br> – do not use these starters (but use for example <em>spring-data-jpa</em> instead and <a href="https://www.baeldung.com/the-persistence-layer-with-spring-and-jpa#javaconfig">configure the beans yourself</a>).<br> – or as a last resort rethink whether to make it possible to exclude the submodule altogether to avoid added complexity.</li><li><strong>Handling shared auto-configurations</strong>: If you have an auto-configured dependency shared across multiple modules (e.g., multiple datasources), it’s safer to disable auto-configuration and configure each instance separately — also with your own defined configuration properties, since those may overlap as well. Otherwise, you risk BeanAlreadyExists exceptions or other unpredictable behavior.</li></ul><h3>Conclusion</h3><p>By implementing Spring profiles into your Gradle modules, you create a highly flexible and configurable application. Profiles help manage configurations across different modules, ensuring that each feature operates in isolation and without configuration conflicts. Furthermore, profiles allow dynamic module activation, enabling resource-heavy modules to be activated only when needed, and ensuring that critical services like public APIs remain responsive under heavy load.</p><p>Each module is encapsulated with its own profile, allowing them to be activated or deactivated dynamically. The main module remains the entry point, ensuring the profiles are managed centrally.</p><p>Ready to make your application more modular and resource-efficient? Start using Spring profiles to manage your Gradle modules effectively, and deliver custom experiences based on technical preferences or environment needs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cf2ab78cdb05" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Gradle modules with a Spring Boot application]]></title>
            <link>https://medium.com/@raffeltjuh/gradle-modules-with-a-spring-boot-application-a622a66ce90a?source=rss-2b2cbbf4de60------2</link>
            <guid isPermaLink="false">https://medium.com/p/a622a66ce90a</guid>
            <category><![CDATA[gradle]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Rafaëla Phaf]]></dc:creator>
            <pubDate>Mon, 23 Sep 2024 19:19:25 GMT</pubDate>
            <atom:updated>2024-09-24T16:02:13.696Z</atom:updated>
            <content:encoded><![CDATA[<h2>Get your Spring Boot monolith modularized with Gradle modules</h2><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jm3EXhjFxAeuo-MoICcRuQ.png" /><figcaption>Gradle modules with Spring Boot</figcaption></figure><p>As modern applications grow in complexity, finding the balance between monolithic and modular architectures becomes increasingly important. In this series, we explore how to leverage Gradle modules, Spring Boot profiles, and other tools to build a scalable, modular monolith. Whether you’re migrating an existing application or starting from scratch, these posts will guide you through some practices and techniques to enhance maintainability, flexibility, and performance for your application.</p><p>In this first post we will focus on using Gradle modules.</p><h3>Initial situation</h3><p>When managing a large Spring Boot monolithic project with a single Gradle build file, it can become unmanageable as the application grows.</p><p>Splitting the application into modules can help to organize your code, to isolate dependencies and to simplify maintaining your application.</p><h3>Gradle modules</h3><p>To break down your code into smaller, focussed components, you can use Gradle modules.</p><p>Each module will have its own build file with its own dependencies and source directories, which can potentially reduce complexity and improve the structure of your codebase, as well as enable parallel builds (and tests!) when splits are carefully thought out.</p><p>It can be applied for both new projects and when refactoring existing large monoliths.</p><h3>Implementation</h3><p>A prerequisite is that the project already uses Gradle. Using Maven? Submodules are also possible but out of scope of this post.</p><p>For practice you can start with a Spring Boot project through <a href="https://start.spring.io/">Spring Initializr</a> (or IntelliJ or any other tool).</p><p>To start creating a module, you can use the following steps:</p><p>1. Add a subdirectory under your root project (where your existing build.gradle is) and include a new <em>build.gradle.kts</em> (or <em>build.gradle</em>)</p><p>2. Ensure you have at least plugins defined in the newly created build file to add the <a href="https://docs.gradle.org/current/userguide/java_plugin.html">Gradle Java plugin</a>:</p><pre>plugins {<br>    java<br>}</pre><p>3. Configure <em>settings.gradle.kts</em> (or <em>settings.gradle</em>) in the root of the project (or create the file if it does not exist):</p><pre>include(&quot;submodule-name&quot;)</pre><p>4. Add the desired (Spring Boot or any other) dependencies to the newly created submodule.</p><p>5. Set up source directories like src/main/java and src/test/java in your newly created submodule and add the code you want have in the submodule.</p><p>6. In the root <em>build.gradle.kts</em> (or <em>build.gradle</em>), you can add a dependency of the created submodule:</p><pre>dependencies { <br>    implementation(project(&quot;:submodule-name&quot;)) <br>}</pre><p>7. Run <em>./gradlew clean build</em> to verify if all still runs correctly</p><p>8. Also ensure you disable the Bootjar task for the submodule when you add a Spring Boot starter.</p><p>9. Move or create code — which can be used or injected through Dependency Injection (Autowiring) with Spring Boot in the original root module.</p><p>By repeating above steps, you can create multiple submodules to your needs.</p><h4>Best practices</h4><p>Some important notes need to be mentioned for maintaining and creating submodules:</p><ul><li><strong>Low coupling, high cohesion</strong>: each module should have as much as a single responsibility and minimal interdependencies as possible.</li><li><strong>Encapsulation</strong>: ensure that only what is necessary is exposed.</li><li><strong>Circular dependencies</strong>: ensure you are not creating circular dependencies between your modules. When finding yourself in such a situation, figure which parts are needed to be centralized and make a dedicated submodule which exposes this as an internal library.</li><li><strong>Shared versions of dependencies</strong>: when you want to ensure all modules use the same version of some dependency, you could start using a <a href="https://docs.gradle.org/current/userguide/platforms.html">version catalog</a>.</li><li><strong>Incremental migration</strong>: start with isolated domains or new features, so that you can gradually modularize the application.</li><li><strong>Parallel builds</strong>: To improve build times, you can enable parallel builds by adding build.parallel=true to a gradle.properties file in your root directory. But, this will only really work when you have a clean organized structure, so keeping the other best practices will be important.</li><li><strong>Testing</strong>: keep unit and integration tests as much as possible within each module (it will help with parallel builds!).</li><li><strong>Versioning</strong>: when using modules across multiple projects, consider using versioning (this is not in the focus of this post — it will add complexity nevertheless).</li></ul><p>If you want to have code by example, take a look at <a href="https://github.com/RDBreed/spring-modulith-demo/tree/main/1-gradle-modules">this repository</a>. This repository will become more important in other posts of the serie!</p><h3>Conclusion</h3><p>By modularizing your project with Gradle, you can enhance maintainability, reduce build complexity, and gain flexibility in development and deployment.</p><p>Gradle Modules are a powerful way to manage growing Spring Boot projects. Whether you’re starting fresh or refactoring an existing application, modularization can help improving your code organization and simplifying development.</p><p>Take the first step towards a modular project today! Break down your monolith, embrace Gradle Modules and enjoy a more streamlined development process. Need help? Please let me know!</p><p><em>Next post (to be posted): Spring Profiles for the win in your Spring Boot Modulith</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a622a66ce90a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>