JAVA-8356: split or move spring-boot-properties

This commit is contained in:
chaos2418
2022-01-14 13:33:39 +05:30
parent 5414c92546
commit 54c7d1ff7d
9 changed files with 18 additions and 17 deletions

View File

@@ -1,37 +0,0 @@
package com.baeldung.configuration.processor;
import org.springframework.boot.context.properties.*;
import org.springframework.context.annotation.*;
@Configuration
@ConfigurationProperties(prefix = "com.baeldung")
public class CustomProperties {
/**
* The url to connect to.
*/
String url;
/**
* The time to wait for the connection.
*/
private int timeoutInMilliSeconds = 1000;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getTimeoutInMilliSeconds() {
return timeoutInMilliSeconds;
}
public void setTimeoutInMilliSeconds(int timeoutInMilliSeconds) {
this.timeoutInMilliSeconds = timeoutInMilliSeconds;
}
}

View File

@@ -1,12 +0,0 @@
package com.baeldung.configuration.processor;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

View File

@@ -1,21 +0,0 @@
package com.baeldung.configuration.processor;
import org.springframework.boot.context.properties.*;
import org.springframework.context.annotation.*;
import org.springframework.beans.factory.annotation.*;
@Configuration
@ConfigurationProperties(prefix = "com.baeldung")
public class JdbcProperties {
@Value("${jdbc.url:jdbc:postgresql:/localhost:5432}")
private String jdbcUrl;
public String getJdbcUrl() {
return jdbcUrl;
}
public void setJdbcUrl(String jdbcUrl) {
this.jdbcUrl = jdbcUrl;
}
}

View File

@@ -1,29 +0,0 @@
package com.baeldung.configuration.processor;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;
@Component
public class PropertyBeanInjection {
private CustomProperties customProperties;
private JdbcProperties jdbcProperties;
PropertyBeanInjection(@Autowired CustomProperties customProperties, @Autowired JdbcProperties jdbcProperties) {
this.customProperties = customProperties;
this.jdbcProperties = jdbcProperties;
}
String getUrl() {
return customProperties.getUrl();
}
String getJdbcUrl() {
return jdbcProperties.getJdbcUrl();
}
int getTimeoutInMilliseconds() {
return customProperties.getTimeoutInMilliSeconds();
}
}

View File

@@ -1,3 +0,0 @@
com.baeldung.url=www.abc.test.com
com.baeldung.jdbc.url=
com.baeldung.timeout-in-milli-seconds=2000

View File

@@ -1,29 +0,0 @@
package com.baeldung.configuration.processor;
import org.junit.jupiter.api.*;
import org.junit.runner.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.boot.test.context.*;
import org.springframework.test.context.*;
import org.springframework.test.context.junit4.*;
@RunWith(SpringRunner.class)
@TestPropertySource("/configuration-processor.properties")
@SpringBootTest(classes = DemoApplication.class)
class PropertyBeanInjectionUnitTest {
@Autowired
private PropertyBeanInjection propertyBeanInjection;
@Test
void checkThatJdbcPropertiesHaveTheCorrectValueFromPropertiesFile() {
Assertions.assertEquals("jdbc:postgresql:/localhost:5432", propertyBeanInjection.getJdbcUrl());
}
@Test
void checkThatCustomPropertiesHaveTheCorrectValueFromPropertiesFile() {
Assertions.assertEquals("www.abc.test.com", propertyBeanInjection.getUrl());
Assertions.assertEquals(2000, propertyBeanInjection.getTimeoutInMilliseconds());
}
}

View File

@@ -1,3 +0,0 @@
com.baeldung.url=www.abc.test.com
com.baeldung.jdbc.url=
com.baeldung.timeout-in-milli-seconds=2000