记录一次ApplicationContext打入jar包后getBean空指针问题

        项目中有两个服务,PC和H5,各自的工程代码里都有Service包。后来做代码优化,将service的包抽出来做了一个jar包,PC和H5的服务各自引用该jar包。原本service包在各自服务内部的时候没有问题,抽出来jar包再引用后就发现,使用ApplicationContext.getBean方法时,ApplicationContext报空指针,以下为项目中的代码结构以及解决方法。

        在framework-common的jar包中有一个SpringContextUtils类,类中对ApplicationContext的方法进行了封装:

@Lazy(false)
@Component("springContext")
public class SpringContextUtils implements ApplicationContextAware {

    private static ApplicationContext context;
    private static boolean started=false;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtils.context=applicationContext;
        started = true;
    }

    public static Object getBean(String name) throws BeansException {
        return context.getBean(name);
    }

}

        在另一个service-common的jar包中,对framework-common的jar包进行了引用,并在某个类中调用的SpringContextUtils.getBean方法:

@Slf4j
@Component
public class HandleStrategy {

    @Transactional(transactionManager = "tx", rollbackFor = Exception.class)
    public void exec(String id, Integer type) {
        long startTime = System.currentTimeMillis();
        log.info("任务开始------------------------------------------");
        final AbstractTypeHandler handler = (AbstractTypeHandler) SpringContextUtils.getBean(type);
        handler.handle();
        log.info("完成:任务总执行时间为" + (System.currentTimeMillis() - startTime) + "毫秒");     
}

结果就是获取AbstractTypeHandler的时候,SpringContextUtils类中一直报空指针。

        从网上搜了一下,说是SpringContextUtils类放到本身的服务中是没问题的,打入到jar包中会有加载顺序的问题。但是奇怪的是,原来在服务中,SpringContextUtils类也是在framework-common的jar包中依赖进去的,是没有问题的。只是后来把服务的Service也抽出来打成了jar包,服务引用service-common的jar包,service-common引用framework-common的jar包,就有问题了。目前虽然解决了,但是不晓得具体原因是啥,还请路过的各位大神能解解惑。

贴一下解决方案吧,代码如下所示:

@Slf4j
@Component
@DependsOn("springContext")
public class HandleStrategy {

    @Transactional(transactionManager = "tx", rollbackFor = Exception.class)
    public void exec(String id, Integer type) {
        long startTime = System.currentTimeMillis();
        log.info("任务开始------------------------------------------");
        final AbstractTypeHandler handler = (AbstractTypeHandler) SpringContextUtils.getBean(type);
        handler.handle();
        log.info("完成:任务总执行时间为" + (System.currentTimeMillis() - startTime) + "毫秒");     
}

在HandlerStrategy类上添加了@DependsOn注解,标注该类的加载将依赖SpringContextUtils类,springContext是SpringContextUtils类的名称,是第一段代码中SpringContextUtils类上面@Component中的名称。

读书小Tip:解读“囿”,总是觉得自己有了,却又看不到困在自己周围的笼子,好可怕~

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值