Spring Boot Custom Components

Spring by Kazimir Malevich

What Is Spring Boot

Build Systems

Gradle

plugins {
id 'org.springframework.boot' version '2.4.0'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
}
plugins {
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
}
dependencyManagement {
dependencies {
dependency 'org.springframework:spring-core:5.3.0'
}
}

Maven

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.5.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Features Overview

Spring Boot Application

org.example.myshop
+- MyShopApplication
+- catalog
+- delivery
+- order
...

Starters

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
}

Auto-Configuration

Application Properties

# application.ymlspring:
datasource:
url: jdbc:postgresql://db:5432/test
username: admin
password: secret

Custom Components

Code Structure

delivery/		-- comp. root
+- domain/ -- domain API
+- events/ -- events API
+- jdbc/ -- JDBC impl
+- rest/ -- Restful API
+- spring-boot-starter/ -- starter
+- pom.xml
+- build.gradle
\- settings.gradle
myshop/
+- application/
+- catalog/
+- delivery/
+- order/
...
myshop/
+- catalog/
| +- application/
| +- domain/
| +- jdbc/
| +- rest/
| \- spring-boot-starter/
+- delivery/
| +- application/
| +- domain/
| +- events/
| +- jdbc/
| +- rest/
| \- spring-boot-starter/
+- order/
| +- application/
| +- domain/
| +- events/
| +- jdbc/
| +- rest/
| \- spring-boot-starter/
...
// rest/build.gradleimplementation 'org.springframework:spring-web'
// spring-boot-starter/build.gradleimplementation 'org.springframework.boot:spring-boot-starter-web'

Java Packages

delivery/
+- domain/
| \- src/main/java/
| \- org.example.myshop.delivery
| \- ○DeliveryService.java
+- events/
| \- src/main/java/
| \- org.example.myshop.delivery
| \- ○DeliveryDispatched.java
+- jdbc/
| \- src/main/java/
| \- org.example.myshop.delivery.jdbc
| \- ●DeliveryServiceJdbc.java
+- rest/
| \- src/main/java/
| \- org.example.myshop.delivery.rest
| \- ●DeliveryRestController.java
\- spring-boot-starter/
\- src/main/java/
\- org.example.myshop.delivery
\- jdbc
\- ●DeliveryJdbcConfig.java
\- rest
\- ●DeliveryRestConfig.java

Custom Spring Boot Starter

org.example.myshop:delivery-spring-boot-starter

Auto-Configurations

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.example.myshop.delivery.DeliveryConfiguration
@Configuration
@Import(JdbcDeliveryConfiguration.class)
class DeliveryConfiguration {
...
}

Dependencies

implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.example.myshop:catalog-spring-boot-starter'
implementation 'org.example.myshop:delivery-spring-boot-starter'
implementation 'org.example.myshop:order-spring-boot-starter'
...

Configuration Properties

@ConfigurationProperties(
prefix = "myshop.delivery")
@Setter
@Getter
class DeliveryProperties {
private String cargoName;
private String dateFormat;
}
@Configuration
@EnableConfigurationProperties(
DeliveryProperties.class)
class DeliveryConfiguration {
@Bean
DeliveryService deliveryService(
DeliveryProperties properties
) {
return new DeliveryServiceImpl(
properties.getCargoName(),
properties.getDateFormat()
);
}
}
# application.ymlmyshop:
delivery:
cargo-name: PPL
date-format: yyyy-mm-dd
order:
prefix-id: OrderID
MYSHOP_DELIVERY_CARGO_NAME=DHL

Conclusion

Example

Links

--

--

--

Software developer and occasional blogger: https://blog.ttulka.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Create an Up and Down arrows Table in Tableau Desktop

Record Player Switch

Hadoop on Azure (Infrastructure as a service)

Darwinia Highlight December | Monthly Recap

Manual vs. Automated Software Testing: Balancing Between the Perks and Downsides

Let’s create a REST API with Django Rest Framework !!

Optimize the DevOps stream with the Value Stream Mapping (VMS)

Enabling Reproducible Builds in Sia

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tomas Tulka

Tomas Tulka

Software developer and occasional blogger: https://blog.ttulka.com

More from Medium

Matrix URIs, their semantics and usage in Java RESTful Services

Spring Boot Framework

Visitor Design Pattern in Java