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
@@ -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;
}
}
@@ -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();
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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);}
}
@@ -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;
}
}
@@ -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;
}
}