想回家种地养猪

导航

ABP框架详解(一)ABPBootstrapper

在ABP框架的AbpBootstrapper主要用于框架的基本配置的注册和初始化,在Web应用启动阶段实例化一个AbpBootstrapper并调用Initialize方法初始化,该类主要包含两个公有属性分别是IIocManager和IAbpModuleManager

  1. IIocManager内部包装了一个Castle的依赖注入容器IWindsorContainer,所有类型的注册,解析还有后面实现的AOP机制的拦截器都是注册在该容器中的,将具体的注册还有解析功能分别包含在其父接口IIocRegistrar和IIocResolver中,其中AddConventionalRegistrar,RegisterAssemblyByConvention(Assembly assembly),RegisterAssemblyByConvention(Assembly assembly, ConventionalRegistrationConfig config)三个方法需要特别注意,第一个方法,是向IocManager的一个私有泛型集合List<IConventionalDependencyRegistrar>注册注册机制,通常所有的Module类的预初始化方法中调用以决定哪些类型需要被注册(如果没有就无需调用),比如在Abp程序集中的BasicConventionalRegistrar实现的就是搜索并注册指定的程序集中的所有实现了ITransientDependency,ISingletonDependency和IInterceptor的类并注册到依赖容器中,第二,第三个方法执行真正的注册逻辑,通常在一个个具体的Module的初始化方法中调用,传入当前Module所属的程序集,迭代List<IConventionalDependencyRegistrar>将当前程序集作为参数执行注册,第二,第三个方法的区别在于第三个方法多了一个ConventionalRegistrationConfig参数,以决定是否还需要搜索当前程序及中的IWindsorInstaller的实现类进行注册,默认是需要的。
  2. IAbpModuleManager主要用于管理所有的模块默认也就是一个个的程序集(一个模块对应一个程序集),主要用于搜索到所有的Module以及他们的依赖Module,首先执行所有Module的PreInitialize方法再执行所有的Initialize,最后执行所有的PostInitialize,执行IAbpModuleManager的ShutdownModules时顺序颠倒依次执行所有具体Module的ShutDown方法。

1.在执行AbpBootstrapper的Initialize()方法时首先会执行

IocManager.IocContainer.Install(new AbpCoreInstaller());

来注册系统框架级的所有配置类,具体代码如下

internal class AbpCoreInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(
                Component.For<IUnitOfWorkDefaultOptions, UnitOfWorkDefaultOptions>().ImplementedBy<UnitOfWorkDefaultOptions>().LifestyleSingleton(),
                Component.For<INavigationConfiguration, NavigationConfiguration>().ImplementedBy<NavigationConfiguration>().LifestyleSingleton(),
                Component.For<ILocalizationConfiguration, LocalizationConfiguration>().ImplementedBy<LocalizationConfiguration>().LifestyleSingleton(),
                Component.For<IAuthorizationConfiguration, AuthorizationConfiguration>().ImplementedBy<AuthorizationConfiguration>().LifestyleSingleton(),
                Component.For<IFeatureConfiguration, FeatureConfiguration>().ImplementedBy<FeatureConfiguration>().LifestyleSingleton(),
                Component.For<ISettingsConfiguration, SettingsConfiguration>().ImplementedBy<SettingsConfiguration>().LifestyleSingleton(),
                Component.For<IModuleConfigurations, ModuleConfigurations>().ImplementedBy<ModuleConfigurations>().LifestyleSingleton(),
                Component.For<IEventBusConfiguration, EventBusConfiguration>().ImplementedBy<EventBusConfiguration>().LifestyleSingleton(),
                Component.For<IMultiTenancyConfig, MultiTenancyConfig>().ImplementedBy<MultiTenancyConfig>().LifestyleSingleton(),
                Component.For<ICachingConfiguration, CachingConfiguration>().ImplementedBy<CachingConfiguration>().LifestyleSingleton(),
                Component.For<IAuditingConfiguration, AuditingConfiguration>().ImplementedBy<AuditingConfiguration>().LifestyleSingleton(),
                Component.For<IAbpStartupConfiguration, AbpStartupConfiguration>().ImplementedBy<AbpStartupConfiguration>().LifestyleSingleton(),
                Component.For<ITypeFinder>().ImplementedBy<TypeFinder>().LifestyleSingleton(),
                Component.For<IModuleFinder>().ImplementedBy<DefaultModuleFinder>().LifestyleTransient(),
                Component.For<IAbpModuleManager>().ImplementedBy<AbpModuleManager>().LifestyleSingleton(),
                Component.For<ILocalizationManager, LocalizationManager>().ImplementedBy<LocalizationManager>().LifestyleSingleton()
                );
        }
    }

2.接着解析AbpStartupConfiguration的实例调用其Initialize()来完成所有配置项的初始设值。

3.解析IAbpModuleManager的实例调用其InitializeModules()初始化所有的Module

在AbpBootstrapper的Dispose方法中析构IAbpModuleManager,执行其ShutdownModules,关闭所有Module。

 

posted on 2015-11-23 16:57  想回家种地养猪  阅读(2732)  评论(0编辑  收藏  举报