在下面的代码中,我使用inAppPurchaseService
来调用其函数,但在运行时,当我打印null
时,它会显示inAppPurchaseService
public class SubscriptionChecker {
private static final Logger log =Logger.getLogger(SubscriptionChecker.class);
@Autowired
private EmployerService employerService;
@Autowired
private InAppPurchaseService inAppPurchaseService;
private static final RenewalCheckStrategy STRATEGY_APPLE
= new AppleRenewalCheckStrategyImpl();
private static final RenewalCheckStrategy STRATEGY_GOOGLE
= new GoogleRenewalCheckStrategyImpl();
public boolean checkSubscription(User user) throws IOException, HunterException{
boolean res = false;
OptedPlan optedPlan = getCurrentSubscription(user);
switch (optedPlan.getPlanType()){
case FREE: res = checkFreeSubscription(optedPlan);
break;
case PREMIUM: res = checkPremiumSubscription(optedPlan,user);
break;
}
return res;
}
/**
*
* @param optedPlan
* @return
*/
public boolean checkFreeSubscription(OptedPlan optedPlan ){
optedPlan.setPlanExpired(HunterUtil.isPlanExpired(optedPlan.getPlanExpirationDate()));
optedPlan.setPlanActive(HunterUtil.isPlanActive(optedPlan.getPlanActiveDate()));
//log.info("checkFreeSubscriptionResponse:: "+(optedPlan.isPlanActive())?(optedPlan.getMaxConnectionCount()>0 && optedPlan.getMaxJobCount()>0):true);
return(optedPlan.isPlanActive())?(optedPlan.getMaxConnectionCount()>0 || optedPlan.getMaxJobCount()>0):true;
}
private boolean checkPremiumSubscription(OptedPlan optedPlan,User user) throws IOException, HunterException{
boolean res = false;
Date now = new Date();
optedPlan.setPlanExpired(HunterUtil.isPlanExpired(optedPlan.getPlanExpirationDate()));
Employer employer=(Employer)user;
//First check stored expiry date
log.info("checkPremiumSubscription:: "+optedPlan.isPlanExpired());
if(optedPlan.isPlanExpired()){
// If expired, check for renewal
optedPlan =
getRenwRenewalCheckStrategy(optedPlan).getRenewal(optedPlan,user);
employer.setOptedPlan(optedPlan);
employerService.save(employer);
if (optedPlan != null){
// Renewal found: check date and update information in db
res = optedPlan.getPlanExpirationDate().after(now);
log.info("checkPremiumSubscriptionFlag:: "+optedPlan.getPlanExpirationDate().after(now));
}
}
return res;
}
private OptedPlan getCurrentSubscription(User user) throws HunterException{
Employer employer=(Employer)user;
log.info("getCurrentSubscriptionResponse:: "+(HunterUtil.isNull(employer.getOptedPlan())?inAppPurchaseService.setFreePlanInEmployer(new StripeChargeTokenDto("FREE",DeviceType.ANDROID), employer)
:employer.getOptedPlan()));
return (HunterUtil.isNull(employer.getOptedPlan())?inAppPurchaseService.setFreePlanInEmployer(new StripeChargeTokenDto("FREE",DeviceType.ANDROID), employer)
:employer.getOptedPlan());
}
private RenewalCheckStrategy getRenwRenewalCheckStrategy(OptedPlan optedPlan){
RenewalCheckStrategy res = null;
res = (optedPlan.getDeviceType().equals(DeviceType.ANDROID))?STRATEGY_GOOGLE:STRATEGY_APPLE;
return res;
}
}
我的inAppPurchaseService
就像
public interface InAppPurchaseService extends GenericService<BillingDetails> {
OptedPlan setFreePlanInEmployer(StripeChargeTokenDto stripeChargeTokenDto,Employer employer)throws HunterException;
String gettingResponseForReceiptValidation(InAppPurchaseReceiptDto inAppPurchaseReceiptDto,Employer employer) throws HunterException;
JSONObject createRequestForValidation(String inAppPurchaseReceipt,Employer employer) throws HunterException;
void receiptForSuccessTransaction(InAppPurchaseReceiptResponse inAppResponse, Employer employer);
void updateOptedPlanOfEmployer(InAppPurchaseReceiptResponse inAppResponse, Employer emp,String productId,DeviceType deviceType) throws HunterException;
}
另请注意InAppPurchaseServiceImpl
我在代码中添加了@Service
注释。
答案 0 :(得分:1)
在SubscriptionChecker类的顶部应该是@Component
,因为SubscriptionChecker没有被spring上下文初始化。