在springboot中,如何做到当项目启动的时候
发布网友
我来回答
共2个回答
懂视网

org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
@RestController
@SpringBootApplication //Spring Boot核心注解,用于开启自动配置
public class StartApplication implements CommandLineRunner{
//程序可以直接在此启动
@RequestMapping("/")
String index(){
return "ok";
}
public static void main(String[] args) {
SpringApplication.run(StartApplication.class, args);
}
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//配置静态资源处理
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/META-INF/")
.addResourceLocations("classpath:/hospitalpay");
}
@Override
public void run(String... args) throws Exception {
//项目启动时会执行这里的任务
//通常加载用于系统参数加载
}
}
springboot启动项目时执行任务,从数据库或者redis获取系统参数
标签:mapping nbsp request start source 资源 location 方法 ppi
热心网友
1、如果是线程级别的访问,放threadlocal里头就可以了,其他类从threadlocal里去访问
2、如果是跨线程的,session是一种方案,setAttribute是ok的,觉得麻烦,可以封装个方法
3、不想用session的话,就放redis,把过期时间设置成跟oauth的过期相当就可以了