@Autowired注入的bean都为null,然后试了各种方法,什么生命周期啥的,试了各种,还是失败了;
最终请教大哥,说是可以这样:
调用类:
@Autowired private UserService userService;
if (null==userService) {
userService = SpringContextUtils.getContext().getBean(UserService.class);
}
@Component public class SpringContextUtils implements ApplicationContextAware { private static ApplicationContext context; @Override public void setApplicationContext(ApplicationContext context) throws BeansException { SpringContextUtils.context = context; } public static ApplicationContext getContext() { return context; } }
然后,然后就可以了。