詳解使用Spring的BeanPostProcessor優(yōu)雅的實(shí)現(xiàn)工廠(chǎng)模式
最近學(xué)習(xí)Spring的源碼,發(fā)現(xiàn)一個(gè)利器BeanPostProcessor。這個(gè)后置處理器可以在bean初始化前后對(duì)bean進(jìn)行操作。我們可以在初始化的時(shí)候?qū)ψ约合胍腷ean進(jìn)行緩存,進(jìn)而實(shí)現(xiàn)自己需要處理的邏輯。
背景
當(dāng)我們需要根據(jù)類(lèi)型調(diào)用接口不同實(shí)現(xiàn)的時(shí)候,我們可以使用工廠(chǎng)模式實(shí)現(xiàn)。下面說(shuō)下博主遇到過(guò)的兩次需要使用工廠(chǎng)的場(chǎng)景。
場(chǎng)景一:當(dāng)有一個(gè)模塊,我們需要根據(jù)數(shù)據(jù)庫(kù)的類(lèi)型實(shí)現(xiàn)不同的的sql。我們此時(shí)需要定義一個(gè)接口然后每一種數(shù)據(jù)庫(kù)實(shí)現(xiàn)不同的sql。在調(diào)用時(shí)根據(jù)當(dāng)前的數(shù)據(jù)庫(kù)類(lèi)型調(diào)用對(duì)應(yīng)的實(shí)現(xiàn)類(lèi)。
場(chǎng)景二:我們業(yè)務(wù)需要對(duì)接不同的傳感器設(shè)備,但是總體業(yè)務(wù)邏輯就是獲取數(shù)據(jù),發(fā)送心跳。每一種設(shè)備的數(shù)據(jù)協(xié)議又不一樣。所以需要使用工廠(chǎng),根據(jù)不同的設(shè)備調(diào)用對(duì)應(yīng)的實(shí)現(xiàn)類(lèi)。
工廠(chǎng)模式
靜態(tài)工廠(chǎng)
/** * @Description * @Author Singh * @Date 2020-07-06 21:54 * @Version **/@Servicepublic class HandlerService1 { public <T> void handle(Constant.HandlerType handlerType, T dataDO) { IHandler handler = null; if(handlerType.getType().intValue() == Constant.HandlerType.HANDLE_TYEP_1.getType()){ handler = new Type1Handler(); }else if(handlerType.getType().intValue() == Constant.HandlerType.HANDLE_TYEP_2.getType()){ handler = new Type2Handler(); }else if(handlerType.getType().intValue() == Constant.HandlerType.HANDLE_TYEP_3.getType()){ handler = new Type3Handler(); }else if(handlerType.getType().intValue() == Constant.HandlerType.HANDLE_TYEP_4.getType()){ handler = new Type4Handler(); }else{ throw new RuntimeException('類(lèi)型錯(cuò)誤'); } handler.handle(dataDO); }}
動(dòng)態(tài)工廠(chǎng),通過(guò)class實(shí)現(xiàn)
/** * @Description * @Author Singh * @Date 2020-07-06 21:54 * @Version **/@Servicepublic class HandlerService2 { public <T,H extends IHandler> void handle(Class<H> clzz, T dataDO) throws IllegalAccessException, InstantiationException { IHandler handler = clzz.newInstance(); handler.handle(dataDO); }}
進(jìn)入主題
BeanPostProcessor實(shí)現(xiàn)相同接口的不同實(shí)現(xiàn)bean的工廠(chǎng)
首先定義一個(gè)注解,后續(xù)用來(lái)標(biāo)示bean的處理類(lèi)型
@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic @interface Handler { @AliasFor(annotation = Component.class) String value() default ''; /** * 業(yè)務(wù)處理類(lèi)型 * @return */ Constant.HandlerType handlerType();}
處理類(lèi)型
/** * @Description * @Author Singh * @Date 2020-07-06 21:25 * @Version **/public class Constant { public enum HandlerType{ HANDLE_TYEP_1(1), HANDLE_TYEP_2(2), HANDLE_TYEP_3(3), HANDLE_TYEP_4(4); private Integer type; HandlerType(Integer type) { this.type = type; } public Integer getType() { return type; } }}
定義接口處理
/** * @Description * @Author Singh * @Date 2020-07-06 21:29 * @Version **/public interface IHandler<T> { void handle(T data);}
BeanPostProcessor實(shí)現(xiàn)對(duì)bean后置處理。通過(guò)注解的類(lèi)型緩存bean對(duì)象。
/** * @Description * @Author Singh * @Date 2020-07-06 21:29 * @Version **/@Servicepublic class HandleService implements BeanPostProcessor { private Map<Integer,IHandler> reportDataHandlerMap = new ConcurrentHashMap<>(); public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if(bean instanceof IHandler){ Handler[] reportHandlers = bean.getClass().getAnnotationsByType(Handler.class); if(reportHandlers == null || reportHandlers.length == 0){return bean; } Handler reportHandler = reportHandlers[0]; reportDataHandlerMap.put(reportHandler.handlerType().getType(), (IHandler) bean); } return bean; } public <T> void handle(Constant.HandlerType handlerType, T dataDO) { IHandler reportDataHandler = reportDataHandlerMap.get(handlerType.getType()); if(reportDataHandler == null){ throw new RuntimeException('類(lèi)型錯(cuò)誤'); } reportDataHandler.handle(dataDO); }}
自定義處理器實(shí)現(xiàn),每一種實(shí)現(xiàn)一次。
/** * @Description * @Author Singh * @Date 2020-07-06 21:32 * @Version **/@Handler(handlerType = Constant.HandlerType.HANDLE_TYEP_1 )public class Type1Handler implements IHandler<String>{ @Override public void handle(String data) { }}
到此這篇關(guān)于詳解使用Spring的BeanPostProcessor優(yōu)雅的實(shí)現(xiàn)工廠(chǎng)模式的文章就介紹到這了,更多相關(guān)Spring BeanPostProcessor 工廠(chǎng)模式內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 開(kāi)發(fā)效率翻倍的Web API使用技巧2. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼3. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式4. 什么是Python變量作用域5. ASP常用日期格式化函數(shù) FormatDate()6. CSS3中Transition屬性詳解以及示例分享7. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼8. java加載屬性配置properties文件的方法9. Python數(shù)據(jù)相關(guān)系數(shù)矩陣和熱力圖輕松實(shí)現(xiàn)教程10. python 如何在 Matplotlib 中繪制垂直線(xiàn)
