在ABP框架的AbpBootstrapper主要用於框架的基本配置的注冊和初始化,在Web應用啟動階段實例化一個AbpBootstrapper並調用Initialize方法初始化,該類主要包含兩個公有屬性分別是IIocManager和IAbpModuleManager
- 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的實現類進行注冊,默認是需要的。
- 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。
