BAEL-20864 Move Spring Boot Custom Starter Module

This commit is contained in:
mikr
2020-01-28 13:26:07 +01:00
parent b168a23f54
commit ed3b7103c8
25 changed files with 15 additions and 16 deletions

View File

@@ -0,0 +1,16 @@
## Spring Boot Custom Starter
This module contains articles about writing [Spring Boot Starters](https://www.baeldung.com/spring-boot-starters).
### Relevant Articles:
- [Creating a Custom Starter with Spring Boot](https://www.baeldung.com/spring-boot-custom-starter)
- **greeter-library**: The sample library that we're creating the starter for.
- **greeter-spring-boot-autoconfigure**: The project containing the auto-configuration for the library.
- **greeter-spring-boot-starter**: The custom starter for the library.
- **greeter-spring-boot-sample-app**: The sample project that uses the custom starter.
- [Multi-Module Project With Spring Boot](https://www.baeldung.com/spring-boot-multiple-modules)

View File

@@ -0,0 +1,19 @@
# Greeter App
This app takes in the user's name and messages for different times of day as configuration parameters and outptus the greeting messge. For example it will take the name **John** and the message for morning time as **Good Morning** and output the message **Hello John, Good Morning**.
## Usage
Create and populate the class `GreetingConfig`, instantiate a `Greeter` using the `GreetingConfig` and use it get greeting messages:
```java
GreetingConfig greetingConfig = new GreetingConfig();
greetingConfig.put(USER_NAME, "World");
greetingConfig.put(MORNING_MESSAGE, "Good Morning");
greetingConfig.put(AFTERNOON_MESSAGE, "Good Afternoon");
greetingConfig.put(EVENING_MESSAGE, "Good Evening");
greetingConfig.put(NIGHT_MESSAGE, "Good Night");
Greeter greeter = new Greeter(greetingConfig);
greeter.greet();
```

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>greeter-library</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>greeter-library</name>
<parent>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>spring-boot-custom-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
</project>

View File

@@ -0,0 +1,35 @@
package com.baeldung.greeter.library;
import static com.baeldung.greeter.library.GreeterConfigParams.*;
import java.time.LocalDateTime;
public class Greeter {
private GreetingConfig greetingConfig;
public Greeter(GreetingConfig greetingConfig) {
this.greetingConfig = greetingConfig;
}
public String greet(LocalDateTime localDateTime) {
String name = greetingConfig.getProperty(USER_NAME);
int hourOfDay = localDateTime.getHour();
if (hourOfDay >= 5 && hourOfDay < 12) {
return String.format("Hello %s, %s", name, greetingConfig.get(MORNING_MESSAGE));
} else if (hourOfDay >= 12 && hourOfDay < 17) {
return String.format("Hello %s, %s", name, greetingConfig.get(AFTERNOON_MESSAGE));
} else if (hourOfDay >= 17 && hourOfDay < 20) {
return String.format("Hello %s, %s", name, greetingConfig.get(EVENING_MESSAGE));
} else {
return String.format("Hello %s, %s", name, greetingConfig.get(NIGHT_MESSAGE));
}
}
public String greet() {
return greet(LocalDateTime.now());
}
}

View File

@@ -0,0 +1,11 @@
package com.baeldung.greeter.library;
public class GreeterConfigParams {
public static final String USER_NAME = "user.name";
public static final String MORNING_MESSAGE = "morning.message";
public static final String AFTERNOON_MESSAGE = "afternoon.message";
public static final String EVENING_MESSAGE = "evening.message";
public static final String NIGHT_MESSAGE = "night.message";
}

View File

@@ -0,0 +1,9 @@
package com.baeldung.greeter.library;
import java.util.Properties;
public class GreetingConfig extends Properties{
private static final long serialVersionUID = 5662570853707247891L;
}

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@@ -0,0 +1,58 @@
package com.baeldung.greeter;
import static com.baeldung.greeter.library.GreeterConfigParams.*;
import static org.junit.Assert.assertEquals;
import java.time.LocalDateTime;
import com.baeldung.greeter.library.Greeter;
import com.baeldung.greeter.library.GreetingConfig;
import org.junit.BeforeClass;
import org.junit.Test;
public class GreeterIntegrationTest {
private static GreetingConfig greetingConfig;
@BeforeClass
public static void initalizeGreetingConfig() {
greetingConfig = new GreetingConfig();
greetingConfig.put(USER_NAME, "World");
greetingConfig.put(MORNING_MESSAGE, "Good Morning");
greetingConfig.put(AFTERNOON_MESSAGE, "Good Afternoon");
greetingConfig.put(EVENING_MESSAGE, "Good Evening");
greetingConfig.put(NIGHT_MESSAGE, "Good Night");
}
@Test
public void givenMorningTime_ifMorningMessage_thenSuccess() {
String expected = "Hello World, Good Morning";
Greeter greeter = new Greeter(greetingConfig);
String actual = greeter.greet(LocalDateTime.of(2017, 3, 1, 6, 0));
assertEquals(expected, actual);
}
@Test
public void givenAfternoonTime_ifAfternoonMessage_thenSuccess() {
String expected = "Hello World, Good Afternoon";
Greeter greeter = new Greeter(greetingConfig);
String actual = greeter.greet(LocalDateTime.of(2017, 3, 1, 13, 0));
assertEquals(expected, actual);
}
@Test
public void givenEveningTime_ifEveningMessage_thenSuccess() {
String expected = "Hello World, Good Evening";
Greeter greeter = new Greeter(greetingConfig);
String actual = greeter.greet(LocalDateTime.of(2017, 3, 1, 19, 0));
assertEquals(expected, actual);
}
@Test
public void givenNightTime_ifNightMessage_thenSuccess() {
String expected = "Hello World, Good Night";
Greeter greeter = new Greeter(greetingConfig);
String actual = greeter.greet(LocalDateTime.of(2017, 3, 1, 21, 0));
assertEquals(expected, actual);
}
}

View File

@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>greeter-spring-boot-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>greeter-spring-boot-autoconfigure</name>
<parent>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>spring-boot-custom-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${spring-boot.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>greeter-library</artifactId>
<version>${greeter.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot.version>1.5.2.RELEASE</spring-boot.version>
<greeter.version>0.0.1-SNAPSHOT</greeter.version>
</properties>
</project>

View File

@@ -0,0 +1,48 @@
package com.baeldung.greeter.autoconfigure;
import static com.baeldung.greeter.library.GreeterConfigParams.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baeldung.greeter.library.Greeter;
import com.baeldung.greeter.library.GreetingConfig;
@Configuration
@ConditionalOnClass(Greeter.class)
@EnableConfigurationProperties(GreeterProperties.class)
public class GreeterAutoConfiguration {
@Autowired
private GreeterProperties greeterProperties;
@Bean
@ConditionalOnMissingBean
public GreetingConfig greeterConfig() {
String userName = greeterProperties.getUserName() == null ? System.getProperty("user.name") : greeterProperties.getUserName();
String morningMessage = greeterProperties.getMorningMessage() == null ? "Good Morning" : greeterProperties.getMorningMessage();
String afternoonMessage = greeterProperties.getAfternoonMessage() == null ? "Good Afternoon" : greeterProperties.getAfternoonMessage();
String eveningMessage = greeterProperties.getEveningMessage() == null ? "Good Evening" : greeterProperties.getEveningMessage();
String nightMessage = greeterProperties.getNightMessage() == null ? "Good Night" : greeterProperties.getNightMessage();
GreetingConfig greetingConfig = new GreetingConfig();
greetingConfig.put(USER_NAME, userName);
greetingConfig.put(MORNING_MESSAGE, morningMessage);
greetingConfig.put(AFTERNOON_MESSAGE, afternoonMessage);
greetingConfig.put(EVENING_MESSAGE, eveningMessage);
greetingConfig.put(NIGHT_MESSAGE, nightMessage);
return greetingConfig;
}
@Bean
@ConditionalOnMissingBean
public Greeter greeter(GreetingConfig greetingConfig) {
return new Greeter(greetingConfig);
}
}

View File

@@ -0,0 +1,54 @@
package com.baeldung.greeter.autoconfigure;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "baeldung.greeter")
public class GreeterProperties {
private String userName;
private String morningMessage;
private String afternoonMessage;
private String eveningMessage;
private String nightMessage;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getMorningMessage() {
return morningMessage;
}
public void setMorningMessage(String morningMessage) {
this.morningMessage = morningMessage;
}
public String getAfternoonMessage() {
return afternoonMessage;
}
public void setAfternoonMessage(String afternoonMessage) {
this.afternoonMessage = afternoonMessage;
}
public String getEveningMessage() {
return eveningMessage;
}
public void setEveningMessage(String eveningMessage) {
this.eveningMessage = eveningMessage;
}
public String getNightMessage() {
return nightMessage;
}
public void setNightMessage(String nightMessage) {
this.nightMessage = nightMessage;
}
}

View File

@@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.baeldung.greeter.autoconfigure.GreeterAutoConfiguration

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@@ -0,0 +1,17 @@
package org.baeldung;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.greeter.autoconfigure.GreeterAutoConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = GreeterAutoConfiguration.class)
public class SpringContextTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}

View File

@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>greeter-spring-boot-sample-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>greeter-spring-boot-sample-app</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../../parent-boot-1</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>greeter-spring-boot-starter</artifactId>
<version>${greeter-starter.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<properties>
<greeter-starter.version>0.0.1-SNAPSHOT</greeter-starter.version>
</properties>
</project>

View File

@@ -0,0 +1,25 @@
package com.baeldung.greeter.sample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.baeldung.greeter.library.Greeter;
@SpringBootApplication
public class GreeterSampleApplication implements CommandLineRunner {
@Autowired
private Greeter greeter;
public static void main(String[] args) {
SpringApplication.run(GreeterSampleApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
String message = greeter.greet();
System.out.println(message);
}
}

View File

@@ -0,0 +1,2 @@
baeldung.greeter.userName=Baeldung
baeldung.greeter.afternoonMessage=Woha\ Afternoon

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@@ -0,0 +1,50 @@
package com.baeldung.greeter.sample;
import static org.junit.Assert.assertEquals;
import java.time.LocalDateTime;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.greeter.library.Greeter;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = GreeterSampleApplication.class)
public class GreeterSampleApplicationIntegrationTest {
@Autowired
private Greeter greeter;
@Test
public void givenMorningTime_ifMorningMessage_thenSuccess() {
String expected = "Hello Baeldung, Good Morning";
String actual = greeter.greet(LocalDateTime.of(2017, 3, 1, 6, 0));
assertEquals(expected, actual);
}
@Test
public void givenAfternoonTime_ifAfternoonMessage_thenSuccess() {
String expected = "Hello Baeldung, Woha Afternoon";
String actual = greeter.greet(LocalDateTime.of(2017, 3, 1, 13, 0));
assertEquals(expected, actual);
}
@Test
public void givenEveningTime_ifEveningMessage_thenSuccess() {
String expected = "Hello Baeldung, Good Evening";
String actual = greeter.greet(LocalDateTime.of(2017, 3, 1, 19, 0));
assertEquals(expected, actual);
}
@Test
public void givenNightTime_ifNightMessage_thenSuccess() {
String expected = "Hello Baeldung, Good Night";
String actual = greeter.greet(LocalDateTime.of(2017, 3, 1, 21, 0));
assertEquals(expected, actual);
}
}

View File

@@ -0,0 +1,17 @@
package org.baeldung;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.greeter.sample.GreeterSampleApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = GreeterSampleApplication.class)
public class SpringContextTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}

View File

@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>greeter-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>greeter-spring-boot-starter</name>
<parent>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>spring-boot-custom-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>greeter-spring-boot-autoconfigure</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>greeter-library</artifactId>
<version>${greeter.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<greeter.version>0.0.1-SNAPSHOT</greeter.version>
<spring-boot.version>1.5.2.RELEASE</spring-boot.version>
</properties>
</project>

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>greeter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>greeter</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../../parent-boot-1</relativePath>
</parent>
</project>

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-custom-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-custom-starter</name>
<packaging>pom</packaging>
<parent>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>spring-boot-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modules>
<module>greeter-library</module>
<module>greeter-spring-boot-autoconfigure</module>
<module>greeter-spring-boot-starter</module>
<module>greeter-spring-boot-sample-app</module>
</modules>
</project>