• spring静态获得配置文件中的属性


    //
    // Source code recreated from a .class file by IntelliJ IDEA
    // (powered by Fernflower decompiler)
    //
    
    package com.example.demo;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    import org.springframework.context.EnvironmentAware;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.env.Environment;
    
    @Configuration
    public class PropertiesBean implements EnvironmentAware, BeanPostProcessor {
        private static Log logger = LogFactory.getLog(PropertiesBean.class);
        private Environment environment;
        private static PropertiesBean propertiesBean;
    
        public PropertiesBean() {
            logger.info("Construct EnvironmentProperties");
            propertiesBean = this;
        }
    
        public static PropertiesBean getInstance() {
            return propertiesBean;
        }
    
        @Override
        public void setEnvironment(Environment environment) {
            this.environment = environment;
        }
    
        public Environment getEnvironment() {
            return this.environment;
        }
    
        public String[] getActiveProfiles() {
            return this.environment.getActiveProfiles();
        }
    
        public String[] getDefaultProfiles() {
            return this.environment.getDefaultProfiles();
        }
    
        public boolean acceptsProfiles(String... profiles) {
            return this.environment.acceptsProfiles(profiles);
        }
    
        public boolean containsProperty(String key) {
            return this.environment.containsProperty(key);
        }
    
        public String getProperty(String key) {
            return this.environment.getProperty(key);
        }
    
        public String getProperty(String key, String defaultValue) {
            return this.environment.getProperty(key, defaultValue);
        }
    
        public <T> T getProperty(String key, Class<T> targetType) {
            return this.environment.getProperty(key, targetType);
        }
    
        public <T> T getProperty(String key, Class<T> targetType, T defaultValue) {
            return this.environment.getProperty(key, targetType, defaultValue);
        }
    
        public String getRequiredProperty(String key) {
            return this.environment.getRequiredProperty(key);
        }
    
        public <T> T getRequiredProperty(String key, Class<T> targetType) throws IllegalStateException {
            return this.environment.getRequiredProperty(key, targetType);
        }
    
        public String resolvePlaceholders(String text) {
            return this.environment.resolvePlaceholders(text);
        }
    
        public String resolveRequiredPlaceholders(String text) {
            return this.environment.resolveRequiredPlaceholders(text);
        }
    }

    注:

    实现 BeanPostProcessor 保证该配置类在spring容器初始化之前初始化,实现EnvironmentAware获得全局配置环境对象environment

    配置文件:

     测试类:

    package com.example.demo.service;
    
    import cn.hutool.core.lang.Console;
    import cn.hutool.http.HttpRequest;
    import cn.hutool.http.HttpResponse;
    import cn.hutool.json.XML;
    import com.alibaba.fastjson.JSONObject;
    import com.example.demo.PropertiesBean;
    import com.example.demo.dao.CmpOclOrderRepository;
    import com.example.demo.dto.supp.BaseHospCstDto;
    import com.example.demo.entry.ocl.CmpOclOrder;
    import com.example.demo.entry.supp.BaseHospCst;
    import com.example.demo.utils.ExcelUtilTest;
    import org.springframework.stereotype.Component;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.annotation.Resource;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.List;
    
    
    @Component
    public class TestService {
    
        public static String string = PropertiesBean.getInstance().getProperty("conf.address.smp");
    
        static{
            Console.log("初始化静态配置属性-------------------------"+string);
        }
    
    //    public void testProxy() {
    //        System.out.println("-------我是需要被代理的方法------");
    //    }
    //
    //    public void testProxy2() {
    //        System.out.println("-------我是需要被代理的方法222------");
    //    }
    }

    启动demo项目:

  • 相关阅读:
    easyui使用datagrid时列名包含特殊字符导致表头与数据错位的问题
    解析:使用easyui的form提交表单,在IE下出现类似附件下载时提示是否保存的现象
    使用PreApplicationStartMethodAttribute
    Entity Framework使用Sqlite时的一些配置
    如何自己编写一个easyui插件续
    如何自己编写一个easyui插件
    启动报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
    Tomcat启动报错:java.net.BindException: Cannot assign requested address: JVM_Bind
    Maven项目Eclipse启动时报错: java.lang.ClassNotFoundException: org.springframework.web.util.IntrospectorCleanupListener
    ORA-12519, TNS:no appropriate service handler found(数据库上当前的连接数目已经超过最大值)
  • 原文地址:https://www.cnblogs.com/guanxiaohe/p/11736573.html
Copyright © 2020-2023  润新知