A good way to play spring bean

@Controller controller (injection service): used to label the control layer, equivalent to the action layer in struts;

@Service service (injection dao): used to mark the service layer, mainly used for business logic processing;

@repository (realizing Dao access): used to mark the data access layer, also can be said to mark the data access component, that is, Dao component;

@Component (instantiating ordinary pojo into the spring container, equivalent to that in the configuration file): it generally refers to various components, that is, when our class does not belong to various classifications (does not belong to @ Controller, @ Services, etc.), we can use @ component to label this class.

@component annotation is generally used when a tool class that does not need to operate data is put into a container. If you need to operate a database and use dao or mapper, even if it is a tool class, it is recommended to use @ service

First, create an enumeration

public enum PayEnum

{

ALIBABA_PAY("0", "alibaba", "Alipay"),



WEIXIN_PAY("1", "weixin", "WeChat");



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;

}

}

Then create a new interface

public interface ThirdPayPushCall

{

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



public PayEnum style();

}

Then the interface implementation is ignored here. The implementation will be more and more complex according to the specific business

Here's how to put these bean s into a cache for later use

@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;

}


}

Here is the key point. Make an adapter like bean to put the bean key value form of the implementation class of the interface into the container. When using, use the interface to receive the incoming enumeration to get the subclass and call the business logic of the corresponding subclass.
--------
Copyright notice: This is the original article of CSDN blogger "name for better or worse", following CC 4.0 BY-SA copyright agreement. Please attach the original source link and this notice for reprint.
Link: https://blog.csdn.net/qq_3092...

Tags: PHP Struts Spring Database

Posted on Sat, 09 Nov 2019 08:17:36 -0500 by j1bird2k