馬士兵Spring學(xué)習(xí)筆記_第1頁(yè)
馬士兵Spring學(xué)習(xí)筆記_第2頁(yè)
馬士兵Spring學(xué)習(xí)筆記_第3頁(yè)
馬士兵Spring學(xué)習(xí)筆記_第4頁(yè)
馬士兵Spring學(xué)習(xí)筆記_第5頁(yè)
已閱讀5頁(yè),還剩12頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

課程內(nèi)容面向接口(抽象)編程的概念與好處2.IOC/DI的概念與好處a) inversionofcontrolb) dependencyinjection3.AOP的概念與好處Spring簡(jiǎn)介Spring應(yīng)用IOC/DI(重要)a) xmlb) annotationSpring應(yīng)用AOP(重要)a) xmlb) annotationStruts2.1.6+Spring2.5.6+Hibernate3.3.2整合(重要)a)opensessionlnviewfilter(記住,解決什么問(wèn)題,怎么解決)SpringJDBC面向接口編程(面向抽象編程).2.3.Spring_0100_AbstractOrientedProgramminga) 不是AOP:AspectOrientedProgramming好處:靈活什么是IOC(DI),有什么好處1.把自己new的東西改為由容器提供初始化具體值裝配好處:靈活裝配Spring簡(jiǎn)介項(xiàng)目名稱:Spring_0200_IOC_Introduction環(huán)境搭建a) 只用IOCspring.jar,jarkata-commons/commons-loggin.jarIOC容器a)實(shí)例化具體beanb)動(dòng)態(tài)裝配AOP支持a)安全檢查管理transactionSpringIOC配置與應(yīng)用FAQ:不給提示:window-preferences-myeclipse-xml-xmlcatalogUserSpecifiedEntries-addLocation:D:\share\0900_Spring\soft\spring-framework-2.5.6\dist\resources\spring-beans-2.5.xsdURI:file:///D:/share/0900_Spring/soft/spring-framework-2.5.6/dist/resources/spring-beans-2.5.xsdKeyType:SchemaLocationKey:/schema/beans/spring-beans-2.5.xsd注入類型Spring_0300_IOC_Injection_Typesetter(重要)構(gòu)造方法(可以忘記)接口注入(可以忘記)idvs.nameSpring_0400_IOC_Id_Namename可以用特殊字符簡(jiǎn)單屬性的注入Spring_0500_IOC_SimplePropertyvpropertyname=...value=....>vbean中的scope屬性Spring_0600_IOC_Bean_Scopesingleton單例proptotype每次創(chuàng)建新的對(duì)象集合注入Spring_0700_IOC_Collections很少用,不重要!參考程序7.自動(dòng)裝配Spring_0800_IOC_AutoWirebyNamec) byTyped)如果所有的bean都用同一種,可以使用beans的屬性:default-autowire8.生命周期a) Spring_0900_IOC_Life_Cycleb) lazy-init(不重要)c) init-methoddestroy-methd不要和prototype—起用(了解)Annotation第一步:a)修改xml文件,參考文檔,在<context:annotation-config/>,此句話會(huì)初始化AutowireAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor ,PersistenceAnnotationBeanPostProcessor ,RequireAnnotationBeanPostProcessor。這四個(gè)處理器是在bean初始化完成后開(kāi)始處理annotation的。@Autowireda) 默認(rèn)按類型bytype,autowired可以與qulifier混合使用。b) 如果想用byName,使用@Qulifier.c) Bean的名字默認(rèn)為qualifier的valued) 如果寫在set上,@qualifier需要寫在參數(shù)上e) 寫在privatefield(第三種注入形式)(不建議,破壞封裝)f) @Required與@override—樣,主要進(jìn)行容錯(cuò)處理。@Resource(重要)a) 加入:j2ee/common-annotations.jarb) 默認(rèn)按名稱,名稱找不到,再按類型查找c) 可以指定特定名稱@Resource(name="u")d) 推薦使用e) 不足:如果沒(méi)有源碼,就無(wú)法運(yùn)用annotation,只能使用xml@Component@Service@Controller@Repository(數(shù)據(jù)倉(cāng)庫(kù)):目前沒(méi)有區(qū)別a) Xml中加入:<context:component-scanbase-package=”com.bjsxt”/>b) 在classpathxmlcontext初始化時(shí)會(huì)讀寫滿了文件,發(fā)現(xiàn)上述內(nèi)容,會(huì)掃描包下的所有包含有component注解的類,并初始化。c) 初始化的名字默認(rèn)為類名首字母小寫d) 可以指定初始化bean的名字,@Component("userdaoimpl”)@Scope:參考文檔@PostConstruct添加到init方法;@PreDestroy添加到destroy方法前。;什么是AOP面向切面編程Aspect-Oriented-Programminga)是對(duì)面向?qū)ο蟮乃季S方式的有力補(bǔ)充Spring_1400_AOP_Introduction

動(dòng)態(tài)代理:在生成代理對(duì)象時(shí),程序會(huì)根據(jù)實(shí)現(xiàn)的接口來(lái)產(chǎn)生代理對(duì)象,并將被代理對(duì)象傳遞給LogInterceptor的target變量,這里生成的$proxy4對(duì)象,該對(duì)象會(huì)實(shí)現(xiàn)相應(yīng)的接口,即接口里有什么方法,生成的代理就有那些方法,這些方法的實(shí)現(xiàn)如上圖所示。AOP:傳統(tǒng)程序編寫就是一條線,AOP就像是在這條線上橫著砍了一刀,加上自己的邏輯,而方法自身不知道。用途:權(quán)限控制,效率檢查,事務(wù)管理,日志控制,異常管理。Spring實(shí)現(xiàn):使用了Jdk的動(dòng)態(tài)代理實(shí)現(xiàn),前提是類實(shí)現(xiàn)了接口,如果類沒(méi)有實(shí)現(xiàn)接口,也能產(chǎn)生動(dòng)態(tài)代理,用繼承直接生成二進(jìn)制碼。好處:可以動(dòng)態(tài)的添加和刪除在切面上的邏輯而不影響原來(lái)的執(zhí)行代碼典型的AOP:FilterStruts2的interceptor概念:了解JoinPoint:切入點(diǎn),那些點(diǎn)上需要加入切面。PointCut:切入點(diǎn)集合Aspect(切面),切面類Advice:@Before,加在切入點(diǎn)的邏輯Target:織入到那個(gè)類的方法Weave:織入SpringAOP配置與應(yīng)用1.兩種方式:a)使用Annotationb)使用xmlAnnotationa)加上對(duì)應(yīng)的xsd文件spring-aop.xsdb)beans.xml<aop:aspectj-autoproxy/>c)<aop:aspectj-autoproxy/>容器啟動(dòng)時(shí)自動(dòng)產(chǎn)生代理,內(nèi)部的實(shí)現(xiàn)采用aspectj,它是一個(gè)專門產(chǎn)生動(dòng)態(tài)代理的框架。此時(shí)就可以解析對(duì)應(yīng)的Annotation了導(dǎo)入類庫(kù):aspectjweaver.jar(織入),aspectjrt.jar建立我們的攔截類用@Aspect注解這個(gè)類建立處理方法用@Before來(lái)注解方法寫明白切入點(diǎn)(execution.)讓spring對(duì)我們的攔截器類進(jìn)行管理@Component<context:component-scan>順帶的查找到aspect。常見(jiàn)的Annotation:@Pointcut@Before@AfterReturning@AfterThrowing@After@Around織入點(diǎn)語(yǔ)法void!void參考文檔(*..)Pointcut注意:一個(gè)類沒(méi)有實(shí)現(xiàn)接口,會(huì)使用直接操作二進(jìn)制碼也就是cjlibrary,所以需要導(dǎo)入cjlib.jar包。6.xml配置AOP(annotation可以不掌握,但是xml必須掌握)a) 把interceptor對(duì)象初始化b) <aop:config<aop:aspect…..<aop:pointcut<aop:beforeSpring整合HibernateSpring指定datasource參考文檔,找dbcp.BasicDataSourcec3p0dbcpproxool在DAO或者Service中注入dataSource在Spring中可以使用PropertyPlaceHolderConfigure來(lái)讀取Properties文件的內(nèi)容Spring整合Hibernate<bean..AnnotationSessionFactoryBean><propertydataSource<annotatedClasses引入hibernate系列jar包User上加AnnotationUserDAO或者UserServie注入SessionFactoryjar包問(wèn)題一個(gè)一個(gè)解決聲明式的事務(wù)管理事務(wù)加在DAO層還是Service層?annotation加入annotation.xsd加入txManagerbean<tx:annotation-driven在需要事務(wù)的方法上加:@Transactional需要注意,使用SessionFactory.getCurrentSession不要使用OpenSession@Transactional詳解什么時(shí)候rollback運(yùn)行期異常,非運(yùn)行期異常不會(huì)觸發(fā)rollback必須uncheck(沒(méi)有catch)不管什么異常,只要你catch了,spring就會(huì)放棄管理事務(wù)傳播特性:propagation_requiredread_onlyd) xml(推薦,可以同時(shí)配置好多方法)<beantxmanager<aop:config1.<aop:pointcut<aop:advisorpointcut-refadvice-refvtx:advice:idtransaction-manager=<?xmlversion="2.0"encoding="UTF-8"?><beansxmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:context="/schema/context"xmlns:aop="/schema/aop"xmlns:tx="/schema/tx"xsi:schemaLocation="/schema/beanshttp://www.springframework.Org/schema/beans/spring-beans-2.5.xsd/schema/contexthttp://www.springframework.Org/schema/context/spring-context-2.5.xsd/schema/aophttp://www.springframework.Org/schema/aop/spring-aop-2.5.xsd/schema/txhttp://www.springframework.Org/schema/tx/spring-tx-2.5.xsd><context:annotation-config/><context:component-scanbase-package="com.bjsxt"/><!--<beanid="dataSource"class="mons.dbcp.BasicDataSource"destroy-method="close"><propertyname="driverClassName"value="com.mysql.jdbc.Driver"/><propertyname="url"value="jdbc:mysql://localhost:3306/spring"/><propertyname="username"value="root"/><propertyname="password"value="bjsxt"/></bean>--><beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><propertyname="locations"〉<value>classpath:perties</value></property></bean><beanid="dataSource"destroy-method="close"class="mons.dbcp.BasicDataSource"><propertyname="driverCLassName"value="${jdbc.driverCLassName}"/><propertyname="urL"value="${jdbc.urL}"/><propertyname="username"value="${jdbc.username}"/><propertyname="password"value="${jdbc.password}"/></bean><beanid="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><propertyname="dataSource"ref="dataSource"/><!--<propertyname="annotatedClasses"><list><value>com.bjsxt.model.User</value><value>com.bjsxt.model.Log</value></list></property>--><propertyname="packages帀Scan"><list><value>com.bjsxt.model</value></list></property><propertyname="hibernateProperties"><props><propkey="hibernate.diaLect">org.hibernate.dialect.MySQLDialect</prop><propkey="hibernate.show_sqL">true</prop></props></property></bean><beanid="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><propertyname="sessionFactory"ref="sessionFactory"/></bean><aop:config><aop:pointcutid="buss訕essSe廣Mce"expression="executionfpudtic*com.bjsxt.service..*.*(..))"/><aop:advisorpointcut-ref="bussinessService"advice-ref="txAdvice"/></aop:config><tx:adviceid="txAdvice"transaction-manager="txManager"><tx:attributes><tx:methodname="getUser"read-only="true"/><tx:methodname="add*"propagation="REQUIRED"/〉</tx:attributes></tx:advice></beans>HibernateTemplataHibernateCallback、HibernateDaoSupport(不重要)介紹設(shè)計(jì)模式:TemplateMethodCallback:回調(diào)/鉤子函數(shù)第一種:(建議)在spring中初始化HibernateTemplate,注入sessionFactoryDAO里注入HibernateTemplatesave寫getHibernateTemplate.save();第二種:從HibernateDaoSupport繼承必須寫在xml文件中,無(wú)法使用Annotation,因?yàn)閟et方法在父類中,而且是final的spring整合hibernate的時(shí)候使用packagesToScan屬性,可以讓spring自動(dòng)掃描對(duì)應(yīng)包下面的實(shí)體類Struts2.1.6+Spring2.5.6+Hibernate3.3.21.需要的jar包列表jar包名稱所在位置說(shuō)明antlr-2.7.6.jarhibernate/lib/required解析HQLaspectjrtspring/lib/aspectjAOP

aspectjweaver..AOPcglib-nodep-2.13.jarspring/lib/cglib代理,二進(jìn)制增強(qiáng)common-annotations.jarspring/lib/j2ee@Resourcecommons-collections-3.1.jarhibernate/lib/required集合框架commons-fileupload-1.2.1.jarstruts/libstrutscommons-io-1.3.2struts/libstrutscommons-logging-1.1.1單獨(dú)下載,刪除1.0.4(struts/lib)strutsspringdom4j-1.6.1.jarhibernate/required解析xmlejb3-persisteneehibernate-annotation/lib@Entityfreemarker-2.3.13struts/libstrutshibernate3.jarhibernatehibernate-annotationshibernate-annotation/hibernate-common-annotationshibernate-annotation/libjavassist-3.9.0.GA.jarhiberante/lib/requiredhibernatejta-1.1.jar..hibernatetransactionjunit4.5mysql-ognl-2.6.11.jarstruts/libslf4j-api-1.5.8.jarhibernate/lib/requiredhibernate-logslf4j-nop-1.5.8.jarhibernate/lib/requiredspring.jarspring/diststruts2-core-2.1.6.jarstruts/libxwork-2.1.2.jarstruts/libstruts2commons-dbcpspring/lib/jarkata-commonscommons-pool.jar..struts2-spring-plugin-2.1.6.jarstruts/libBestPractice:a)將這些所有的jar包保存到一個(gè)位置,使用的時(shí)候直接copy3.步驟a)加入jar包b)首先整合Spring+Hibernatei.建立對(duì)應(yīng)的package1.dao/dao.impl/model/service/service.impl/testii.建立對(duì)應(yīng)的接口與類框架1.S2SH_01iii.建立spring的配置文件(建議自己保留一份經(jīng)常使用的配置文件,以后用到的時(shí)候直接copy改)iv.建立數(shù)據(jù)庫(kù)v.加入Hibernate注解在實(shí)體類上加相應(yīng)注解@〔門tity@Id等在beans配置文件配置對(duì)應(yīng)的實(shí)體類,使之受管vi.寫daoservice的實(shí)現(xiàn)vii.加入Spring注解在對(duì)應(yīng)Service及DAO實(shí)現(xiàn)中加入@Component,讓spring對(duì)其初始化在Service上加入@Transactional或者使用xml方式(此處建議后者,因?yàn)楦?jiǎn)單)在DAO中注入sessionFactory在Service中注入DAO寫DAO與Service的實(shí)現(xiàn)viii. 寫測(cè)試整合Struts2結(jié)合點(diǎn):Struts2的Action由Spring產(chǎn)生步驟:修改web.xml加入struts的filter再加入spring的listener,這樣的話,webapp一旦啟動(dòng),spring容器就初始化了規(guī)劃struts的action和jsp展現(xiàn)加入struts.xmla)修改配置,由spring替代struts產(chǎn)生Action對(duì)象修改action配置把類名改為bean對(duì)象的名稱,這個(gè)時(shí)候就可以使用首字母小寫了@Scope(''prototype")不要忘記struts的讀常量:pertiesweb.xml中文問(wèn)題:Struts2.1.8已經(jīng)修正,只需要改i18n.encoding=gbk使用spring的characterencoding需要嚴(yán)格注意filter的順序需要加到Struts2的filter前面LazyInitializationExceptionOpenSessionInViewFilter需要嚴(yán)格順序問(wèn)題需要加到struts2的filter前面<?xmlversion="2.0"encoding="UTF-8"?><web-appversion="2.5"xmlns="/xml/ns/javaee"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/xml/ns/javaee/xml/ns/javaee/web-app_2_5.xsd"><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class><!--default:/WEB-INF/applicationContext.xml--></listener><context-param><param-name>contextConfigLocation</param-name><!--<param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>--><param-value>classpath:beans.xml</param-value></context-param><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app><?xmlversion="1.0"encoding="U7F-8"?><beansxmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:context="/schema/context"xmlns:aop="/schema/aop"xmlns:tx="/schema/tx"xsi:schemaLocation="/schema/beanshttp://www.springframework.Org/schema/beans/spring-beans-2.5.xsd/schema/contexthttp://www.springframework.Org/schema/context/spring-context-2.5.xsd/schema/aophttp://www.springframework.Org/schema/aop/spring-aop-2.5.xsd/schema/txhttp://www.springframework.Org/schema/tx/spring-tx-2.5.xsd><context:annotation-config/><context:component-scanbase-package="com.bjsxt"/><!--<beanid="dataSource"class="mons.dbcp.BasicDataSource"destroy-method="close"><propertyname="driverClassName"value="com.mysql.jdbc.Driver"/><propertyname="url"value="jdbc:mysql://localhost:3306/spring"/><propertyname="username"value="root"/><propertyname="password"value="bjsxt"/></bean>--><beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><propertyname="locations"〉<value>classpath:perties</value></property></bean><beanid="dataSource"destroy-method="close"class="mons.dbcp.BasicDataSource"><propertyname="driverClassName"value="${jdbc.driverClassName}"/><propertyname="url"value="${jdbc.url}"/><propertyname="username"value="${jdbc.username}"/><propertyname="password"value="${jdbc.pas

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論