zoukankan      html  css  js  c++  java
  • 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;
  • 相关阅读:
    tableView操作数据持久化
    9.0banb以前和9.0以后版本后JSON解析
    数据持久化存储回顾
    解决Xcode会出现的问题
    iview-cli 项目、iView admin 代理与跨域问题解决方案
    将变量做为一个对象的key,push新增进一个数组
    页面加载速度优化的建议
    vue中渲染页面,动态设置颜色
    e.currentTarget与e.target
    iview中tree的事件运用
  • 原文地址:https://www.cnblogs.com/konglxblog/p/15077011.html
Copyright © 2011-2022 走看看