Springboot使用-静态类读取配置文件

1 springboot读取配置文件的两种方式

属性上使用:@Value("${xxxx}")
类上使用:@ConfigurationProperties(prefix = "xxx")

2 静态类读取配置文件

思路

1 工具类增加静态属性的初始化方法init
2 创建bean对象
    - 通过@ConfigurationProperties读取配置文件
    - 通过component将bean对象注入到容器中
3 创建代理类
    - 通过component读取容器中bean对象的属性
    - 在构造方法中调用工具类静态属性的初始化方法init,从而初始化工具类的静态属性
  • 1 创建maven项目GetProperties
  • 2 修改pom.xml,引入parent/web/test依赖
    <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 增加配置文件/resources/application.properties,内容为
study.theme=getProperties
study.result=true
  • 4 增加启动类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 核心三部曲,工具类增加静态变量初始化方法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 核心三部曲,具体bean对象读取配置文件
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 核心三部曲,代理类构造方法中调用工具类静态属性的初始化方法
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 增加测试类,测试验证
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());
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_45544465/article/details/106320280