標籤:
架構簡介:
Spring是一個開源架構,Spring是於2003 年興起的一個輕量級的Java 開發架構,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中闡述的部分理念和原型衍生而來。它是為瞭解決公司專屬應用程式開發的複雜性而建立的。架構的主要優勢之一就是其分層架構,分層架構允許使用者選擇使用哪一個組件,同時為 J2EE 應用程式開發提供整合的架構。Spring使用基本的JavaBean來完成以前只可能由EJB完成的事情。然而,Spring的用途不僅限於伺服器端的開發。從簡單性、可測試性和松耦合的角度而言,任何Java應用都可以從Spring中受益。Spring的核心是控制反轉(IoC)和面向切面(AOP)。簡單來說,Spring是一個分層的JavaSE/EEfull-stack(一站式) 輕量級開源架構。
需要的lib包:
下載spring3.2的所有包,還要包括struts2-spring-plugin-2.3.24.jar。
還有3個可選配置包,如果有用到AOP切面的時候需要引入aspectjweaver.jar,cglib-nodep-2.1_3.jar,com.springsource.org.aopalliance-1.0.0.jar
配置過程:
1.首先要在src下面建立Spring的設定檔.
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd "> <bean id="manageServlet" class="com.babybus.sdteam.bo.ManageServlet"> </bean> <bean id="aopClass" class="com.babybus.sdteam.bo.AopClass"> </bean> <!-- loginaction --> <bean id="LoginAction" class="com.babybus.sdteam.action.LoginAction"> <property name="manageServlet" ref="manageServlet"></property> </bean> <!-- 切面配置 --> <aop:config proxy-target-class="true"> <!-- 切面對應的類 --> <aop:aspect id="myAop" ref="aopClass"> <!-- 切點配置 --> <aop:pointcut id="ap" expression="execution(* com.babybus.sdteam.bo.*.*(..))"/> <aop:before method="methodBefore" pointcut-ref="ap"/> <aop:after method="methodAfter" pointcut-ref="ap"/> </aop:aspect> </aop:config> </beans>
2.接著要在web.xml進行spring監聽配置
<!--spring設定檔位置--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </context-param><!--監聽器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
3.注意事項
當報錯的時候,請確認<aop:config proxy-target-class="true">和本文開頭說的3個可選包的匯入,匯入後對項目進行clean一遍再執行,然後項目就可以啟動成功了。
到這邊,我們已經講了JAVAWEB的所有後端技術基礎了,可以靈活的應對後端的工作了。
結語
本站文章為 寶寶巴士 SD.Team 原創,轉載務必在明顯處註明:(作者官方網站: 寶寶巴士 )
轉載自【寶寶巴士SuperDo團隊】 原文連結: http://www.cnblogs.com/superdo/p/4641670.html
[JavaWeb基礎] 008.Spring初步配置