日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Mybatis ResolverUtil的设计概念

發布時間:2025/5/22 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mybatis ResolverUtil的设计概念 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

設計模式原則之開放-封閉原則

  程序擴展對外開放,修改對外封閉

ResolverUtil

其中有一個接口、兩個內部類,Class對象和Annotation對象被封裝成了Test對象

Test

兩個實現類,核心功能是匹配Class類型

IsA

public static class IsA implements Test {private Class<?> parent;/** Constructs an IsA test using the supplied Class as the parent class/interface. */public IsA(Class<?> parentType) {this.parent = parentType;}/** Returns true if type is assignable to the parent type supplied in the constructor. */@Overridepublic boolean matches(Class<?> type) {return type != null && parent.isAssignableFrom(type);}@Overridepublic String toString() {return "is assignable to " + parent.getSimpleName();} }

AnnotatedWith

public static class AnnotatedWith implements Test {private Class<? extends Annotation> annotation;/** Constructs an AnnotatedWith test for the specified annotation type. */public AnnotatedWith(Class<? extends Annotation> annotation) {this.annotation = annotation;}/** Returns true if the type is annotated with the class provided to the constructor. */@Overridepublic boolean matches(Class<?> type) {return type != null && type.isAnnotationPresent(annotation);}@Overridepublic String toString() {return "annotated with @" + annotation.getSimpleName();} }

使用

String packageName = "com/wjz/mybatis/type/scan"; ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<Class<?>>(); resolverUtil.find(new ResolverUtil.IsA(CommonService.class), packageName); Set<Class<? extends Class<?>>> handlerSet = resolverUtil.getClasses();

如此設計

我們可以實現Test接口,傳參find方法時傳入,而不用修改ResolverUtil的內部方法。

package com.wjz.mybatis.type.scan;import org.apache.ibatis.io.ResolverUtil.Test;public class Testtest implements Test {@Overridepublic boolean matches(Class<?> type) {if (type == Testtest.class) {return true;}return false;}}

?

轉載于:https://www.cnblogs.com/BINGJJFLY/p/9905198.html

總結

以上是生活随笔為你收集整理的Mybatis ResolverUtil的设计概念的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。