一直不能坐下來好好學(xué)習(xí)一下, 最近研究了spring framework, 一點(diǎn)感受:
1. IOC
傳統(tǒng)方法:如果動態(tài)設(shè)置一個對象屬性,可以借助Java的Reflection機(jī)制完成,invoke()激活返回調(diào)用
Class cls = Class.forName("com.eking.User");
Method mtd = cls.getMethod("setName",new Class[]{String.class});
Object obj = (Object)cls.newInstance();
mtd.invoke(obj,new Object[]{"Erica"});
return obj;
在spring中, 面向接口編程,動態(tài)代理機(jī)制:BeanWrapper, BeanFactory,ApplicationContext提供了管理javabean的包裝器,所有的一切都在容器中配置,dependency injection.
也可以不提供接口,CGLib與Dynamic Proxy的代理機(jī)制基本類似,只是其動態(tài)生成的代理對象并非某接口的實(shí)現(xiàn),而是針對目標(biāo)類擴(kuò)展的子類。換句話說,Dynamic Proxy返回的動態(tài)代理類,是目標(biāo)類所實(shí)現(xiàn)的接口的另一個實(shí)版本,它實(shí)現(xiàn)了對目標(biāo)類的代理(如同UserDAOProxy與UserDAOImp的關(guān)系)。而CGLib返回的動態(tài)代理類,則是目標(biāo)代理類的一個子類(代理類擴(kuò)展了UserDAOImp類)。
2. AOP
各種通知類型有MethodInterceptor (來自AOP聯(lián)盟的攔截器API)和定義在org.springframework.aop包中的 通知接口。所有通知必須實(shí)現(xiàn)org.aopalliance.aop.Advice標(biāo)簽接口。 取出就可使用的通知有 MethodInterceptor、 ThrowsAdvice、 BeforeAdvice和 AfterReturningAdvice。
也可以自己寫個攔截器,在實(shí)現(xiàn)自己的方法之前出發(fā)某個動作,執(zhí)行一些處理。
3. WebApplicationContext
1) web.xml 中通過聲明監(jiān)聽器接口 或servlet類加載
通過:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener-->
或:
<servlet>
<servlet-name>SpringContextServlet</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Web 容器會自動加載 /WEB-INF/applicationContext.xml 初始化 ApplicationContex t實(shí)例;
也可以通過
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-*.xml</param-value>
</context-param>
使 Web 容器加載指定名稱路徑的 Spring 配置文件。
我個人認(rèn)為Listerner要比Servlet更好一些,因?yàn)?/span>Listerner監(jiān)聽?wèi)?yīng)用的啟動和結(jié)束,而Servlet得啟動要稍微延遲一些,如果在這時要做一些業(yè)務(wù)的操作,啟動的前后順序是有影響的。 2) struts-config.xml 中通過插件加載 在struts-config.xml中Action的配置變成類似下面的樣子 別急,我們還是來看一下ContextLoaderPlugIn的源碼(源碼不再貼出),我們可以發(fā)現(xiàn),原來ContextLoaderPlugIn仍然是把WebApplicationContext放在ServletContext中,只是這個KEY不太一樣了,這個KEY值為ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX+ModuleConfig.getPrefix()(具體請查看源代碼),這下好了,我們知道了WebApplicationContext放在哪里,只要我們在Web應(yīng)用中能夠取到ServletContext也就能取到WebApplicationContext了 4.Struts+spring+hibernate spring live中myusers,一個3層架構(gòu)的web 程序,通過一個Action 來調(diào)用業(yè)務(wù)代理,再通過它來回調(diào) DAO類。下面的流程圖表示了MyUsers是如何工作的。數(shù)字表明了流程的先后順序,從web層(UserAction)到中間層(UserManager),再到數(shù)據(jù)層(UserDAO),然后返回。 Spring是AOP, UserManager和UserDAO都是接口. 1) web層(UserAction) :調(diào)用中間層的接口方法,將UserManager作為屬性注入 2) 中間層(UserManager):將UserDAO作為屬性注入,其實(shí)現(xiàn)主要是調(diào)用數(shù)據(jù)層接口的一些方法; 它處于事務(wù)控制中 3) 數(shù)據(jù)層(UserDAO):實(shí)現(xiàn)類繼承HibernateDaoSupport類,在該類中可以調(diào)用getHibernateTemplate()的一些方法執(zhí)行具體的數(shù)據(jù)操作。 spring配置部分文件: <bean id="userDAO" class="com.eking.entity.UserDAOImp"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <bean id="userManagerTarget" class="com.eking.service.UserManagerImp"> <property name="userDAO"><ref local="userDAO"/></property> </bean> <bean id="userManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref local="transactionManager" /> </property> <property name="target"> <ref local="userManagerTarget" /> </property> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="get*">PROPAGATION_REQUIRED</prop> <prop key="is*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <bean name="/login" class="com.eking.struts.action.LoginAction" singleton="false"> <property name="userManager"> <ref local="userManager" /> </property> </bean> spring live中myusers,一個3層架構(gòu)的web 程序,通過一個Action 來調(diào)用業(yè)務(wù)代理,再通過它來回調(diào) DAO類。下面的流程圖表示了MyUsers是如何工作的。數(shù)字表明了流程的先后順序,從web層(UserAction)到中間層(UserManager),再到數(shù)據(jù)層(UserDAO),然后返回。 Spring是AOP, UserManager和UserDAO都是接口. 1) web層(UserAction) :調(diào)用中間層的接口方法,將UserManager作為屬性注入 2) 中間層(UserManager):將UserDAO作為屬性注入,其實(shí)現(xiàn)主要是調(diào)用數(shù)據(jù)層接口的一些方法; 它處于事務(wù)控制中 3) 數(shù)據(jù)層(UserDAO):實(shí)現(xiàn)類繼承HibernateDaoSupport類,在該類中可以調(diào)用getHibernateTemplate()的一些方法執(zhí)行具體的數(shù)據(jù)操作。 spring配置部分文件: <bean id="userDAO" class="com.eking.entity.UserDAOImp"> <property name="sessionFactory"> <ref local="sessionFactory" /> </property> </bean> <bean id="userManagerTarget" class="com.eking.service.UserManagerImp"> <property name="userDAO"><ref local="userDAO"/></property> </bean> <bean id="userManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref local="transactionManager" /> </property> <property name="target"> <ref local="userManagerTarget" /> </property> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="get*">PROPAGATION_REQUIRED</prop> <prop key="is*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <bean name="/login" class="com.eking.struts.action.LoginAction" singleton="false"> <property name="userManager"> <ref local="userManager" /> </property> </bean>
那么在ContextLoaderListener和ContextLoaderServlet中到底做了什么呢?
以ContextLoaderListener為例,我們可以看到
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
protected ContextLoader createContextLoader() {
return new ContextLoader();
}
ContextLoader是一個工具類,用來初始化WebApplicationContext,其主要方法就是initWebApplicationContext,我們繼續(xù)追蹤initWebApplicationContext這個方法(具體代碼我不貼出,大家可以看Spring中的源碼),我們發(fā)現(xiàn),原來ContextLoader是把WebApplicationContext(XmlWebApplicationContext是默認(rèn)實(shí)現(xiàn)類)放在了ServletContext中,ServletContext也是一個“容器”,也是一個類似Map的結(jié)構(gòu),而WebApplicationContext在ServletContext中的KEY就是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,我們?nèi)绻褂?/span>WebApplicationContext則需要從ServletContext取出,Spring提供了一WebApplicationContextUtils類,可以方便的取出WebApplicationContext,只要把ServletContext傳入就可以了。
通過
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml,
/WEB-INF/action-servlet.xml"/>
</plug-in>
來加載 Spring 配置文件。
<action attribute="aForm" name="aForm" path="/aAction" scope="request" type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="forward" path="forward.jsp" />
</action>