SpringBoot case - configuration file - @ConfigurationProperties

problem analysis

In previous articles on configuration parameters, the parameters of Alibaba Cloud OSS are set in the yml configuration file, and then @Value ("${}") is used to assign values ​​to the parameters, as follows:

This method is more cumbersome
 

problem solved

Use annotations

  • @Data
    • Automatically generate get/set methods for variables
  • @Component
    • Hand over the parameters to the IOC container for management and become a bean object
  • @ConfigurationProperties
    • Specify the prefix of the variable in the yml configuration file

Specific ideas : Create an entity class to encapsulate the attribute values ​​that need to be used. In the program that needs to be used, by injecting the entity class object, the get method will be called to obtain the corresponding attribute value.

Specific code:

  • Entity class
    • package com.example.tlias.pojo;
      
      import lombok.Data;
      import org.springframework.boot.context.properties.ConfigurationProperties;
      import org.springframework.stereotype.Component;
      
      @Data // 自动生成get/set方法
      @Component // 注入IOC容器,生成Bean对象
      @ConfigurationProperties(prefix = "aliyun.oss") // 设置属性前缀,便于找到对应在yml映射文件中的属性位置
      public class AliOSSProperties {
          private String endpoint;
          private String accessKeyId;
          private String accessKeySecret;
          private String bucketName;
      }
      
  • mapping file

    • ​​​​​​​ 

  • call property program 
    • ​​​​​​​
      package com.example.tlias.utils;
      
      import com.aliyun.oss.OSS;
      import com.aliyun.oss.OSSClientBuilder;
      import com.example.tlias.pojo.AliOSSProperties;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.stereotype.Component;
      import org.springframework.stereotype.Service;
      import org.springframework.web.multipart.MultipartFile;
      
      import java.io.*;
      import java.util.UUID;
      
      /**
       * 阿里云 OSS 工具类
       */
      @Component
      public class AliOSSUtils {
      
          //    // todo 指定OSS服务地址
      //    @Value("${aliyun.oss.endpoint}")
      //    private String endpoint;
      //    // todo 设置密钥账号和密码
      //    @Value("${aliyun.oss.accessKeyId}")
      //    private String accessKeyId;
      //    @Value("aliyun.oss.accessKeySecret")
      //    private String accessKeySecret;
      //    // todo 设置文件存储buket
      //    @Value("aliyun.oss.bucketName")
      //    private String bucketName;
          // todo 注入实体类对象
          @Autowired
          private AliOSSProperties aliOSSProperties;
      
          /**
           * 实现上传图片到OSS
           */
          public String upload(MultipartFile file) throws IOException {
              // todo 获取已经封装了的属性值
              String endpoint = aliOSSProperties.getEndpoint();
              String accessKeyId = aliOSSProperties.getAccessKeyId();
              String accessKeySecret = aliOSSProperties.getAccessKeySecret();
              String bucketName = aliOSSProperties.getBucketName();
              // 获取上传的文件的输入流
              InputStream inputStream = file.getInputStream();
      
              // 避免文件覆盖
              String originalFilename = file.getOriginalFilename();
              String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
      
              //上传文件到 OSS
              OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
              ossClient.putObject(bucketName, fileName, inputStream);
      
              //文件访问路径
              String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
              // 关闭ossClient
              ossClient.shutdown();
              return url;// 把上传到oss的路径返回
          }
      
      }
      

 Comparison of @ConfigurationProperties and @Value

  • Same point
    • ​​​​​​​​It is used to inject the properties of external configuration
  • Differences
    • The @Value annotation can only inject external properties one by one
    • @ConfigurationProperties can batch inject external property configurations into the properties of bean objects

Guess you like

Origin blog.csdn.net/weixin_64939936/article/details/132477654