[springboot]启动时,先马上执行一些测试,可以实现CommandLineRunner接口 博客分类: CommandLineRunner springboot
程序员文章站
2024-03-17 13:04:22
...
[springboot]启动时,先马上进行一些测试,可以实现CommandLineRunner接口,在里面执行测试的内容。开始时在main方法里通过new了XXApp类,调用redisUtils不成功,原来main不在spring容器管理范围,自然bean初始化是null
@ComponentScan(value = {"com.xx"})
@SpringBootApplication
@EnableAsync
public class XXApp implements CommandLineRunner {
public static ConfigurableApplicationContext TURBO_CTX;
private static Logger logger = LoggerFactory.getLogger(XXApp.class);
@Autowired
private RedisUtils redisUtil ;
public static void main(String[] args) {
XXX = SpringApplication.run(XXApp.class, args);
logger.info("-----------------XXX 启动完毕!----------------");
}
@Override
public void run(String... strings) throws Exception {
System.out.println(redisUtil.get("name"));
redisUtil.set("info","哈哈哈");
redisUtil.set("中文","a哈哈哈b");
System.out.println(redisUtil.get("info"));
System.out.println(redisUtil.get("info2"));
}
@ComponentScan(value = {"com.xx"})
@SpringBootApplication
@EnableAsync
public class XXApp implements CommandLineRunner {
public static ConfigurableApplicationContext TURBO_CTX;
private static Logger logger = LoggerFactory.getLogger(XXApp.class);
@Autowired
private RedisUtils redisUtil ;
public static void main(String[] args) {
XXX = SpringApplication.run(XXApp.class, args);
logger.info("-----------------XXX 启动完毕!----------------");
}
@Override
public void run(String... strings) throws Exception {
System.out.println(redisUtil.get("name"));
redisUtil.set("info","哈哈哈");
redisUtil.set("中文","a哈哈哈b");
System.out.println(redisUtil.get("info"));
System.out.println(redisUtil.get("info2"));
}
上一篇: bzoj3659: Which Dreamed It
下一篇: vue需要注意的地方1