constant 定义枚举类
package com.jiniutech.common.openacclink.constant;
/**
* EnterStrategyEnum
*
* @author zhangLinwei
* @since 2021/11/8 21:38
*/
public enum OpenAccountLinkStrategyEnum {
/**
* 华福
*/
HUAFU("mp-service-huafu");
private String type;
OpenAccountLinkStrategyEnum(String type) {
this.type = type;
}
public String getType() {
return type;
}
}
yml 文件内加入默认项
impl:
default:
information: mp-service-huafu
service 定义方法
接口抽象类:
package com.jiniutech.common.openacclink.service;
/**
* IOpenAccountLinkService
*
* @author zhangLinwei
* @since 2021/11/8 21:56
*/
public interface IOpenAccountLinkService {
/**
* 获取开户链接
*
* @return
*/
public String getGenericOpenAccLink(Integer userId, Integer branchNo);
}
接口实现类:
package com.jiniutech.common.openacclink.service.impl;
import com.jiniutech.common.annotation.HandlerBrokerStrategy;
import com.jiniutech.common.openacclink.constant.OpenAccountLinkStrategyEnum;
import com.jiniutech.common.openacclink.service.IOpenAccountLinkService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* OpenAccountLinkService
*
* @author zhangLinwei
* @since 2021/11/8 21:55
*/
@Service
@HandlerBrokerStrategy(value = OpenAccountLinkStrategyEnum.HUAFU)
public class OpenAccountLinkService implements IOpenAccountLinkService {
@Value("${open-account-link.huafu.default}")
private String defaultLink;
@Value("${open-account-link.huafu.staff}")
private String linkWithStaff;
@Override
public String getGenericOpenAccLink(Integer userId, Integer branchNo) {
if (userId == null) {
return defaultLink;
} else {
return linkWithStaff + "&yddh=" + userId + "&branchno=" + (branchNo != null ? branchNo : 0);
}
}
}
config 策略容器获取bean实体、策略加载
package com.jiniutech.common.openacclink.config;
import com.jiniutech.common.openacclink.service.IOpenAccountLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
* OpenAccLinkStrategyTypeConfig
*
* @author zhangLinwei
* @since 2021/11/8 22:11
*/
@Component
public class BrokerStrategyTypeConfig {
@Autowired
private ApplicationContext applicationContext;
public static Map<String, Class<IOpenAccountLinkService>> linkStrategyBeanMap = new HashMap();
/**
* 根据类型获取校验类型的bean
*
* @param type type
* @return
*/
public IOpenAccountLinkService getOpenAccountLinkService(String type) {
Class<IOpenAccountLinkService> strategyClass = linkStrategyBeanMap.get(type);
if (strategyClass == null) {
throw new IllegalArgumentException("没有对应的校验类型");
}
//从容器中获取对应的策略Bean
return applicationContext.getBean(strategyClass);
}
}
package com.jiniutech.common.openacclink.config;
import com.jiniutech.common.annotation.HandlerBrokerStrategy;
import com.jiniutech.common.openacclink.service.IOpenAccountLinkService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import java.util.Map;
/**
* 策略加载类
* @author: zlw
* @create: 2021-11-09
*/
@Configuration
public class HandlerBrokerProcessor implements ApplicationContextAware {
/**
* 获取所有的策略Beanclass 加入HandlerOrderContext属性中
* @param applicationContext applicationContext
* @throws BeansException beansException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
Map<String, Object> orderStrategyMap = applicationContext.getBeansWithAnnotation(HandlerBrokerStrategy.class);
orderStrategyMap.forEach((k, v) -> {
if (v instanceof IOpenAccountLinkService) {
Class<IOpenAccountLinkService> orderStrategyClass = (Class<IOpenAccountLinkService>) v.getClass();
String type = orderStrategyClass.getAnnotation(HandlerBrokerStrategy.class).value().getType();
//将class加入map中,type作为key
BrokerStrategyTypeConfig.linkStrategyBeanMap.put(type, orderStrategyClass);
}
});
}
}
controller 层方法实现
package com.jiniutech.common.openacclink.controller;
import com.jiniutech.common.openacclink.config.BrokerStrategyTypeConfig;
import com.jiniutech.common.openacclink.service.IOpenAccountLinkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* OpenAccountLink
*
* @author zhangLinwei
* @since 2021/11/8 21:52
*/
@Slf4j
@RestController
@RequestMapping("/openAccountLink")
@Api(value = "开户链接服务", description = "开户链接服务")
public class OpenAccountLinkController {
@Resource
private BrokerStrategyTypeConfig brokerStrategyTypeConfig;
@Value("${impl.default.information}")
private String defaultInfoImpl;
@GetMapping("/generic-get-openAccLink")
@ApiOperation(
value = "(泛化)获取开户链接",
notes = "(泛化)获取开户链接",
response = String.class,
httpMethod = "GET")
public String getOpenAccLink(@ApiParam("所属券商") @RequestParam(required = false) String informationImpl,
@ApiParam("投顾的userId(非必传,不传就是默认自行开户)") @RequestParam(required = false) Integer userId,
@ApiParam("投顾的分公司号(非必传)") @RequestParam(required = false) Integer branchNo) {
IOpenAccountLinkService openAccountLinkService = brokerStrategyTypeConfig
.getOpenAccountLinkService(StringUtils.isNotBlank(informationImpl) ? informationImpl : defaultInfoImpl);
return openAccountLinkService.getGenericOpenAccLink(userId, branchNo);
}
}