spring bean的一种不错玩法

@controller 控制器(注入服务):用于标注控制层,相当于struts中的action层;

@service 服务(注入dao):用于标注服务层,主要用来进行业务的逻辑处理;

@repository(实现dao访问):用于标注数据访问层,也可以说用于标注数据访问组件,即DAO组件;

@component (把普通pojo实例化到spring容器中,相当于配置文件中的) :泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类。

@component注解一般是不需要操作数据的工具类放入容器时使用,如果需要操作数据库使用dao或者mapper时即使是工具类建议使用@service

首先建立一个枚举

public enum PayEnum

{

    ALIBABA_PAY("0", "alibaba", "支付宝"),

    

    WEIXIN_PAY("1", "weixin", "微信");

    private String code;

    private String display;

    

    private String displayCN;

    private PayEnum(String code, String display, String displayCN)

    {

        this.code = code;

        this.display = display;

        this.displayCN = displayCN;

    }

    public static PayEnumfromCode(String code)

    {

        PayEnum[] values = values();

        for (PayEnumtype : values)

        {

            if (type.code.equals(code))

            {

                return type;

            }

        }

        return null;

    }

    public static PayEnumbuildDisplay(String display)

    {

        for (PayEnumtype : values())

        {

            if (type.display.equals(display))

            {

                return type;

            }

        }

        return null;

    }

    public static PayEnumbuildDisplayCN(String displayCN)

    {

        for (PayEnumtype : values())

        {

            if (type.displayCN.equals(displayCN))

            {

                return type;

            }

        }

        return null;

    }

    

    public int intCode()

    {

        return Integer.valueOf(this.code).intValue();

    }

    

    @Override

    public String code()

    {

        return code;

    }

    

    @Override

    public String display()

    {

        return display;

    }

    public String displayCN() {

        return displayCN;

    }

}

然后新建接口

public interface ThirdPayPushCall

{

    public void push(Account account, int i, int code);

    

    public PayEnum style();

}

然后是接口实现这里忽略,实现根据具体业务会多而繁杂

下面就是如何将这些bean放入一个缓存中方便后面使用

@Service

public class PayDispatcherServiceImpl implements BeanPostProcessor

{

    private static Map<PayEnum, ThirdPayPushCall> calls = new HashMap<ThirdPayEnum, ThirdPayPushDownCall>();

    public static ThirdPayPushCall getCall(PayEnum pay)

    {

        return calls.get(pay);

    }

    @Override

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException

    {

        if (bean instanceof ThirdPayPushCall)

        {

            ThirdPayPushCall call = (ThirdPayPushCall) bean;

            calls.put(((ThirdPayPushCall) bean).style(), call);

        }

        

        return bean;

    }

    @Override

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException

    {

        return bean;

    }

}

这里是重点,做一个类似适配器的bean将接口的实现类的bean按键值形式放入容器中,使用时用接口接收传入枚举获取子类调用相应子类的业务逻辑。

ThirdPayPushCall PayPushCall = PayDispatcherServiceImpl.getCall(payStyle);

                PayPushCall.push(account);

发布了52 篇原创文章 · 获赞 16 · 访问量 67万+

猜你喜欢

转载自blog.csdn.net/qq_30920479/article/details/102954364
今日推荐