extends IInterceptor>> entry : Warehouse.interceptorsIndex.entrySet()) { Class IInterceptor iInterceptor = interceptorClass.getConstructor().newInstance(); iInterceptor.init(context); Warehouse.interceptors.add(iInterceptor); 在 init 方法中,做的主要事情就是遍历所有 IInterceptor class 并创建出对象,调用其 init 方法,完成初始化操作。 iInterceptor = Warehouse.interceptors.get(index); iInterceptor.process(postcard, new InterceptorCallback
extends IInterceptor>> entry : Warehouse.interceptorsIndex.entrySet()) { Class IInterceptor iInterceptor = interceptorClass.getConstructor().newInstance(); iInterceptor.init(context); Warehouse.interceptors.add(iInterceptor); 在 init 方法中,做的主要事情就是遍历所有 IInterceptor class 并创建出对象,调用其 init 方法,完成初始化操作。 iInterceptor = Warehouse.interceptors.get(index); iInterceptor.process(postcard, new InterceptorCallback
自定义拦截器需要实现IInterceptor接口,并且添加@Interceptor的注解,其中priority为拦截器的优先级,值越小,优先级越高;然后实现pocess()和init()方法。 拦截器1:Test1Interceptor @Interceptor(priority = 5) public class Test1Interceptor implements IInterceptor IInterceptor iInterceptor = interceptorClass.getConstructor().newInstance(); iInterceptor.init(context); Warehouse.interceptors.add(iInterceptor); iInterceptor = Warehouse.interceptors.get(index); iInterceptor.process(postcard, new InterceptorCallback
接下来进入正题
1、定义接口IInterceptor
定义好我们AOP需要实现的接口,不同职责可以定义不同接口,大家根据实际情况划分
internal interface IInterceptor
{ }
internal interface IInterceptorAction : IInterceptor
{
///
: ((fromContext: NavDestinationContext) => void) | undefined = (fromContext) => { console.log("IInterceptor 添加多个拦截器单个拦截器的使用方式和全局拦截器是类似的,首先实现接口IInterceptor,然后使用setInterceptor()方法注册拦截器,,代码示例如下:export interface IInterceptor process: ", 100, context.name) return context } } as IInterceptor) // 第二种方式设置拦截器,类的实例对象的形式 ZRouter.setInterceptor(new UrlInterceptor())}export class UrlInterceptor implements IInterceptor { id=69&name=harAPage3")})拦截器:export class UrlInterceptor implements IInterceptor { // 设置拦截器优先级,数值越大则优先执行
public class DynamicProxy<T> : RealProxy
{
public T Instance = default;
public IInterceptor T))
{
}
public T GetInstance< Target, TInterceptor>() where TInterceptor: IInterceptor DispatchProxy
{
///
= (provider) => { var target = provider.GetService(implType); List<IInterceptor > interceptors = interceptorTypes.ToList().ConvertAll<IInterceptor>(interceptorType => { return provider.GetService(interceptorType) as IInterceptor; });
public void Add(string name) => Console.WriteLine($"新增产品:{name}"); } public class LoggerInterceptor : IInterceptor static void Main(string[] args) { ProxyGenerator generator = new ProxyGenerator(); IInterceptor }"); } static void Main(string[] args) { ProxyGenerator generator = new ProxyGenerator(); IInterceptor Console.WriteLine($"异步新增产品:{name}"); }); } } public class LoggerInterceptor : IInterceptor public class LoggerInterceptor : IInterceptor { readonly LoggerAsyncInterceptor interceptor;
interceptor The interceptor object to register. 9: */ 10: public function addInterceptor(interceptor:IInterceptor interceptor The interceptor object to remove. 19: */ 20: public function removeInterceptor(interceptor:IInterceptor 31: public function run():void { 32: var n:int = interceptors.length; 33: var interceptor:IInterceptor
: ((fromContext: NavDestinationContext) => void) | undefined = (fromContext) => { console.log("IInterceptor InterceptorInfo) => void) | undefined = (info)=>{ if (info.notRegistered) return console.log("IInterceptor 自定义拦截器 自定义拦截器,首先实现接口 IInterceptor,然后使用 setInterceptor()方法注册拦截器,,代码示例如下: export interface IInterceptor (): void { ZRouter.setInterceptor(new UrlInterceptor()) } export class UrlInterceptor implements IInterceptor id=69&name=harAPage3") }) 拦截器: export class UrlInterceptor implements IInterceptor { // 设置拦截器优先级,数值越大则优先执行
Castle.Core的拦截接口
'''
创建 UserServiceInterceptor 拦截类,继承自 IInterceptor: public class UserServiceInterceptor:IInterceptor {
AOP拦截器
在Blog.Core新建文件夹AOP,并添加拦截器BlogLogAOP,并设计其中用到的日志记录Logger方法或者类
关键的一些知识点,注释中已经说明了,主要是有以下:
1、继承接口IInterceptor 2、实例化接口IINterceptor的唯一方法Intercept
3、void Proceed();表示执行当前的方法和object ReturnValue { get; set; }执行后调用,object ///
第一步:创建拦截器
下面是一个简单的拦截器示例,该拦截器的功能是显示被拦截的方法名称、参数列表和返回结果
1 ///
extends IInterceptor>> interceptorsIndex = new UniqueKeyTreeMap<>("More than one interceptors use same extends IInterceptor>> entry : Warehouse.interceptorsIndex.entrySet()) { Class //反射机制构造自定义的每一个拦截器实例 IInterceptor iInterceptor = interceptorClass.getConstructor ().newInstance(); iInterceptor.init(context); iInterceptor = Warehouse.interceptors.get(index); iInterceptor.process(postcard, new InterceptorCallback
如页面不存在时跳转统一错误页)动画与 Flag:支持转场动画(withTransition())和启动模式(withFlags())2、拦截器(Interceptor)自定义拦截逻辑:在跳转前执行登录验证、权限检查等操作(如 IInterceptor MutableList<RouteMeta>>() // 拦截器表:优先级 -> 拦截器类 val interceptors = sortedMapOf<Int, Class<out IInterceptor /** * 拦截器接口 */interface IInterceptor { fun process(postcard: Postcard, callback: InterceptorCallback Interceptors : IInterceptorGroup { override fun loadInto(interceptors: MutableMap<Int, Class<out IInterceptor val interceptorGroup = ARouter$$Interceptors() val interceptors = mutableMapOf<Int, Class<out IInterceptor
3、添加AOP拦截器
在api层新建文件夹AOP,添加拦截器BlogLogAOP,并设计其中用到的日志记录Logger方法或者类
关键的一些知识点,注释中已经说明了,主要是有以下:
1、继承接口IInterceptor 2、实例化接口IINterceptor的唯一方法Intercept
3、void Proceed();表示执行当前的方法
4、执行后,输出到日志文件。 namespace blog.core.test3._0.AOP
{
///
首先,引用Castle.Core 然后,定义拦截器,实现IInterceptor接口 public class LoggerInterceptor : IInterceptor { private IProductRepository target = new ProductRepository(); ProxyGenerator generator = new ProxyGenerator(); IInterceptor 由于IAsyncInterceptor接口和DP框架中的IInterceptor接口没有关联,所以我们还需要一个同步拦截器,此处直接修改旧的同步拦截器: public class LoggerInterceptor : IInterceptor { private readonly LoggerAsyncInterceptor interceptor; public LoggerInterceptor } } 从代码中可以看到,异步拦截器LoggerAsyncInterceptor具有名为ToInterceptor()的扩展方法,该方法可以将IAsyncInterceptor接口的对象转换为IInterceptor
Interceptors 是注册了声明 Interceptor 注解, 并实现 IInterceptor 接口的类, Providers 是注册了声明 Route 注解, 并实现了 IProvider postcard) { // 递归退出条件 if (index < Warehouse.interceptors.size()) { // 获取要执行的拦截器 IInterceptor iInterceptor = Warehouse.interceptors.get(index); // 执行拦截 iInterceptor.process(postcard postcard) { // 递归退出条件 if (index < Warehouse.interceptors.size()) { // 获取要执行的拦截器 IInterceptor iInterceptor = Warehouse.interceptors.get(index); // 执行拦截 iInterceptor.process(postcard
iInterceptor = Warehouse.interceptors.get(index); iInterceptor.process(postcard, new InterceptorCallback IInterceptor iInterceptor = interceptorClass.getConstructor().newInstance(); static List<IInterceptor> interceptors = new ArrayList<>(); ... } Warehouse存了哪些信息呢? extends IInterceptor>>,也就是WareHouse.interceptorsIndex的类型。 loadInfo方法体内,是用接收的map来put当前module所有拦截器的class,即使用 @Interceptor 注解并实现 IInterceptor 接口的类。