Java 开发经验谈:动手造轮子:实现一个简单的 -AOP- 框架
{IReadOnlyList<IInterceptor> ResolveInterceptors(IInvocation invocation);}
public interface IEnricher<in TContext>{void Enrich(TContext context);}public interface IInvocationEnricher : IEnricher<IInvocation>{}
// apply enrichersforeach (var enricher in FluentAspects.AspectOptions.Enrichers){try{enricher.Enrich(invocation);}catch (Exception ex){InvokeHelper.OnInvokeException?.Invoke(ex);}}
// get delegatevar builder = PipelineBuilder.CreateAsync(completeFunc);foreach (var interceptor in interceptors){builder.Use(interceptor.Invoke);}return builder.Build();
IServiceCollection services = new ServiceCollection();services.AddFluentAspects(options =>{// 注册拦截器配置 options.NoInterceptProperty<IFly>(f => f.Name);
// 使用 Castle 生成代理 services.AddFluentAspects(options =>{// 注册拦截器配置 options.NoInterceptProperty<IFly>(f => f.Name);
services.AddTransientProxy<IFly, MonkeyKing>();services.AddSingletonProxy<IEventBus, EventBus>();services.AddDbContext<TestDbContext>(options =>{options.UseInMemoryDatabase("Test");});services.AddScopedProxy<TestDbContext>();
var serviceProvider = services.BuildServiceProvider();
IServiceCollection services = new ServiceCollection();services.AddTransient<IFly, MonkeyKing>();services.AddSingleton<IEventBus, EventBus>();services.AddDbContext<TestDbContext>(options =>{options.UseInMemoryDatabase("Test");});
var serviceProvider = services.BuildFluentAspectsProvider(options =>{options.InterceptAll().With<TestOutputInterceptor>(output);});
// 使用 Castle 来生成代理 var serviceProvider = services.BuildFluentAspectsProvider(options =>{options.InterceptAll().With<TestOutputInterceptor>(output);}, builder => builder.UseCastle());
// 忽略命名空间为 Microsoft/System 的服务类型 var serviceProvider = services.BuildFluentAspectsProvider(options =>{
最后
以上分享的全部分布式技术专题+面试解析+相关的手写和学习的笔记 pdf,高清完整版戳这里免费领取
还有更多 Java 笔记分享如下:
评论