自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

基于BeanPostProcessor接口+自定義注解玩轉(zhuǎn)策略模式

開發(fā) 前端
有人說不要用業(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

?

責(zé)任編輯:武曉燕 來源: 今日頭條
相關(guān)推薦

2021-12-30 12:30:01

Java注解編譯器

2009-07-06 13:49:29

2015-02-12 15:33:43

微信SDK

2024-10-14 17:18:27

2015-02-12 15:38:26

微信SDK

2023-12-04 07:27:54

SpringMVC方法

2023-10-11 07:57:23

springboot微服務(wù)

2023-10-24 13:48:50

自定義注解舉值驗(yàn)證

2024-04-03 09:18:03

Redis數(shù)據(jù)結(jié)構(gòu)接口防刷

2024-12-27 15:37:23

2013-04-10 18:40:59

微信公眾平臺(tái)接口開發(fā)

2023-06-06 08:01:18

自定義接口響應(yīng)

2023-12-28 08:22:33

響應(yīng)數(shù)據(jù)轉(zhuǎn)換

2021-02-20 11:40:35

SpringBoot占位符開發(fā)技術(shù)

2022-02-17 07:10:39

Nest自定義注解

2020-11-25 11:20:44

Spring注解Java

2024-07-02 11:42:53

SpringRedis自定義

2024-10-09 10:46:41

springboot緩存redis

2023-10-09 07:37:01

2017-08-03 17:00:54

Springmvc任務(wù)執(zhí)行器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)