很多初学的朋友们可能会遇到这样的问题,自己new的一个对象,但是在这个对象内部,@Autowired引入对象的时候,运行的时候会报空指针异常,,针对此类问题,可能查了好多帖子也无济于事,下面就这种情况作为说明,首先创建一个class
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class GetSpringBean implements ApplicationContextAware {
/**
* 上下文对象实例
*/
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
/**
* 获取applicationContext
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 通过name获取 Bean.
*
* @param