Add missing code snippets (#9228)
This commit is contained in:
+21
@@ -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;
|
||||
}
|
||||
}
|
||||
+9
-2
@@ -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();
|
||||
}
|
||||
|
||||
+36
@@ -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;
|
||||
}
|
||||
}
|
||||
+23
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
+13
@@ -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);}
|
||||
|
||||
}
|
||||
+23
@@ -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;
|
||||
}
|
||||
}
|
||||
+24
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
+5
-1
@@ -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>
|
||||
+23
@@ -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>
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
com.baeldung.url=www.abc.test.com
|
||||
com.baeldung.jdbc.url=
|
||||
com.baeldung.timeout-in-milli-seconds=2000
|
||||
@@ -0,0 +1,4 @@
|
||||
database.url=jdbc:postgresql:/localhost:5432/instance
|
||||
database.username=foo
|
||||
database.password=bar
|
||||
jdbc.url=jdbc:postgresql:/localhost:5432
|
||||
@@ -0,0 +1,5 @@
|
||||
database:
|
||||
url: jdbc:postresql:/localhost:5432/instance
|
||||
username: foo
|
||||
password: bar
|
||||
secret: foo
|
||||
Reference in New Issue
Block a user