Article directory
Use kotlin's spring boot project to read the content in properties, including reading strings, lists, maps, etc.
1. Read the properties content
1.1 Read the string in properties
Read the name and number of books from the configuration file
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Configuration
@Configuration
@ConfigurationProperties(prefix = "test.book")
class BookProperties {
lateinit var name: String
var count by Delegates.notNull<Int>()
}
Then configure the following content in properties:
test.book.name=Kotlin
test.book.count=20
In this way, when the program is started, the configured name value Kotlin and count value 20 can be obtained.
1.2 Read the list and map in properties
Read the list from the configuration file, which contains the name of the author, and read the customer in the form of a map, indicating how many books each person bought.
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Configuration
@Configuration
@ConfigurationProperties(prefix = "test.book")
class BookProperties {
lateinit var authors: List<String>
lateinit var customers: Map<String, Int>
}
The content configured in properties is as follows
test.book.authors=AAA,BBB,CCC
test.book.customers[tom]=23
test.book.customers[jerry]=20
In this way, when using the BookProperties bean, you can read the values in the list and map.
2. Properties content to static class
There is such a requirement to map the contents of properties to a static class, which will only be loaded once. (I don’t understand the meaning of this, so I’ll dig a hole first)
2.1 Java
In Java, we can write like this
import lombok.Data;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.List;
import java.util.Map;
@Data
@RequiredArgsConstructor
@Configuration
public class YYProperties {
private final BookProperties bookProperties;
@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "test.book")
public static class BookProperties {
private String name;
private int count;
private List<String> authors;
private Map<String, Integer> customers;
}
}
The configuration of the properties file is as follows:
test.book.name=JAVA
test.book.count=40
test.book.authors=AAA,BBB,CCC
test.book.customers[tom]=12
test.book.customers[jerry]=28
In this way, when we use the yYProperties bean, we can read the value of this member variable bookProperties, such as
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class YYService {
private final YYProperties yyProperties;
public void hello() {
System.out.println(yyProperties.getBookProperties().getAuthors());
System.out.println(yyProperties.getBookProperties().getName());
System.out.println(yyProperties.getBookProperties().getCount());
System.out.println(yyProperties.getBookProperties().getCustomers());
}
}
2.2 Kotlin
So in kotlin, can it be written like this?
We all know that there is no static modifier static in kotlin, only object. After the object class is compiled into a class file, final will be added, and the class modified by final cannot be added with the @Configuration annotation.
3. Assign values to static variables
Sometimes, I just want to assign the value configured in properties to a static variable. How should I do this?
When will this need arise? For example, I configure a key that needs to be used in a util class. This util class is not registered as a bean, and there is no configured class content injected into it, so I want static variable values.
3.1 Java
3.1.1 Use @ConfigurationProperties annotation
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.List;
import java.util.Map;
@Configuration
@ConfigurationProperties(prefix = "test.book")
public class YYProperties {
public static String name;
public static int count;
public static List<String> authors;
public static Map<String, String> customers;
public void setName(String name) {
YYProperties.name = name;
}
public void setCount(int count) {
YYProperties.count = count;
}
public void setAuthors(List<String> authors) {
YYProperties.authors = authors;
}
public void setCustomers(Map<String, String> customers) {
YYProperties.customers = customers;
}
}
Configure it like this in properties
test.book.name=JAVA
test.book.count=40
test.book.authors=AAA,BBB,CCC
test.book.customers[tom]=12
test.book.customers[jerry]=28
This way you can use this written value
System.out.println(YYProperties.name);
System.out.println(YYProperties.authors);
System.out.println(YYProperties.customers);
System.out.println(YYProperties.count);
The print result is as follows:
JAVA
[AAA, BBB, CCC]
{
tom=12, jerry=28}
40
3.1.2 Using @Value annotation
Of course, if you don’t use the @configurationProperties annotation and use the @Value annotation directly, what should you do?
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.List;
import java.util.Map;
@Configuration
//@ConfigurationProperties(prefix = "test.book")
public class YYProperties {
public static String name;
public static int count;
public static List<String> authors;
public static Map<String, String> customers;
@Value("${test.book.name}")
public void setName(String name) {
YYProperties.name = name;
}
@Value("${test.book.count}")
public void setCount(int count) {
YYProperties.count = count;
}
@Value("${test.book.authors}")
public void setAuthors(List<String> authors) {
YYProperties.authors = authors;
}
@Value("#{${test.book.customers}}")
public void setCustomers(Map<String, String> customers) {
YYProperties.customers = customers;
}
}
What should be noted is the map configuration. The @value annotation is on the method. At the same time, it is used
#{
${
test.book.customers}}
And the configuration file properties are also different.
test.book.name=JAVA
test.book.count=40
test.book.authors=AAA,BBB,CCC
test.book.customers={'a': 'aa', 'b':'bb'}
The output result is
3.2 Kotlin
3.2.1 Use @ConfigurationProperties annotation
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Configuration
@Configuration
@ConfigurationProperties(prefix = "redmi.book")
class BookProperties {
fun setName(name: String) {
BookProperties.name = name
}
fun setAuthors(authors: List<String>) {
BookProperties.authors = authors
}
fun setCustomers(customers: Map<String, String>) {
BookProperties.customers = customers
}
companion object {
var name: String = ""
var authors: List<String> = ArrayList()
var customers: Map<String, String> = HashMap()
}
}
Just configure it like this in properties
redmi.book.customers[aa]=AA
redmi.book.customers[bb]=BB
redmi.book.name=lisi
redmi.book.authors=aaa,bbb,ccc
In this way, static variables can be called directly in other classes.
println(BookProperties.authors)
println(BookProperties.customers)
The output result is
[aaa, bbb, ccc]
{
aa=AA, bb=BB}
3.2.2 Using @Value annotation
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Configuration
@Configuration
class BookProperties {
@Value("\${redmi.book.name}")
fun setName(name: String) {
BookProperties.name = name
}
@Value("\${redmi.book.authors}")
fun setAuthors(authors: List<String>) {
BookProperties.authors = authors
}
@Value("#{\${redmi.book.customers}}")
fun setCustomers(customers: Map<String, String>) {
BookProperties.customers = customers
}
companion object {
var name: String = ""
var authors: List<String> = ArrayList()
var customers: Map<String, String> = HashMap()
}
}
The properties are configured as follows
redmi.book.customers={
'a': 'aa', 'b': 'bb'}
redmi.book.name=lisi
redmi.book.authors=aaa,bbb,ccc
This way you can also get the corresponding value.