日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

SpringCloud统一配置之使用配置

發布時間:2023/10/11 综合教程 255 老码农
生活随笔 收集整理的這篇文章主要介紹了 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, 完美!

總結

以上是生活随笔為你收集整理的SpringCloud统一配置之使用配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。