1.创建操作枚举
@Getter
@AllArgsConstructor
public enum CustomerOperatorType {
/**
* 用户操作记录分类
*/
ACCOUNT("账户冻结/解冻"),
LEVEL("等级记录"),
SCORE( "积分记录");
private final String desc;
}
2.公共基础类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BaseCustomerOpContent<T> {
private String adminName;
private String customerNo;
private T data;
public BaseCustomerOpContent(T data) {
this.data = data;
}
public BaseCustomerOpContent(String adminName, String customerNo) {
this.adminName = adminName;
this.customerNo = customerNo;
}
}
3.公共接口
public interface OperatorRecorder<T> {
/**
* 记录
* @param content
*/
void record(BaseCustomerOpContent<T> content);
}
4.账户操作类
@Data
public class AccountOpContent {
private String accountNo;
}
5.等级记录类
@Data
public class LevelRecordContent {
private String level;
}
6.账户操作记录实现方法
@Component
@RequiredArgsConstructor
@Slf4j
public class AccountOpRecorder implements OperatorRecorder<AccountOpContent> {
@Override
public void record(BaseCustomerOpContent<AccountOpContent> opContent) {
AccountOpContent accountOpContent=opContent.getData();
System.out.println(accountOpContent.getAccountNo());
}
}
7.等级操作记录实现方法
@Component
@RequiredArgsConstructor
@Slf4j
public class LevelOpRecorder implements OperatorRecorder<LevelRecordContent> {
@Override
public void record(BaseCustomerOpContent<LevelRecordContent> content) {
LevelRecordContent c=content.getData();
System.out.println(c.getLevel());
}
}
8.创建获取bean工具类
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextUtil.applicationContext = applicationContext;
}
public static Object getBean(String beanName) {
return applicationContext.getBean(beanName);
}
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}
}
9.创建操作枚举
public enum CustomerOperatorEnum {
/**
* 记录用户
**/
ACCOUNT(CustomerOperatorType.ACCOUNT, AccountOpRecorder.class),
LEVEL(CustomerOperatorType.LEVEL, LevelOpRecorder.class);
private final CustomerOperatorType customerOperatorType;
private final Class<? extends OperatorRecorder<?>> customerOperatorRecorder;
CustomerOperatorEnum(CustomerOperatorType customerOperatorType, Class<? extends OperatorRecorder<?>> customerOperatorRecorder) {
this.customerOperatorType = customerOperatorType;
this.customerOperatorRecorder = customerOperatorRecorder;
}
public Class<? extends OperatorRecorder<?>> getRecorder() {
return customerOperatorRecorder;
}
public static CustomerOperatorEnum findByShippingType(CustomerOperatorType customerOperatorType) {
CustomerOperatorEnum[] values = CustomerOperatorEnum.values();
return Arrays.stream(values).filter(operator -> operator.customerOperatorType == customerOperatorType)
.findFirst()
.orElseThrow(() -> new RuntimeException("不支持的结算类型:" + customerOperatorType));
}
public static OperatorRecorder<?> getRecorder(CustomerOperatorType customerOperatorType) {
CustomerOperatorEnum operatorEnum = findByShippingType(customerOperatorType);
return ApplicationContextUtil.getBean(operatorEnum.getRecorder());
}}
10.使用示例
@GetMapping("demo")
public void demo() {
CustomerOperatorType customerOperatorType = CustomerOperatorType.ACCOUNT;
AccountOpContent accountOpContent = new AccountOpContent();
accountOpContent.setAccountNo("这是测试");
BaseCustomerOpContent baseCustomerOpContent = new BaseCustomerOpContent();
baseCustomerOpContent.setData(accountOpContent);
OperatorRecorder<?> recorder = CustomerOperatorEnum.getRecorder(customerOperatorType);
recorder.record(baseCustomerOpContent);
}
调用结果