有人說不要用業(yè)務(wù)類實(shí)現(xiàn)InitializingBean接口,可以借助自定義的注解來實(shí)現(xiàn)類似的邏輯。那我們換這種思路實(shí)現(xiàn)下。
前言
有人說不要用業(yè)務(wù)類實(shí)現(xiàn)InitializingBean接口,可以借助自定義的注解來實(shí)現(xiàn)類似的邏輯。那我們換這種思路實(shí)現(xiàn)下。
定義注解PayType
/**
* 消息通知類型注解
* @author francis
*
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface PayType {
/**
* 支付類型
* @return
*/
PayTypeEnum value();
}
其中PayTypeEnum枚舉
@Getter
@AllArgsConstructor
public enum PayTypeEnum {
WX("WX", "微信"),
ZFB("A","支付寶支付"),;
private String type;
private String desc;
}
BeanPostProcessor(Bean后置處理器)
功能:它是Spring中定義的接口,在Spring容器的創(chuàng)建過程中(具體為Bean初始化前后)會(huì)回調(diào)BeanPostProcessor中定義的兩個(gè)方法。
- postProcessBeforeInitialization方法
會(huì)在每一個(gè)bean對(duì)象的初始化方法調(diào)用之前回調(diào)
- postProcessAfterInitialization方法
會(huì)在每個(gè)bean對(duì)象的初始化方法調(diào)用之后被回調(diào)
源碼如下:
package org.springframework.beans.factory.config;
import org.springframework.beans.BeansException;
import org.springframework.lang.Nullable;
public interface BeanPostProcessor {
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
兩個(gè)方法入?yún)⒄f明:
bean:容器正在創(chuàng)建的那個(gè)bean的引用beanName:容器正在創(chuàng)建的那個(gè)bean的名稱
策略工廠實(shí)現(xiàn)BeanPostProcessor接口
getClass() 返回此 Object 的運(yùn)行時(shí)該對(duì)象的類. 該方法返回一個(gè)Class對(duì)象, 可以通過該對(duì)象可以獲取某個(gè)類的相關(guān)信息, 如構(gòu)造方法 屬性 方法 等
import com.example.demo.celuemoshi.PayService;
import com.example.demo.celuemoshi.PayTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@Component
@Slf4j
public class StrategyFactory2 implements BeanPostProcessor {
private static final Map<PayTypeEnum, PayService> serviceMap = new ConcurrentHashMap<>();
/**
* @param bean 實(shí)例化bean的引用
* @param beanName 實(shí)例化bean的名字
* @return
* @throws
@Nullable
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// 只對(duì)實(shí)現(xiàn)了PayService的類做操作
if (bean instanceof PayService) {
//獲取對(duì)象運(yùn)行時(shí)該對(duì)象的類
Class<?> clazz = bean.getClass();
//獲取自定義的注解
PayType annotation = clazz.getAnnotation(PayType.class);
//綁定對(duì)應(yīng)關(guān)系
serviceMap.put(annotation.value(), (PayService) bean);
}
return bean;
}
/**
* 尋找對(duì)應(yīng)得策略處理器
*/
public PayService getHandler(PayTypeEnum type){
return serviceMap.get(type);
}
}
業(yè)務(wù)類加上@PayType
@Service
@Slf4j
@PayType(PayTypeEnum.ZFB)
public class AliService implements PayService {
public Boolean pay(String type) {
log.info("調(diào)用阿里支付={}",type);
return true;
}
}
import com.example.demo.strategy2.PayType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Service
@Slf4j
@PayType(PayTypeEnum.WX)
public class WxServiceA implements PayService {
@Override
public Boolean pay(String type) {
log.info("調(diào)用微信支付={}",type);
return true;
}
}
定義一個(gè)控制器測試
import com.example.demo.celuemoshi.StrategyFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PayController {
@GetMapping("pay/{type}")
public boolean pay(@PathVariable("type") String type){
StrategyFactory.getService(type);
return true;
}
}
測試結(jié)果
測試微信支付:http://localhost:10001/pay/wx

測試阿里支付:http://localhost:10001/pay/zfb

?