1. 在某些业务中,需要将类的配置写到配置文件中, 不在代码中写死配置。
2. 所以需要读取配置文件(yaml, properties) 读取到Java Bean中。
3. 本文以oss对象存储配置为demo。两种方式初始化配置文件。

-----------------
1. yml配置文件

server:  port: 8888spring:  application:    name: hello-world# oss对象存储配置oss-constant:  endpoint: secret  access-key: key  secret-key: key  bucket: bucket

2. 绑定方式1

使用@Value注解, 注入配置文件的值。
好处:能够选择性的配置。能够类名.属性。
坏处:写死了@Value(value=“xxx”) 前缀,项目打包后了不方便修改。

package top.bitqian.hello.constant;import lombok.Data;import org.springframework.beans.factory.InitializingBean;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;/** * 加载oss对象存储配置 * java项目www.fhadmin.org * @date 2021/3/20 09:30 */ @Data@Componentpublic class OssLoadConstant implements InitializingBean {    @Value("${oss-constant.endpoint}")    private String endpoint;    @Value("${oss-constant.access-key}")    private String accessKeyId;    @Value("${oss-constant.secret-key}")    private String accessKeySecret;    @Value("${oss-constant.bucket}")    private String bucketName;    public static String ENDPOINT;    public static String ACCESS_KEY_ID;    public static String ACCESS_KEY_SECRET;    public static String BUCKET_NAME;    @Override    public void afterPropertiesSet() {        // 初始化设置值        ENDPOINT = this.endpoint;        ACCESS_KEY_ID = this.accessKeyId;        ACCESS_KEY_SECRET = this.accessKeySecret;        BUCKET_NAME = this.bucketName;    }}

3. 绑定方式2

3.1 依赖.

    <dependencies><!-- 属性装配提示 -->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-configuration-processor</artifactId>            <optional>true</optional>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>                <executions>                    <execution>                        <!-- 打包时排除 属性装配提示插件 -->                        <configuration>                            <groupId>org.springframework.boot</groupId>                            <artifactId>spring-boot-configuration-processor</artifactId>                        </configuration>                    </execution>                </executions>            </plugin>        </plugins>    </build>

3.2 boot 中提供的属性装配功能。

yaml中的前缀和下面的ConfigurationProperties的prefix对应。
yaml中的属性和实体类属性对应。(yaml中的横杠可以转驼峰)

package top.bitqian.hello.constant;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;/** * 此Java类与配置文件绑定了. * java项目www.fhadmin.org * @date 2021/3/20 09:36 *//*  1. 绑定方法1:  @Component  @ConfigurationProperties(prefix = "oss-constant")  2. 绑定方法2:  实体配置类, @ConfigurationProperties(prefix = "oss-constant")  在配置类上开启和配置文件(yml, properties)绑定 :  @EnableConfigurationProperties(value = {OssConstantConfig.class}) */@Data@Component@ConfigurationProperties(prefix = "oss-constant")public class OssConstantConfig {    private String endpoint;    private String accessKey;    private String secretKey;    private String bucket;}

获取的话,从容器中获取就行了。配置文件中的值会自动装配到Java Bean中。


©著作权归作者所有:来自51CTO博客作者fhspringcloud的原创作品,如需转载,请注明出处,否则将追究法律责任

你的鼓励让我更有动力

赞赏

0人进行了赞赏支持

更多相关文章

  1. 前端工程师的CI进阶之路
  2. Spring Cloud 2.x之SpringBoot配置Cors解决跨域请求
  3. 监控系统项目实施--安装与部署-PG数据库监控配置-libzbxpgsql部
  4. MySQL 8.0 常用语句、主从及MHA配置文件调整
  5. 不懂运维的开发人员能走多远?快来学习一下 Nginx 的配置吧!
  6. 利用ELK分析Nginx日志生产实战(高清多图)
  7. 跨域配置相关选项
  8. 强大!Nginx 配置在线一键生成“神器”
  9. 面试必备,电商系统中并发测试是怎样进行的?

随机推荐

  1. Android的Selector与Shape
  2. android 各个属性
  3. Android实战技巧之二十二:Android 5.1 SDK
  4. android.webkit.WebView/WebViewClient/W
  5. android 资料地址
  6. Android 5.1 API 22 所有sdk文件下载地址
  7. ANDROID 绝对布局 相对布局 Linear…
  8. Android(安卓)编译,打包、签程名详细教
  9. android 带文字的ImageButton
  10. 修改标题栏的高度