首页 技术 正文
技术 2022年11月11日
0 收藏 970 点赞 4,797 浏览 1903 个字
  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, 完美!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289