java根据枚举值动态获取bean示例

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);
    }

调用结果 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今朝花落悲颜色

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值