Skip to content

Commit 54d2452

Browse files
vivekkr12pivovarit
authored andcommitted
add modules for spring boot custom starter BAEL-762 (eugenp#1661)
1 parent 709857b commit 54d2452

File tree

16 files changed

+532
-0
lines changed

16 files changed

+532
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>greeter-spring-boot-autoconfigure</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
9+
<properties>
10+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11+
<spring-boot.version>1.5.2.RELEASE</spring-boot.version>
12+
<greeter.version>0.0.1-SNAPSHOT</greeter.version>
13+
</properties>
14+
15+
<dependencies>
16+
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot</artifactId>
20+
<version>${spring-boot.version}</version>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-autoconfigure</artifactId>
26+
<version>${spring-boot.version}</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-configuration-processor</artifactId>
32+
<version>${spring-boot.version}</version>
33+
<optional>true</optional>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>com.baeldung</groupId>
38+
<artifactId>greeter</artifactId>
39+
<version>${greeter.version}</version>
40+
<optional>true</optional>
41+
</dependency>
42+
43+
</dependencies>
44+
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-compiler-plugin</artifactId>
50+
<version>3.5.1</version>
51+
<configuration>
52+
<source>1.8</source>
53+
<target>1.8</target>
54+
</configuration>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
59+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.greeter.autoconfigure;
2+
3+
import static com.baeldung.greeter.GreeterConfigParams.*;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
7+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
8+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
12+
import com.baeldung.greeter.Greeter;
13+
import com.baeldung.greeter.GreetingConfig;
14+
15+
@Configuration
16+
@ConditionalOnClass(Greeter.class)
17+
@EnableConfigurationProperties(GreeterProperties.class)
18+
public class GreeterAutoConfiguration {
19+
20+
@Autowired
21+
private GreeterProperties greeterProperties;
22+
23+
@Bean
24+
@ConditionalOnMissingBean
25+
public GreetingConfig greeterConfig() {
26+
27+
String userName = greeterProperties.getUserName() == null ? System.getProperty("user.name") : greeterProperties.getUserName();
28+
String morningMessage = greeterProperties.getMorningMessage() == null ? "Good Morning" : greeterProperties.getMorningMessage();
29+
String afternoonMessage = greeterProperties.getAfternoonMessage() == null ? "Good Afternoon" : greeterProperties.getAfternoonMessage();
30+
String eveningMessage = greeterProperties.getEveningMessage() == null ? "Good Evening" : greeterProperties.getEveningMessage();
31+
String nightMessage = greeterProperties.getNightMessage() == null ? "Good Night" : greeterProperties.getNightMessage();
32+
33+
GreetingConfig greetingConfig = new GreetingConfig();
34+
greetingConfig.put(USER_NAME, userName);
35+
greetingConfig.put(MORNING_MESSAGE, morningMessage);
36+
greetingConfig.put(AFTERNOON_MESSAGE, afternoonMessage);
37+
greetingConfig.put(EVENING_MESSAGE, eveningMessage);
38+
greetingConfig.put(NIGHT_MESSAGE, nightMessage);
39+
return greetingConfig;
40+
}
41+
42+
@Bean
43+
@ConditionalOnMissingBean
44+
public Greeter greeter(GreetingConfig greetingConfig) {
45+
return new Greeter(greetingConfig);
46+
}
47+
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.greeter.autoconfigure;
2+
3+
import org.springframework.boot.context.properties.ConfigurationProperties;
4+
5+
@ConfigurationProperties(prefix = "baeldung.greeter")
6+
public class GreeterProperties {
7+
8+
private String userName;
9+
private String morningMessage;
10+
private String afternoonMessage;
11+
private String eveningMessage;
12+
private String nightMessage;
13+
14+
public String getUserName() {
15+
return userName;
16+
}
17+
18+
public void setUserName(String userName) {
19+
this.userName = userName;
20+
}
21+
22+
public String getMorningMessage() {
23+
return morningMessage;
24+
}
25+
26+
public void setMorningMessage(String morningMessage) {
27+
this.morningMessage = morningMessage;
28+
}
29+
30+
public String getAfternoonMessage() {
31+
return afternoonMessage;
32+
}
33+
34+
public void setAfternoonMessage(String afternoonMessage) {
35+
this.afternoonMessage = afternoonMessage;
36+
}
37+
38+
public String getEveningMessage() {
39+
return eveningMessage;
40+
}
41+
42+
public void setEveningMessage(String eveningMessage) {
43+
this.eveningMessage = eveningMessage;
44+
}
45+
46+
public String getNightMessage() {
47+
return nightMessage;
48+
}
49+
50+
public void setNightMessage(String nightMessage) {
51+
this.nightMessage = nightMessage;
52+
}
53+
54+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
2+
com.baeldung.greeter.autoconfigure.GreeterAutoConfiguration
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>greeter-spring-boot-sample-app</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
9+
<parent>
10+
<groupId>org.springframework.boot</groupId>
11+
<artifactId>spring-boot-starter-parent</artifactId>
12+
<version>1.5.2.RELEASE</version>
13+
<relativePath></relativePath>
14+
</parent>
15+
16+
<properties>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
<java.version>1.8</java.version>
19+
<greeter-starter.version>0.0.1-SNAPSHOT</greeter-starter.version>
20+
</properties>
21+
22+
<dependencies>
23+
24+
<dependency>
25+
<groupId>com.baeldung</groupId>
26+
<artifactId>greeter-spring-boot-starter</artifactId>
27+
<version>${greeter-starter.version}</version>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-test</artifactId>
33+
<scope>test</scope>
34+
</dependency>
35+
36+
</dependencies>
37+
38+
39+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.greeter.sample;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.boot.CommandLineRunner;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
8+
import com.baeldung.greeter.Greeter;
9+
10+
@SpringBootApplication
11+
public class GreeterSampleApplication implements CommandLineRunner {
12+
13+
@Autowired
14+
private Greeter greeter;
15+
16+
public static void main(String[] args) {
17+
SpringApplication.run(GreeterSampleApplication.class, args);
18+
}
19+
20+
@Override
21+
public void run(String... args) throws Exception {
22+
String message = greeter.greet();
23+
System.out.println(message);
24+
}
25+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
baeldung.greeter.userName=Baeldung
2+
baeldung.greeter.afternoonMessage=Woha\ Afternoon
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.greeter.sample;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.time.LocalDateTime;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
12+
13+
import com.baeldung.greeter.Greeter;
14+
15+
@RunWith(SpringJUnit4ClassRunner.class)
16+
@SpringBootTest(classes = GreeterSampleApplication.class)
17+
public class GreeterSampleApplicationTest {
18+
19+
@Autowired
20+
private Greeter greeter;
21+
22+
@Test
23+
public void givenMorningTime_ifMorningMessage_thenSuccess() {
24+
String expected = "Hello Baeldung, Good Morning";
25+
String actual = greeter.greet(LocalDateTime.of(2017, 3, 1, 6, 0));
26+
assertEquals(expected, actual);
27+
}
28+
29+
@Test
30+
public void givenAfternoonTime_ifAfternoonMessage_thenSuccess() {
31+
String expected = "Hello Baeldung, Woha Afternoon";
32+
String actual = greeter.greet(LocalDateTime.of(2017, 3, 1, 13, 0));
33+
assertEquals(expected, actual);
34+
}
35+
36+
@Test
37+
public void givenEveningTime_ifEveningMessage_thenSuccess() {
38+
String expected = "Hello Baeldung, Good Evening";
39+
String actual = greeter.greet(LocalDateTime.of(2017, 3, 1, 19, 0));
40+
assertEquals(expected, actual);
41+
}
42+
43+
@Test
44+
public void givenNightTime_ifNightMessage_thenSuccess() {
45+
String expected = "Hello Baeldung, Good Night";
46+
String actual = greeter.greet(LocalDateTime.of(2017, 3, 1, 21, 0));
47+
assertEquals(expected, actual);
48+
}
49+
50+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>greeter-spring-boot-starter</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
9+
<properties>
10+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11+
<greeter.version>0.0.1-SNAPSHOT</greeter.version>
12+
<spring-boot.version>1.5.2.RELEASE</spring-boot.version>
13+
</properties>
14+
15+
<dependencies>
16+
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter</artifactId>
20+
<version>${spring-boot.version}</version>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>com.baeldung</groupId>
25+
<artifactId>greeter-spring-boot-autoconfigure</artifactId>
26+
<version>${project.version}</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>com.baeldung</groupId>
31+
<artifactId>greeter</artifactId>
32+
<version>${greeter.version}</version>
33+
</dependency>
34+
35+
</dependencies>
36+
37+
<build>
38+
<plugins>
39+
<plugin>
40+
<groupId>org.apache.maven.plugins</groupId>
41+
<artifactId>maven-compiler-plugin</artifactId>
42+
<version>3.5.1</version>
43+
<configuration>
44+
<source>1.8</source>
45+
<target>1.8</target>
46+
</configuration>
47+
</plugin>
48+
</plugins>
49+
</build>
50+
51+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Greeter App
2+
3+
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**.
4+
5+
## Usage
6+
7+
Create and populate the class `GreetingConfig`, instantiate a `Greeter` using the `GreetingConfig` and use it get greeting messages:
8+
9+
```java
10+
GreetingConfig greetingConfig = new GreetingConfig();
11+
greetingConfig.put(USER_NAME, "World");
12+
greetingConfig.put(MORNING_MESSAGE, "Good Morning");
13+
greetingConfig.put(AFTERNOON_MESSAGE, "Good Afternoon");
14+
greetingConfig.put(EVENING_MESSAGE, "Good Evening");
15+
greetingConfig.put(NIGHT_MESSAGE, "Good Night");
16+
17+
Greeter greeter = new Greeter(greetingConfig);
18+
greeter.greet();
19+
```

0 commit comments

Comments
 (0)