shiro框架中调用service失败(也就是bean注入失败)

@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;
    }
}

然后,然后就可以了。

上一篇:Spring - JUnit整合测试


下一篇:spring学习笔记之---IOC和DI