1. 创建配置文件
# 七牛云相关
qiniu:
accessKey: accessKey
secretKey: secretKey
# CDN加速域名前缀
cdnDomainPrefix: cdnDomainPrefix
2. 添加映射配置
- 项目添加相关依赖
<!-- 引入依赖将配置文件中的配置变更为常量 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
- 创建实体类
创建实体类使用
@ConfigurationProperties(prefix = "qiniu")
指定配置文件前缀为qiniu
,还可以添加
ignoreInvalidFields = true
,ignoreUnknownFields = true
属性用来忽略未知属性和类型错误的属性
package net.lesscoding.common;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author eleven
* @date 2023/1/5 14:13
* @description 七牛云常量配置
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@ConfigurationProperties(prefix = "qiniu")
public class QiNiuConst {
/**
* 七牛云 accessKey
*/
private String accessKey;
/**
* 七牛云 secretKey
*/
private String secretKey;
/**
* CDN加速域名前缀
*/
private String cdnDomainPrefix;
}
- 启动类添加配置
启动类添加
@EnableConfigurationProperties({QiNiuConst.class})
用于扫描配置类
package net.lesscoding;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import net.lesscoding.common.QiNiuConst;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* @author eleven
* @date 2022-11-10 19:55:27
* @description 启动启动类
* Generated By: lesscoding.net basic service
* Link to: <a href="https://lesscoding.net">https://lesscoding.net</a>
* mail to:2496290990@ zjh292411@
*/
@MapperScan("net.lesscoding.mapper")
@EnableKnife4j
@EnableScheduling
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigurationProperties({QiNiuConst.class})
public class MainApp {
public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);
}
}
- 使用
@Autowrited
注解直接注入即可
package net.lesscoding.utils;
import lombok.extern.slf4j.Slf4j;
import net.lesscoding.common.QiNiuConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* @author eleven
* @date 2023/1/5 14:03
* @description
*/
@Component
@Slf4j
public class PropertiesUtil {
@Autowired
private QiNiuConst qiNiuConst;
/**
* 项目启动后打印数据
* @throws Exception
*/
@PostConstruct
public void afterRunning() throws Exception {
log.info("====七牛云配置 {}====",qiNiuConst);
}
}
3. 配置项映射为静态变量
修改映射实体类,实现
InitializingBean
接口,将属性赋值给静态变量,使用的时候就不需要注入了,直接QiNiuConst.ACCESS_KEY
即可
package net.lesscoding.common;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author eleven
* @date 2023/1/5 14:13
* @description 七牛云常量配置
*/
@ConfigurationProperties(prefix = "qiniu")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class QiNiuConst implements InitializingBean {
/**
* 七牛云 accessKey
*/
private String accessKey;
/**
* 七牛云 secretKey
*/
private String secretKey;
/**
* CDN加速域名前缀
*/
private String cdnDomainPrefix;
private static String ACCESS_KEY;
private static String SECRET_KEY;
private static String CDN_DOMAIN_PREFIX;
@Override
public void afterPropertiesSet() throws Exception {
ACCESS_KEY = accessKey;
SECRET_KEY = secretKey;
CDN_DOMAIN_PREFIX = cdnDomainPrefix;
}
}
4. 推荐一下自己的面试题网站
跟外包公司合作的面试题网站,不用自己出去面试也能拿到最新的八股文面试题哈哈哈