工具类(类)中使用ApplicationContext获取bean包空指针

项目中遇到这样一个问题,当时是要写一个工具类,里面有这样的一段话,
MsgTools是工具类,里面有一个属性applicationContext

MsgTools.applicationContext
          .getBean(com.cheshangma.operation.wx.service.internal.WeChatServiceImpl.class)

但这样会报错,applicationContext一直是null,不能注入进来,听公司大牛解释就是,加载MsgTools类时,ApplicationContext类还没有被spring代理加载,所以不能注入进MsgTools这个类里面.解决的思路就是要让ApplicationContext这个类先被spring先管理注入,然后在去加载管理MsgTools这个类.

代码如下

package com.cheshangma.operation.wx.common.utils;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

import com.cheshangma.operation.common.enums.MessageType;
import com.cheshangma.operation.entity.NoticeEventEntity;
import com.cheshangma.operation.wx.common.template.CarCertifiMsg;
import com.cheshangma.operation.wx.common.template.CarIllegalMsg;
import com.cheshangma.operation.wx.common.template.CarNoticeMsg;
import com.cheshangma.operation.wx.common.template.EventReply;
import com.cheshangma.operation.wx.common.template.MsgTemplate;
import com.cheshangma.operation.wx.common.template.OrderNewMsg;
import com.cheshangma.operation.wx.common.template.OrderPayMsg;
import com.cheshangma.operation.wx.common.template.OrderShipMsg;
import com.cheshangma.operation.wx.common.template.WxTemplate;
import com.cheshangma.operation.wx.service.MsgService;
import com.cheshangma.operation.wx.service.NoticeEventService;


/**
 * 根据通知类型不同调用不同service发送消息
 * 
 * @author yhy
 *
 */
@Configuration
public class MsgTools implements ApplicationContextAware {//这里实现ApplicationContextAware接口
  @SuppressWarnings("unused")
  private static Logger log = Logger.getLogger(MsgTools.class);

  public static int TIME_INTERVAL = 1;
  public static int SLEEP_TIME = 10 * 1000;
  /** 存入memcached的时间 **/
  public static int MEM_TIME = (TIME_INTERVAL + 1) * 60 * 1000;
  private static Map map = new HashMap();
  private static NoticeEventService noticeEventService = null;
  private static MsgTools msgTools = new MsgTools();
  private static boolean isReloaded = false;
  private static ApplicationContext applicationContext;










  public static MsgTools getInstance() {
    return msgTools;
  }








  public boolean sendOneMsg(MessageType msgType, MsgTemplate msgTemplate) {
    synchronized (MsgTools.class) {
      while (!isReloaded) {//ApplicationContext类没有加载完,让线程等待
        try {
          MsgTools.class.wait();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      MsgService msgService = (MsgService) map.get(msgType);
      return msgService.sendMsg(msgTemplate);
    }
  }


  @Override
  public void setApplicationContext(ApplicationContext arg0) throws BeansException {
  //这里就是让ApplicationContext加载完后,在使用ApplicationContext来获得bean
    synchronized (MsgTools.class) {
      MsgTools.applicationContext = arg0;
      isReloaded = true;
      map.put(MessageType.MSGTYPE_WEIXIN, MsgTools.applicationContext
          .getBean(com.cheshangma.operation.wx.service.internal.WeChatServiceImpl.class));
      map.put(MessageType.MSGTYPE_SMS, MsgTools.applicationContext
          .getBean(com.cheshangma.operation.wx.service.internal.SmsServiceImpl.class));
      map.put(MessageType.MSGTYPE_VOICE, MsgTools.applicationContext
          .getBean(com.cheshangma.operation.wx.service.internal.VoiceServiceImpl.class));
      noticeEventService =
          (NoticeEventService) MsgTools.applicationContext
              .getBean(com.cheshangma.operation.wx.service.internal.NoticeEventServiceImpl.class);
      MsgTools.class.notifyAll();
    }

  }

}



你可能感兴趣的:(工具类(类)中使用ApplicationContext获取bean包空指针)