成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

基于BeanPostProcessor接口+自定義注解玩轉策略模式

開發 前端
有人說不要用業務類實現InitializingBean接口,可以借助自定義的注解來實現類似的邏輯。那我們換這種思路實現下。

前言

有人說不要用業務類實現InitializingBean接口,可以借助自定義的注解來實現類似的邏輯。那我們換這種思路實現下。

定義注解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容器的創建過程中(具體為Bean初始化前后)會回調BeanPostProcessor中定義的兩個方法。

  • postProcessBeforeInitialization方法

會在每一個bean對象的初始化方法調用之前回調

  • postProcessAfterInitialization方法

會在每個bean對象的初始化方法調用之后被回調

源碼如下:

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

兩個方法入參說明:

bean:容器正在創建的那個bean的引用beanName:容器正在創建的那個bean的名稱

策略工廠實現BeanPostProcessor接口

getClass() 返回此 Object 的運行時該對象的類. 該方法返回一個Class對象, 可以通過該對象可以獲取某個類的相關信息, 如構造方法 屬性 方法 等

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 實例化bean的引用
* @param beanName 實例化bean的名字
* @return
* @throws
@Nullable
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// 只對實現了PayService的類做操作
if (bean instanceof PayService) {
//獲取對象運行時該對象的類
Class<?> clazz = bean.getClass();
//獲取自定義的注解
PayType annotation = clazz.getAnnotation(PayType.class);
//綁定對應關系
serviceMap.put(annotation.value(), (PayService) bean);
}
return bean;
}
/**
* 尋找對應得策略處理器
*/
public PayService getHandler(PayTypeEnum type){
return serviceMap.get(type);
}
}

業務類加上@PayType

@Service
@Slf4j
@PayType(PayTypeEnum.ZFB)
public class AliService implements PayService {

public Boolean pay(String type) {
log.info("調用阿里支付={}",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("調用微信支付={}",type);
return true;
}
}

定義一個控制器測試

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

測試結果

測試微信支付:http://localhost:10001/pay/wx

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

?

責任編輯:武曉燕 來源: 今日頭條
相關推薦

2021-12-30 12:30:01

Java注解編譯器

2009-07-06 13:49:29

2015-02-12 15:33:43

微信SDK

2015-02-12 15:38:26

微信SDK

2024-10-14 17:18:27

2023-12-04 07:27:54

SpringMVC方法

2023-10-11 07:57:23

springboot微服務

2023-10-24 13:48:50

自定義注解舉值驗證

2024-04-03 09:18:03

Redis數據結構接口防刷

2024-12-27 15:37:23

2013-04-10 18:40:59

微信公眾平臺接口開發

2023-06-06 08:01:18

自定義接口響應

2023-12-28 08:22:33

響應數據轉換

2022-02-17 07:10:39

Nest自定義注解

2024-10-09 10:46:41

springboot緩存redis

2021-02-20 11:40:35

SpringBoot占位符開發技術

2024-07-02 11:42:53

SpringRedis自定義

2020-11-25 11:20:44

Spring注解Java

2017-08-03 17:00:54

Springmvc任務執行器

2023-10-09 07:37:01

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美精品一区二区三区在线 | 中文字幕在线视频一区二区三区 | 久久国产精品精品 | 国产剧情一区 | 国产91丝袜 | 日本中出视频 | 网站国产 | 天天综合网91 | 97久久精品午夜一区二区 | 久久av网站 | 国产特一级黄色片 | 亚洲激情一区二区 | 亚洲精品一区二三区不卡 | 国产成人免费视频 | 精品一区二区久久久久久久网站 | 欧美13videosex性极品 | 亚洲精品一区二三区不卡 | 一区二区中文字幕 | 亚洲精品一区中文字幕乱码 | 国产精品久久久久aaaa九色 | 国产欧美精品一区二区三区 | 欧美看片 | 国产大学生情侣呻吟视频 | 天堂一区 | 2019天天干天天操 | 激情久久久久 | 岛国视频 | 久久国产精品一区二区 | 精品一区二区三区在线视频 | 国产精品成人一区二区 | 美女国产精品 | 欧美一级片 | 国产麻豆乱码精品一区二区三区 | 91精品国产综合久久精品 | 中文字幕一二三区 | 久久久久国产精品 | 亚洲精品一区av在线播放 | 国产成人精品久久二区二区91 | av喷水 | 精品粉嫩aⅴ一区二区三区四区 | 99久久免费观看 |