sync source code with article

This commit is contained in:
amit.pandey
2020-04-12 00:01:36 +05:30
parent 8d5660f7a4
commit 53454088b0
20 changed files with 310 additions and 8 deletions

View File

@@ -7,7 +7,7 @@ import org.springframework.context.annotation.ComponentScan;
import com.baeldung.configurationproperties.ConfigProperties;
@SpringBootApplication
@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class })
@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class, Database.class })
public class ConfigPropertiesDemoApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer())

View File

@@ -0,0 +1,33 @@
package com.baeldung.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "database")
public class Database {
private String url;
private String username;
private String password;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
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;
}
}

View File

@@ -1,4 +1,8 @@
management.endpoints.web.exposure.include=refresh
spring.properties.refreshDelay=1000
spring.config.location=file:extra.properties
spring.main.allow-bean-definition-overriding=true
spring.main.allow-bean-definition-overriding=true
database.url=jdbc:postgresql:/localhost:5432/instance
database.username=foo
database.password=bar

View File

@@ -0,0 +1,30 @@
package com.baeldung.configurationproperties;
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;
import com.baeldung.properties.ConfigPropertiesDemoApplication;
import com.baeldung.properties.Database;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ConfigPropertiesDemoApplication.class)
@TestPropertySource("classpath:application.properties")
public class DatabasePropertiesIntegrationTest {
@Autowired
private Database database;
@Test
public void testDatabaseProperties() {
Assert.assertNotNull(database);
Assert.assertEquals("jdbc:postgresql:/localhost:5432/instance", database.getUrl());
Assert.assertEquals("foo", database.getUsername());
Assert.assertEquals("bar", database.getPassword());
}
}

View File

@@ -1,4 +1,8 @@
management.endpoints.web.exposure.include=refresh
spring.properties.refreshDelay=1000
spring.config.location=file:extra.properties
spring.main.allow-bean-definition-overriding=true
spring.main.allow-bean-definition-overriding=true
database.url=jdbc:postgresql:/localhost:5432/instance
database.username=foo
database.password=bar