【乐企文件生成工程】如何通过一个接口,管理Spring上下文的容器

1、需求

乐企文件生成工程中,有不同的票种,每个票种对应的实现类不一样,现在希望通过一个类就能管理所有的票种实现类,不用每次都使用@Autowired或者 @Resource逐个注入。

2、做法

创建一个DataComponent,实现Spring中的ApplicationContextAware以及PriorityOrdered接口,代码如下:


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.PriorityOrdered;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * User: yanjun.hou
 * Date: 2024/12/10 17:51
 * Description:获取指定的上下文组件
 */
@Component
public class DataComponent implements ApplicationContextAware, PriorityOrdered {

    private static ApplicationContext applicationContext;

    public static final Map<Class, Object> dataTableMap = new ConcurrentHashMap<>();

    /**
     * ApplicationContextAware 需要获取应用上下文
     *
     * @param applicationContext 应用上下问
     * @throws BeansException bean 异常
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        DataComponent.applicationContext = applicationContext;
    }

    /**
     * PriorityOrdered 初始化优先级(赋最小值,最先加载)
     *
     * @return 优先级
     */
    @Override
    public int getOrder() {
        return PriorityOrdered.HIGHEST_PRECEDENCE;
    }

    //@SuppressWarnings("all") 作用:忽略警告
    @SuppressWarnings("all")
    public static <T> T of(Class<T> clazz) {

        T instance = (T) dataTableMap.get(clazz);
        if (null != instance) {
            return instance;
        }
        // 当获取不到bean时,将bean通过class的方式添加到
        dataTableMap.put(clazz, bean(clazz));
        return (T) dataTableMap.get(clazz);
    }

    /**
     * 通过bean的名称加载bean
     *
     * @param beanName bean名称
     * @param <T>      bean范型
     * @return bean
     */
    @SuppressWarnings("all")
    private static <T> T bean(String beanName) {
        return (T) applicationContext.getBean(beanName);
    }

    @SuppressWarnings("all")
    private static <T> T bean(Class clazz) {
        return (T) applicationContext.getBean(clazz);
    }
}

3、用法

package com.lq.invoice.factory;

import com.lq.invoice.service.file.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * User: yanjun.hou
 * Date: 2024/10/19 19:15
 * Description:
 */
@Component
public class InvoiceFileFactory {
    public InvoiceFileService getInvoiceFileService(String tdys) {
        if (StringUtils.isEmpty(tdys)) {
            return DataComponent.of(BaseInvoiceFileServiceImpl.class);
        }
        switch (tdys) {
            case "04":
                return DataComponent.of(GoodsTransportInvoiceFileImpl.class);//货物运输
            case "03":
                return DataComponent.of(BuildingInvoiceFileImpl.class);//建筑服务
            case "05":
                return DataComponent.of(RealPropertySellInvoiceFileImpl.class);//不动产销售 暂不支持 后续开发
            case "06":
                return DataComponent.of(RealPropertyRentInvoiceFileImpl.class);//不动产租赁
            case "09":
                return DataComponent.of(TravellerTransportInvoiceFileImpl.class);//旅客运输
            default:
                return DataComponent.of(BaseInvoiceFileServiceImpl.class);
        }
    }
}

OK,通过DataComponent.of(xxx.class)就能实现需要什么组件获取什么组件的需求了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

还算善良_

如果对你的工作有所帮助,拜托啦

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

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

打赏作者

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

抵扣说明:

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

余额充值