Article Directory
1 Two ways for springboot to read configuration files
属性上使用:@Value("${xxxx}")
类上使用:@ConfigurationProperties(prefix = "xxx")
2 static class read configuration file
Ideas
1 工具类增加静态属性的初始化方法init
2 创建bean对象
- 通过@ConfigurationProperties读取配置文件
- 通过component将bean对象注入到容器中
3 创建代理类
- 通过component读取容器中bean对象的属性
- 在构造方法中调用工具类静态属性的初始化方法init,从而初始化工具类的静态属性
- 1 Create a maven project GetProperties
- 2 Modify pom.xml to introduce parent/web/test dependency
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
- 3 Add the configuration file /resources/application.properties, the content is
study.theme=getProperties
study.result=true
- 4 Add startup class cn.ithzp.properties.PropertiesApplication
package cn.ithzp.properties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PropertiesApplication {
public static void main(String[] args) {
SpringApplication.run(PropertiesApplication.class);
}
}
- 5 Core trilogy, tool class adds static variable initialization method init
package cn.ithzp.properties.util;
import cn.ithzp.properties.entity.StudyEntity;
public class StudyUtil {
private static String theme;
private static String result;
public static void initStaticProperties(StudyEntity studyEntity){
theme = studyEntity.getTheme();
result = studyEntity.getResult();
}
public static String getStaticProperties() {
return theme + ":" + result;
}
}
- 6 Core trilogy, specific bean objects read configuration files
package cn.ithzp.properties.entity;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "study")
public class StudyEntity {
private String theme;
private String result;
}
- 7 The core trilogy, the initialization method of static properties of the tool class is called in the proxy class construction method
package cn.ithzp.properties.Proxy;
import cn.ithzp.properties.entity.StudyEntity;
import cn.ithzp.properties.util.StudyUtil;
import org.springframework.stereotype.Component;
@Component
public class StudyProxy {
public StudyProxy(StudyEntity studyEntity) {
StudyUtil.initStaticProperties(studyEntity);
}
}
- 8 Add test category, test verification
package cn.ithzp.properties;
import cn.ithzp.properties.entity.StudyEntity;
import cn.ithzp.properties.util.StudyUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = {PropertiesApplication.class})
@RunWith(SpringRunner.class)
public class TestApplication {
@Autowired
private StudyEntity studyEntity;
@Test
public void test() {
System.out.println(studyEntity);
System.out.println(StudyUtil.getStaticProperties());
}
}