读书人

Spring开闭准则的表现-BeanPostProces

发布时间: 2012-07-15 20:20:06 作者: rapoo

Spring开闭原则的表现-BeanPostProcessor扩展点-2

?


?从图中我们可以看出一共五个接口,共十个回调方法,即十个扩展点,但我们之前的文章只分析了其中八个,另外两个稍候也会解析一下是干什么的。

?

===================================================================

===================================================================

五、五个接口十个扩展点解析

?部分内置的BeanPostProcessor



1、如果只对ServiceBean,DaoBean 放在spring-root.xml 即可

2、如果只对ControllerBean,放在spring-mvc.xml 即可

3、如果对ControllerBean、ServiceBean,DaoBean都起作用,两边都得放(因为是两个不同的上下文)

protected WebApplicationContext initWebApplicationContext() {        //ROOT上下文(ContextLoaderListener加载的)WebApplicationContext rootContext =WebApplicationContextUtils.getWebApplicationContext(getServletContext());WebApplicationContext wac = null;if (this.webApplicationContext != null) {// 1、在创建该Servlet注入的上下文wac = this.webApplicationContext;if (wac instanceof ConfigurableWebApplicationContext) {ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;if (!cwac.isActive()) {if (cwac.getParent() == null) {cwac.setParent(rootContext);}configureAndRefreshWebApplicationContext(cwac);}}}if (wac == null) {             //2、查找已经绑定的上下文wac = findWebApplicationContext();}if (wac == null) {             //3、如果没有找到相应的上下文,并指定父亲为ContextLoaderListenerwac = createWebApplicationContext(rootContext);}if (!this.refreshEventReceived) {             //4、刷新上下文(执行一些初始化)onRefresh(wac);}if (this.publishContext) {// Publish the context as a servlet context attribute.String attrName = getServletContextAttributeName();getServletContext().setAttribute(attrName, wac);//省略部分代码}return wac;}

如上代码的第三步创建了SpringMVC的上下文 并只对ContextLoaderListener为根

读书人网 >操作系统

热点推荐