Add missing code snippets (#9228)

This commit is contained in:
sasam0320 2020-05-06 05:28:28 +02:00 committed by GitHub
parent 50208fb013
commit fb5b157ce4
22 changed files with 268 additions and 10 deletions

View File

@ -128,6 +128,7 @@
<httpcore.version>4.4.11</httpcore.version>
<resource.delimiter>@</resource.delimiter>
<configuration-processor.version>2.2.4.RELEASE</configuration-processor.version>
<start-class>com.baeldung.buildproperties.Application</start-class>
</properties>
</project>

View File

@ -0,0 +1,21 @@
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

@ -6,16 +6,23 @@ import org.springframework.stereotype.*;
@Component
public class PropertyBeanInjection {
private final CustomProperties customProperties;
private CustomProperties customProperties;
PropertyBeanInjection(@Autowired 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

@ -0,0 +1,36 @@
package com.baeldung.configurationproperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "database")
public class Database {
String url;
String username;
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

@ -0,0 +1,23 @@
package com.baeldung.configurationproperties;
import org.springframework.core.env.Environment;
import org.springframework.beans.factory.annotation.*;
import org.springframework.context.annotation.*;
@Configuration
public class DatabaseConfig {
@Autowired private Environment env;
@Bean(name="dataSource")
public Database dataSource() {
Database dataSource = new Database();
dataSource.setUrl(env.getProperty("jdbc.url"));
dataSource.setUsername(env.getProperty("database.username"));
dataSource.setPassword(env.getProperty("database.password"));
return dataSource;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.configurationproperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackageClasses = {Database.class,DatabaseConfig.class})
public class DatabaseConfigPropertiesApp{
public static void main(String[]args) {SpringApplication.run(DatabaseConfigPropertiesApp.class,args);}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.properties.spring;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.*;
@Configuration
public class PropertyPlaceholderConfig {
public PropertyPlaceholderConfig(){
super();
}
@Bean
public static PropertyPlaceholderConfigurer properties() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
Resource[] resources = new ClassPathResource[]{ new ClassPathResource("foo.properties") };
ppc.setLocations( resources );
ppc.setIgnoreUnresolvablePlaceholders( true );
return ppc;
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.properties.spring;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.*;
@Configuration
public class PropertySourcesPlaceholderConfig{
public PropertySourcesPlaceholderConfig(){
super();
}
@Bean
public static PropertySourcesPlaceholderConfigurer properties(){
PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
Resource[] resources = new ClassPathResource[]{ new ClassPathResource("foo.properties") };
pspc.setLocations(resources);
pspc.setIgnoreUnresolvablePlaceholders(true);
return pspc;
}
}

View File

@ -3,6 +3,3 @@ spring.properties.refreshDelay=1000
spring.config.location=file:extra.properties
spring.main.allow-bean-definition-overriding=true
database.url=jdbc:postgresql:/localhost:5432/instance
database.username=foo
database.password=bar

View File

@ -7,10 +7,14 @@
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
>
<context:property-placeholder location="classpath:foo.properties,classpath:bar.properties"/>
<context:property-placeholder location="classpath:foo.properties,classpath:bar.properties, classpath:database.properties"/>
<bean id="componentInXmlUsingProperties" class="com.baeldung.properties.core.ComponentInXmlUsingProperties">
<constructor-arg value="${key.something}"/>
</bean>
<bean id="dataSource" class="com.baeldung.configurationproperties.Database">
<property name="url" value="${jdbc.url}" />
</bean>
</beans>

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"
>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:foo.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:foo.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
</beans>

View File

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

View File

@ -0,0 +1,4 @@
database.url=jdbc:postgresql:/localhost:5432/instance
database.username=foo
database.password=bar
jdbc.url=jdbc:postgresql:/localhost:5432

View File

@ -0,0 +1,5 @@
database:
url: jdbc:postresql:/localhost:5432/instance
username: foo
password: bar
secret: foo

View File

@ -15,6 +15,11 @@ 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());

View File

@ -4,6 +4,7 @@ import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
@ -13,13 +14,17 @@ import com.baeldung.properties.AdditionalProperties;
import com.baeldung.properties.ConfigPropertiesDemoApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ConfigPropertiesDemoApplication.class)
@TestPropertySource("classpath:configprops-test.properties")
@SpringBootTest(classes = {ConfigPropertiesDemoApplication.class, DatabaseConfigPropertiesApp.class})
@TestPropertySource(locations = {"classpath:configprops-test.properties", "classpath:database-test.properties"})
public class ConfigPropertiesIntegrationTest {
@Autowired
private ConfigProperties properties;
@Autowired
@Qualifier("dataSource")
private Database databaseProperties;
@Autowired
private AdditionalProperties additionalProperties;
@ -53,4 +58,11 @@ public class ConfigPropertiesIntegrationTest {
Assert.assertTrue(additionalProperties.getUnit().equals("km"));
Assert.assertTrue(additionalProperties.getMax() == 100);
}
@Test
public void whenDatabasePropertyQueriedthenReturnsProperty() {
Assert.assertTrue(databaseProperties.getUrl().equals("jdbc:postgresql:/localhost:5432"));
Assert.assertTrue(databaseProperties.getUsername().equals("foo"));
Assert.assertTrue(databaseProperties.getPassword().equals("bar"));
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.properties.multiple;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import com.baeldung.properties.spring.PropertyPlaceholderConfig;
import com.baeldung.properties.spring.PropertySourcesPlaceholderConfig;
import static org.assertj.core.api.Assertions.assertThat;
@SpringJUnitConfig({PropertyPlaceholderConfig.class, PropertySourcesPlaceholderConfig.class})
public class MultiplePlaceholdersJavaConfigIntegrationTest {
@Value("${key.something}")
private String something;
@Test
public void whenReadInjectedValues_thenGetCorrectValues() {
assertThat(something).isEqualTo("val");
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.properties.multiple;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@SpringJUnitConfig(locations = "classpath:configForPropertyPlaceholderBeans.xml")
public class MultiplePlaceholdersXmlConfigIntegrationTest {
@Value("${foo}")
private String something;
@Value("${key.something}")
private String something2;
@Test
public void whenReadInjectedValues_thenGetCorrectValues() {
assertThat(something).isEqualTo("bar");
assertThat(something2).isEqualTo("val");
}
}

View File

@ -13,9 +13,12 @@ public class MultiplePropertiesXmlConfigIntegrationTest {
@Value("${key.something2}") private String something2;
@Value("${jdbc.url}") private String jdbcUrl;
@Test
public void whenReadInjectedValues_thenGetCorrectValues() {
assertThat(something).isEqualTo("val");
assertThat(something2).isEqualTo("val2");
assertThat(jdbcUrl).isEqualTo("jdbc:postgresql:/localhost:5432");
}
}

View File

@ -10,6 +10,8 @@ import com.baeldung.properties.basic.PropertiesWithXmlIntegrationTest;
import com.baeldung.properties.external.ExternalPropertiesWithJavaIntegrationTest;
import com.baeldung.properties.external.ExternalPropertiesWithMultipleXmlsIntegrationTest;
import com.baeldung.properties.external.ExternalPropertiesWithXmlManualTest;
import com.baeldung.properties.multiple.MultiplePropertiesXmlConfigIntegrationTest;
import com.baeldung.properties.multiple.MultiplePlaceholdersXmlConfigIntegrationTest;
@RunWith(Suite.class)
@SuiteClasses({ //@formatter:off
@ -17,8 +19,8 @@ import com.baeldung.properties.external.ExternalPropertiesWithXmlManualTest;
ExternalPropertiesWithJavaIntegrationTest.class,
ExternalPropertiesWithMultipleXmlsIntegrationTest.class,
ExternalPropertiesWithXmlManualTest.class,
ExtendedPropertiesWithJavaIntegrationTest.class,
PropertiesWithMultipleXmlsIntegrationTest.class,
ExtendedPropertiesWithJavaIntegrationTest.class, MultiplePropertiesXmlConfigIntegrationTest.class,
PropertiesWithMultipleXmlsIntegrationTest.class, MultiplePlaceholdersXmlConfigIntegrationTest.class
})// @formatter:on
public final class IntegrationTestSuite {
//

View File

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

View File

@ -0,0 +1,3 @@
jdbc.url=jdbc:postgresql:/localhost:5432
database.username=foo
database.password=bar