Bael 2997 spring boot configuration processor (#7376)
* BAEL-2997: Adding spring boot configuration processor dependency. * BAEL-2997: Added examples for spring boot configuration processor, simple and nested properties. * BAEL-2997: Fixed the identation * BAEL-2997: Formatted the added dependencies * BAEL-2997: Added version for configuration-processor dependency.
This commit is contained in:
parent
8b2b894bbd
commit
da152e53a1
@ -38,6 +38,19 @@
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<version>2.1.6.RELEASE</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hsqldb</groupId>
|
||||
<artifactId>hsqldb</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -50,12 +63,10 @@
|
||||
</resources>
|
||||
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
@ -0,0 +1,15 @@
|
||||
package com.baeldung.autoconfiguration.annotationprocessor;
|
||||
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
import com.baeldung.autoconfiguration.MySQLAutoconfiguration;
|
||||
|
||||
@EnableAutoConfiguration(exclude = { MySQLAutoconfiguration.class})
|
||||
@ComponentScan(basePackageClasses = {DatabaseProperties.class})
|
||||
public class AnnotationProcessorApplication {
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(AnnotationProcessorApplication.class).run();
|
||||
}
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
package com.baeldung.autoconfiguration.annotationprocessor;
|
||||
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "database")
|
||||
public class DatabaseProperties {
|
||||
|
||||
public static class Server {
|
||||
|
||||
/**
|
||||
* The IP of the database server
|
||||
*/
|
||||
private String ip;
|
||||
|
||||
/**
|
||||
* The Port of the database server.
|
||||
* The Default value is 443.
|
||||
* The allowed values are in the range 400-4000.
|
||||
*/
|
||||
@Min(400)
|
||||
@Max(800)
|
||||
private int port = 443;
|
||||
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
public void setIp(String ip) {
|
||||
this.ip = ip;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
}
|
||||
|
||||
private String username;
|
||||
private String password;
|
||||
private Server server;
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public Server getServer() {
|
||||
return server;
|
||||
}
|
||||
|
||||
public void setServer(Server server) {
|
||||
this.server = server;
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.baeldung.autoconfiguration.annotationprocessor;
|
||||
|
||||
import org.junit.Assert;
|
||||
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.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = AnnotationProcessorApplication.class)
|
||||
@TestPropertySource("classpath:databaseproperties-test.properties")
|
||||
public class DatabasePropertiesIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private DatabaseProperties databaseProperties;
|
||||
|
||||
@Test
|
||||
public void whenSimplePropertyQueriedThenReturnsPropertyValue() throws Exception {
|
||||
Assert.assertEquals("Incorrectly bound Username property", "baeldung", databaseProperties.getUsername());
|
||||
Assert.assertEquals("Incorrectly bound Password property", "password", databaseProperties.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNestedPropertyQueriedThenReturnsPropertyValue() throws Exception {
|
||||
Assert.assertEquals("Incorrectly bound Server IP nested property", "127.0.0.1", databaseProperties.getServer().getIp());
|
||||
Assert.assertEquals("Incorrectly bound Server Port nested property", 3306, databaseProperties.getServer().getPort());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
#Simple Properties
|
||||
database.username=baeldung
|
||||
database.password=password
|
||||
|
||||
#Nested Properties
|
||||
database.server.ip=127.0.0.1
|
||||
database.server.port=3306
|
Loading…
x
Reference in New Issue
Block a user