`
确实比较男
  • 浏览: 113140 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Guzz源码分析(一) guzz容器初始化过程

    博客分类:
  • Guzz
阅读更多

1. Guzz是什么?

百度Guzz http://baike.baidu.com/link?url=2ILIi8BXdhEUrVmVkYIDoYbehSN-jZ4F83iyJtNhl40zECi8_T7n_Br9acqJhKVhIdoxiyWh9sw2feqGJvq8la

2. 感觉Guzz的资料还是太少,但是还是有一篇很完整的帮助文档:

http://code.google.com/p/guzz/wiki/TutorialGuzzXml?wl=zh-Hans

3. 自己学习Guzz的时间也不长,由于资料比较少,所以就只有直接看源码,在这里记录下过程,供以后查看。

4. Guzz容器初始化的流程图:

5. 接着对应着流程图来查看源码:

5.1 首先servlet容器在初始化的时候会读取到我们在web.xml中的配置:

 

<listener>
        <listener-class>
            org.guzz.web.context.spring.GuzzWithSpringContextLoaderListener
        </listener-class>
</listener>    
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext.xml</param-value>
</context-param>

所以直接就会执行到GuzzWithSpringContextLoaderListener。

5.2 在GuzzWithSpringContextLoaderListener中,首先去初始化了spring的容器,由于我们在applicationContext.xml中配置了guzzContext:

 

<!--GUZZ声明-->
<bean id="guzzContext" class="org.guzz.web.context.spring.GuzzContextBeanFactory" factory-method="createGuzzContext">
	<constructor-arg>
		<value>classpath:guzz.xml</value>
	</constructor-arg>
</bean>

在spring初始化的时候肯定是会创建guzzContext对象的,所以进入到了GuzzContextBeanFactory,只有一个方法把guzz.xml又传入到Configuration,然后调用newGuzzContext()

 

public static GuzzContext createGuzzContext(Resource springResource) throws Exception{
		File file = springResource.getFile() ;
		if(file == null){
			throw new IOException("file is null. spring resource:" + springResource) ;
		}
		return new Configuration(file.getAbsolutePath()).newGuzzContext() ;
	}

在newGuzzContext中创建了guzz中很重要的一个类对象GuzzContextImpl,然后调用了initFromMainConfig,传入了一个Resource(其实也就是guzz.xml)

 

public GuzzContext newGuzzContext() throws Exception{
		GuzzContextImpl gf =  new GuzzContextImpl() ;
		gf.initFromMainConfig(this.config) ;
		if(autoCloseResource){
			CloseUtil.close(config) ;
		}
		return gf ;
	}

5.3 在initFromMainConfig就是主要的初始化过程,主要是解析guzz.xml,初始化数据库数据类型,连接ConfigServer读取配置,初始化Service,初始化事务管理。内容很多,会在后面文章分析。
5.4 在完成了GuzzContextImpl后然后就会回到GuzzWithSpringContextLoaderListener,然后把spring容器通过SpringExtendedBeanFactory这个对象set给GuzzContextImpl,接着就是在创建ContextLoader对象,通过initGuzzContext方法把GuzzContextImpl对象放入到服务器的ServletContext中。

 

ApplicationContext applicationContext = org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext() ;
			GuzzContextImpl gc = (GuzzContextImpl) applicationContext.getBean("guzzContext") ;
			
			if(gc == null){
				throw new GuzzException("spring bean [guzzContext] not found.") ;
			}
			
			//必须在spring初始化完毕以后才能调用。
			SpringExtendedBeanFactory ebf = new SpringExtendedBeanFactory(applicationContext) ;
			gc.setExtendedBeanFactory(ebf) ;
			
			//启动guzz
			if(context == null){
				context = new ContextLoader() ;
				try {
					context.initGuzzContext(sc, gc) ;
				} catch (Exception e) {
					log.error("can't start guzz.", e) ;
					context = null ;
					onLoadError(e) ;
				}
			}

5.5 到此guzz初始化过程完成。这也只是一个大体的过程,很多的细节实现都是在GuzzContextImpl的initFromMainConfig里面

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics