springboot项目中使用自定义注解

需求要求:发送一段信息例如“XXX,你好!你关注XXX最近有新品发布,更新数量为XXX”,消息挖坑字段相同但底层使用策略不同,发送平台需要依据接口版本号进行控制。

因为xxx内容是根据不同策略做产出,所以可以考虑策略模式,在这里使用注解形式标注实现。

注解主体类 :@Retention注解作用https://blog.csdn.net/m0_37840000/article/details/80921775    注解之 @Targethttps://blog.csdn.net/dongzhanglong/article/details/120130237

@Retention(RetentionPolicy.RUNTIME)
@Target(value = { ElementType.TYPE })
public @interface PushStrategy {

    /**
     * 获取query的策略
     * @return
     */
    public Class<?> qStrategy();

    /**
     * 人群类型
     * @return
     */
    public String type();

    /**
     * 承接页的跳转链接
     * @return
     */
    public String actionPattern();
}

注解class初始化

/**
 * 通用的push策略工厂
 */
@Service
@Slf4j
public class PushStrategyFactory implements InitializingBean {

    @Resource
    private List<CustomMaterialContentService> customMaterialContentServiceList;

    @Override
    public void afterPropertiesSet() throws Exception {
        initHSFProvider();
    }

    private void initHSFProvider(){
        if(CollectionUtils.isEmpty(customMaterialContentServiceList)){
            return;
        }

        customMaterialContentServiceList.stream().forEach(service -> {
            String version = "1.0.0";
            PushStrategy pushStrategy = service.getClass().getAnnotation(PushStrategy.class);
            if(pushStrategy != null){
                version += "."+pushStrategy.type();
            }
            try {
                // 初始化 Provider Bean,发布服务
                HSFApiProviderBean hsfApiProviderBean = new HSFApiProviderBean();
                hsfApiProviderBean.setServiceInterface(CustomMaterialContentService.class.getName());
                hsfApiProviderBean.setTarget(service); // target 为 serviceInterface 指定接口的实现对象
                hsfApiProviderBean.setServiceVersion(version);
                hsfApiProviderBean.setServiceGroup("HSF");
                hsfApiProviderBean.init();
            } catch (Exception e) {
                log.error("PushStrategyFactory.initHSFProvider error, e = {}",e);
            }
        });
    }
}

策略:注解使用

@PushStrategy(qStrategy = Q2qQueryServiceImpl.class, type= "low",actionPattern = "action://xxxx")
@Service
public class TestStrategy extends AbstractPushStrategyService {

}

代码实现


public abstract class AbstractPushStrategyService implements CustomMaterialContentService {

    @Resource
    private List<PushQueryService> pushQueryServiceList;

    @Override
    public AmmoResult<CustomPushContent> buildPushMaterialContentVariable(MaterialRequestParam materialRequestParam) {
        PushStrategy pushStrategy = this.getClass().getAnnotation(PushStrategy.class);
        if(pushStrategy == null || materialRequestParam == null){
            return null;
        }

        PushQueryParam pushQueryParam = new PushQueryParam();
        pushQueryParam.setLang(materialRequestParam.getLanguage());
       
        AmmoResult<CustomPushContent> ammoResult = new AmmoResult<>();
        CustomPushContent content = new CustomPushContent();
        Map<String,Object> customContentMap = new HashMap<>(16);

        String targetQuery = pushQueryServiceList.stream()
                .filter(pushQueryService -> pushQueryService.getClass().equals(pushStrategy.qStrategy()))
                .map(pushQueryService -> pushQueryService.getSearchQuery(materialRequestParam.getDeviceId(),pushStrategy.type(),pushQueryParam))
                .findFirst()
                .get();

        String searchActionPattern = pushStrategy.actionPattern();
        if(StringUtils.isBlank(searchActionPattern) || StringUtils.isBlank(searchActionPattern)){
            return null;
        }

        customContentMap.put("targetQuery",targetQuery);
        customContentMap.put("searchAction",String.format(searchActionPattern,targetQuery));

        content.setCustomContentMap(customContentMap);
        ammoResult.setValue(content);
        return ammoResult;
    }


    @Override
    public AmmoResult<Map<String, Object>> buildEmailMaterialContentVariable(MaterialRequestParam materialRequestParam) {
        throw  new RuntimeException("not supported!");
    }

    @Override
    public AmmoResult<Map<String, Object>> buildCustomMaterialContentVariable(MaterialRequestParam materialRequestParam) {
        throw  new RuntimeException("not supported!");
    }

    @Override
    public AmmoResult<Map<String, Object>> buildMaterialDynamicPhrase(MaterialRequestParam materialRequestParam) {
        throw  new RuntimeException("not supported!");
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值