• java实体类读取属性文件,并赋值


    1.application.properties文件

    #阿里云 OSS
    #不同的服务器,地址不同
    aliyun.oss.file.endpoint=oss-cn-beijing.aliyuncs.com
    aliyun.oss.file.keyid=LT
    aliyun.oss.file.keysecret=VQGvW
    
    #bucket可以在控制台创建,也可以使用java代码创建
    aliyun.oss.file.bucketname=aaa

    2.java工具类

    package com.stu.oss.utils;
    
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    //项目启动,spring接口,spring加载之后,执行接口一个方法
    @Component
    public class ConstantPropertiesUtil implements InitializingBean {
    
        //读取配置文件的内容
    
        @Value("${aliyun.oss.file.endpoint}")
        private String endpoint;
        @Value("${aliyun.oss.file.keyid}")
        private String keyid;
        @Value("${aliyun.oss.file.keysecret}")
        private String keysecret;
        @Value("${aliyun.oss.file.bucketname}")
        private String bucketname;
    
        //定义一些静态常量
        public  static String END_POINT;
        public  static String KEY_ID;
        public  static String KEY_SECRET;
        public  static String BUCKET_NAME;
    
        //上边赋值完成后,会执行afterPropertiesSet方法,这是spring机制
        @Override
        public void afterPropertiesSet() throws Exception {
            END_POINT = endpoint;
            KEY_ID = keyid;
            KEY_SECRET = keysecret;
            BUCKET_NAME = bucketname;
    
    
        }
    }

    3.通过工具类取得属性文件的值

    String endPoint = ConstantPropertiesUtil.END_POINT;
    String accessKeyId = ConstantPropertiesUtil.KEY_ID;
    String accessKeySecret = ConstantPropertiesUtil.KEY_SECRET;
    String bucketName = ConstantPropertiesUtil.BUCKET_NAME;
  • 相关阅读:
    LeetCode OJ String to Integer (atoi) 字符串转数字
    HDU 1005 Number Sequence(AC代码)
    HDU 1004 Let the Balloon Rise(AC代码)
    HDU 1003 Max Sum(AC代码)
    012 Integer to Roman 整数转换成罗马数字
    011 Container With Most Water 盛最多水的容器
    010 Regular Expression Matching 正则表达式匹配
    007 Reverse Integer 旋转整数
    006 ZigZag Conversion
    005 Longest Palindromic Substring 最长回文子串
  • 原文地址:https://www.cnblogs.com/konglxblog/p/15077011.html
Copyright © 2020-2023  润新知