一直不能坐下來好好學習一下, 最近研究了sPRing framework, 一點感受:
1. IOC
傳統方法:假如動態設置一個對象屬性,可以借助java的Reflection機制完成,invoke()激活返回調用
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中, 面向接口編程,動態代理機制:BeanWrapper, BeanFactory,applicationContext提供了治理javabean的包裝器,所有的一切都在容器中配置,dependency injection.
也可以不提供接口,CGLib與Dynamic Proxy的代理機制基本類似,只是其動態生成的代理對象并非某接口的實現,而是針對目標類擴展的子類。換句話說,Dynamic Proxy返回的動態代理類,是目標類所實現的接口的另一個實版本,它實現了對目標類的代理(如同UserDAOProxy與UserDAOImp的關系)。而CGLib返回的動態代理類,則是目標代理類的一個子類(代理類擴展了UserDAOImp類)。
2. AOP
各種通知類型有MethodInterceptor (來自AOP聯盟的攔截器API)和定義在org.springframework.aop包中的 通知接口。所有通知必須實現org.aopalliance.aop.Advice標簽接口。 取出就可使用的通知有 MethodInterceptor、 ThrowsAdvice、 BeforeAdvice和 AfterReturningAdvice。
也可以自己寫個攔截器,在實現自己的方法之前出發某個動作,執行一些處理。
3. WebApplicationContext
1) web.xml 中通過聲明監聽器接口 或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實例;
也可以通過
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-*.xml</param-value>
</context-param>
使 Web 容器加載指定名稱路徑的 Spring 配置文件。
我個人認為Listerner要比Servlet更好一些,因為Listerner監聽應用的啟動和結束,而Servlet得啟動要稍微延遲一些,假如在這時要做一些業務的操作,啟動的前后順序是有影響的。
那么在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,我們繼續追蹤initWebApplicationContext這個方法(具體代碼我不貼出,大家可以看Spring中的源碼),我們發現,原來ContextLoader是把WebApplicationContext(XmlWebApplicationContext是默認實現類)放在了ServletContext中,ServletContext也是一個“容器”,也是一個類似Map的結構,而WebApplicationContext在ServletContext中的KEY就是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,我們假如要使用WebApplicationContext則需要從ServletContext取出,Spring提供了一WebApplicationContextUtils類,可以方便的取出WebApplicationContext,只要把ServletContext傳入就可以了。
2) struts-config.xml 中通過插件加載
通過
<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 配置文件。
在struts-config.xml中Action的配置變成類似下面的樣子
<action attribute="aForm" name="aForm" path="/aAction" scope="request" type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="forward" path="forward.jsp" />
</action>
別急,我們還是來看一下ContextLoaderPlugIn的源碼(源碼不再貼出),我們可以發現,原來ContextLoaderPlugIn仍然是把WebApplicationContext放在ServletContext中,只是這個KEY不太一樣了,這個KEY值為ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX+ModuleConfig.getPrefix()(具體請查看源代碼),這下好了,我們知道了WebApplicationContext放在哪里,只要我們在Web應用中能夠取到ServletContext也就能取到WebApplicationContext了
4.Struts+spring+hibernate
spring live中myusers,一個3層架構的web 程序,通過一個Action 來調用業務代理,再通過它往返調 DAO類。下面的流程圖表示了MyUsers是如何工作的。數字表明了流程的先后順序,從web層(UserAction)到中間層(UserManager),再到數據層(UserDAO),然后返回。
Spring是AOP, UserManager和UserDAO都是接口.
1) web層(UserAction) :調用中間層的接口方法,將UserManager作為屬性注入
2) 中間層(UserManager):將UserDAO作為屬性注入,其實現主要是調用數據層接口的一些方法; 它處于事務控制中
3) 數據層(UserDAO):實現類繼續HibernateDaoSupport類,在該類中可以調用getHibernateTemplate()的一些方法執行具體的數據操作。
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層架構的web 程序,通過一個Action 來調用業務代理,再通過它往返調 DAO類。下面的流程圖表示了MyUsers是如何工作的。數字表明了流程的先后順序,從web層(UserAction)到中間層(UserManager),再到數據層(UserDAO),然后返回。
Spring是AOP, UserManager和UserDAO都是接口.
1) web層(UserAction) :調用中間層的接口方法,將UserManager作為屬性注入
2) 中間層(UserManager):將UserDAO作為屬性注入,其實現主要是調用數據層接口的一些方法; 它處于事務控制中
3) 數據層(UserDAO):實現類繼續HibernateDaoSupport類,在該類中可以調用getHibernateTemplate()的一些方法執行具體的數據操作。
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">
 
新聞熱點
疑難解答