在日常开发中某些工具类的静态方法需要使用到service或者dao,而java的语法要求静态方方法的只能调用静态变量,下面为注入静态bean的例子。
/**
* @author zsy
*/
@Component
public class WxghOnceMsgHelper {
private static EpMsgSendRecordMapper msgSendRecordMapper;
private static EpCampCfgCampPlatformService campCfgCampPlatformService;
private static EpWxghService wxghService;
@Autowired(required = true)
public WxghOnceMsgHelper(EpMsgSendRecordMapper msgSendRecordMapper,
EpCampCfgCampPlatformService campCfgCampPlatformService, EpWxghService wxghService) {
WxghOnceMsgHelper.msgSendRecordMapper = msgSendRecordMapper;
WxghOnceMsgHelper.campCfgCampPlatformService = campCfgCampPlatformService;
WxghOnceMsgHelper.wxghService = wxghService;
}
}
1.不能直接在变量上方加@Autowired,需要在其构造方法时注入
2.类需要加入@Component注解