Skip to content

Commit ec6e27e

Browse files
committed
Add Properties
1 parent 479565b commit ec6e27e

6 files changed

Lines changed: 169 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Krzysztof Chruściel
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Spring Boot Properties
2+
This repository contains Spring Boot Properties example
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>pl.codecouple.properties</groupId>
7+
<artifactId>properties</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>properties</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.2.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-configuration-processor</artifactId>
40+
<optional>true</optional>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.hibernate</groupId>
44+
<artifactId>hibernate-validator</artifactId>
45+
<version>5.3.4.Final</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.projectlombok</groupId>
49+
<artifactId>lombok</artifactId>
50+
<version>1.16.12</version>
51+
</dependency>
52+
</dependencies>
53+
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-maven-plugin</artifactId>
59+
</plugin>
60+
</plugins>
61+
</build>
62+
63+
64+
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package pl.codecouple.properties;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.boot.CommandLineRunner;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
8+
import pl.codecouple.properties.custom.CustomProperties;
9+
10+
@SpringBootApplication
11+
@EnableConfigurationProperties(CustomProperties.class)
12+
public class PropertiesApplication implements CommandLineRunner{
13+
14+
@Autowired
15+
CustomProperties customProperties;
16+
17+
public static void main(String[] args) {
18+
SpringApplication.run(PropertiesApplication.class, args);
19+
20+
}
21+
22+
@Override
23+
public void run(String... strings) throws Exception {
24+
System.out.println(customProperties);
25+
System.out.println(customProperties.getPathMapper().get("first"));
26+
System.out.println(customProperties.getPathMapper().get("second"));
27+
System.out.println(customProperties.getPathMapper().get("third"));
28+
}
29+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package pl.codecouple.properties.custom;
2+
3+
import lombok.Data;
4+
import org.hibernate.validator.constraints.NotEmpty;
5+
import org.springframework.boot.context.properties.ConfigurationProperties;
6+
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
7+
import org.springframework.validation.annotation.Validated;
8+
9+
import java.util.Map;
10+
11+
/**
12+
* Created by Krzysztof Chruściel.
13+
*/
14+
@Data
15+
@ConfigurationProperties(prefix="custom")
16+
@Validated
17+
public class CustomProperties {
18+
19+
/** Property value. */
20+
@NotEmpty
21+
private String property;
22+
23+
/** Map example. */
24+
private Map<String, String> pathMapper;
25+
26+
/** Connection propeties. */
27+
private Connection connection;
28+
29+
@Data
30+
public static class Connection {
31+
32+
/** Connection port. */
33+
private int port = 8080;
34+
/** Connection address. */
35+
private String address = "localhost";
36+
/** This value is deprecated. */
37+
private String server;
38+
39+
@DeprecatedConfigurationProperty(replacement = "custom.connection.address")
40+
@Deprecated
41+
public String getServer() {
42+
return getAddress();
43+
}
44+
45+
@Deprecated
46+
public void setServer(String server){
47+
setAddress(server);
48+
}
49+
50+
}
51+
52+
53+
}

spring-boot-properties-example/src/main/resources/application.properties

Whitespace-only changes.

0 commit comments

Comments
 (0)