前言:
这边做自动公开,设计了一个策略模式针对不同的条件走不同的逻辑,本来设计没有需要引用dao查询的操作,后续需求越来越多,导致需要不断地应用dao来在策略实现类里面进行查询操作;导致空指针,应该用过这个设计的人都应该懂得,也会在第一次懵逼,但是少钝一下就明白因为策略直接是new处理的对象,所以里面的bean,是null,也可以直接使用new对象出来,但是就怕,
@Autowired里面的bean里面还有其他引用注入,那就不能直接new了。
解决办法
/**
* 债券舆情 所属项目
*/
private static final Integer FILTER_RANGE = 5;
private static final String expression = "patternfunction('$TITLE$','#ECONOMYFLAG#')";
@Autowired
private NewFunctionFilterService newFunctionFilterService;
@Autowired
private FunctionRuleContantService ruleContantService;
public static EconomyNewsStrategy economyNewsStrategy;
@PostConstruct
public void init() {
economyNewsStrategy = this;
economyNewsStrategy.ruleContantService = this.ruleContantService;
}
@PostConstruct
public void start() {
economyNewsStrategy = this;
economyNewsStrategy.newFunctionFilterService = this.newFunctionFilterService;
}
@Override
public String checkAutoPublishPremise(TbmNewsProcessing tbmNewsProcessing, TbmNewsInfo tbmNewsInfo, Configuration configuration, List<TbmNewsTags> tbmNewsTagsList) {
NewsInfo newsInfo = configuration.getNewsInfo();
//原需求中的不自动化公开条件由“所属项目不为单债券舆情”变更为“项目不包含债券舆情”,并且公开条件需判断标度长度,若长度大于300字节则不自动公开
String contentType = "ECONOMYFLAG";
List<RuleContant> ruleContantList = economyNewsStrategy.ruleContantService.getListByContentType(contentType);
List<String> parameterList = ruleContantList.stream().map(item -> item.getParameter()).collect(Collectors.toList());
String matchRangeStr = economyNewsStrategy.newFunctionFilterService.getMatchParameter(tbmNewsProcessing.getTitle(), expression, parameterList);
if (StringUtils.isNotBlank(matchRangeStr)) {
return "标题中出现关键词,不自动公开";
}
return null;
}
场景还原
ruleContantDao里面注入了,ruleContantMapper导致new出来的对象
@Component
public class EconomyNewsStrategy implements NewsAutoPublishStrategy {
/**
* 债券舆情 所属项目
*/
private static final Integer FILTER_RANGE = 5;
private static final String expression = "patternfunction('$TITLE$','#ECONOMYFLAG#')";
private NewFunctionFilterService newFunctionFilterService = new NewFunctionFilterServiceImpl();
private RuleContantDao ruleContantDao = new RuleContantDaoImpl();
@Override
public String checkAutoPublishPremise(TbmNewsProcessing tbmNewsProcessing, TbmNewsInfo tbmNewsInfo, Configuration configuration, List<TbmNewsTags> tbmNewsTagsList) {
NewsInfo newsInfo = configuration.getNewsInfo();
//原需求中的不自动化公开条件由“所属项目不为单债券舆情”变更为“项目不包含债券舆情”,并且公开条件需判断标度长度,若长度大于300字节则不自动公开
List<Integer> appIds = newsInfo.getAppIds();
if (CollectionUtils.isNotEmpty(appIds)) {
if (!appIds.contains(FILTER_RANGE)) {
return "项目不包含债券舆情";
}
}
if (!Objects.equals(1005000006L, tbmNewsProcessing.getRangeBase())) {
return "范围≠公司";
}
String contentType = "ECONOMYFLAG";
List<RuleContant> ruleContantList = ruleContantDao.getListByContentType(contentType);
List<String> parameterList = ruleContantList.stream().map(item -> item.getParameter()).collect(Collectors.toList());
String matchRangeStr = newFunctionFilterService.getMatchParameter(tbmNewsProcessing.getTitle(), expression, parameterList);
if (StringUtils.isNotBlank(matchRangeStr)) {
return "标题中出现关键词,不自动公开";
}
return null;
}
}