1.在应用的Filter或Listener中使用了@Autowired ,
原因:因为Filter和Listener加载顺序优先于spring容器初始化实例,所以使用@Autowired肯定为null了~~
解决:用ApplicationContext根据bean名称(注意名称为实现类而不是接口)去获取bean,随便写个工具类即可
ApplicationContext的工具类:
@Component
@Lazy(false)
public class SpringUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
//获取applicationContext
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
//通过name获取 Bean.
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
//通过class获取Bean.
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
//通过name,以及Clazz返回指定的Bean
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (SpringUtil.applicationContext == null) {
SpringUtil.applicationContext = applicationContext;
}
}
private SpringUtil() {
}
}
2.你写的代码有问题,没加@Service注解等 ,这一类低级错误自己检查即可
3.你写的@Service、@Componet、@Configuration、@Repository等Spring注解未被扫描到,例如:springboot的主类扫描规则 -> @ComponentScan扫描多个包配置
============================================================================
暂时遇到这几种情况,以后遇到接着补充~~
转载于:https://blog.csdn.net/u013189824/article/details/91579230