springboot判空工具_springboot获取getBean方法以及ApplicationContext空指针问题解决

场景:在springboot多模块中使用getBean获取bean导致空指针异常。

@Test

public void test1(){

IUserService userService = SpringContextHolder.getBean(UserServiceImpl.class);

User user = userService.getUserByMobile("18782080356");

System.out.println(user);

}

假如有个工程有common模块和admin(启动类模块)模块,获取getBean的类在admin中,则不会报空指针,如果在common模块中,在admin模块中调用,则会报空指针异常,解决方法,使用@Import导入getBean方法所在的类

@SpringBootApplication

@Import(SpringContextHolder.class)

public class AdminApplication {

public static void main(String[] args) {

SpringApplication.run(OathServiceApplication.class, args);

}

}

import org.springframework.aop.framework.AopContext;

import org.springframework.beans.BeansException;

import org.springframework.beans.factory.NoSuchBea

你可能感兴趣的:(springboot判空工具)