zoukankan      html  css  js  c++  java
  • SpringCloud统一配置之使用配置

    1. 配置的读取方式在上一篇文章中有提到。
    2. 取到值之后赋值给静态类里的静态变量。
    3. 因为SpringCloud项目启动的时候会把所有的api都执行一遍(相当蛋疼),所以这里直接就可以写一个方法进行赋值。

    代码如下:

    入口类:

    package com.shinho;
    
    import java.util.Map;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    
    import com.alibaba.fastjson.JSON;
    import com.shinho.log.ShinhoLog;
    
    @Configuration
    @EnableAutoConfiguration
    @ComponentScan(basePackages={"com.shinho"})
    @EnableWebMvc
    @EnableEurekaClient
    @RestController
    public class KylintestApplication {
    
        @Value("${foo}")
        String foo;
        
        private static final Logger logger = LoggerFactory.getLogger(KylintestApplication.class);
        
        public static void main(String[] args) {
            SpringApplication.run(KylintestApplication.class, args);
        }
        
        @RequestMapping("/log")
        @Bean
        public String Log() {
            logger.info("init:log");
            ShinhoLog.foo = this.foo;
            String json = "log";
            return json;
        }
        
        @RequestMapping("/hi")
        @Bean
        public String home() {
            String json = "123:"+ShinhoLog.info();
            return json;
        }
    }

    静态类:

    package com.shinho.log;
    
    import java.io.File;
    
    import org.apache.commons.io.FileUtils;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.stereotype.Component;
    
    @Component
    public class ShinhoLog {
        
        public static String foo;
        
        public static String info(){
            return foo;
        }
        
    }

    启动之后立即访问 http://localhost:XXXX/hi 这是会返回123:foo version 2, 完美!

  • 相关阅读:
    Windows JScript 在 游览器 中运行 调试 Shell 文件系统
    autohotkey 符号链接 软连接 symbolink
    软链接 硬链接 测试
    SolidWorks 修改 基准面 标准坐标系
    手机 路径 WebDAV 映射 驱动器
    Win10上手机路径
    explorer 命令行
    单位公司 网络 封锁 屏蔽 深信 AC
    cobbler自动化部署原理篇
    Docker四种网络模式
  • 原文地址:https://www.cnblogs.com/wpcnblog/p/8992030.html
Copyright © 2011-2022 走看看