Spring学习讲义.docx

上传人:b****7 文档编号:10167953 上传时间:2023-02-09 格式:DOCX 页数:10 大小:48.61KB
下载 相关 举报
Spring学习讲义.docx_第1页
第1页 / 共10页
Spring学习讲义.docx_第2页
第2页 / 共10页
Spring学习讲义.docx_第3页
第3页 / 共10页
Spring学习讲义.docx_第4页
第4页 / 共10页
Spring学习讲义.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

Spring学习讲义.docx

《Spring学习讲义.docx》由会员分享,可在线阅读,更多相关《Spring学习讲义.docx(10页珍藏版)》请在冰豆网上搜索。

Spring学习讲义.docx

Spring学习讲义

Spring几个重要概念(0200)

1.DIDependencyInjection依赖注入

2.IOC(DI)InversionofControl控制反转

3.AOPAspect-OrientedProgramming面向切面编程

4.SSH框架

什么是IOC(DI),有什么好处

1.把自己new的东西改为由容器提供

a)初始化具体值

b)装配

2.好处:

灵活装配

SpringIOC配置与应用

1.环境搭建:

A)引入jar包:

spring-core.jar,sping-beans.jar,spring-context.jar

jarkata-commons/commons-loggin.jar,

B)引入XML文件的元文件(解决没有提示的问题):

a)window–preferences–myeclipse–xml–xmlcatalog

b)UserSpecifiedEntries–add

i.Location:

D:

\share\0900_Spring\soft\spring-framework-2.5.6\dist\resources\spring-beans-2.5.xsd

ii.URI:

file:

///D:

/share/0900_Spring/soft/spring-framework-2.5.6/dist/resources/spring-beans-2.5.xsd

iii.KeyType:

SchemaLocation

iv.Key:

http:

//www.springframework.org/schema/beans/spring-beans-2.5.xsd

2.BeanFactory和ApplicationContext

ApplicationContext实现BeanFactory接口

1ClassPathXmlApplicationContext从classpath路径中读取xml文件

3.Bean的实例化(xml):

1调用无参构造函数;(0300)“常用”

2采用静态工厂方法。

(0400)“不用了解具体实现方法”

4.Bean的Scope

1Singlelon(默认)

2Prototype

3Request

4Session

5.Bean的生命周期

一个BEAN从建立到消亡,会经历一系列的生命周期。

1Bean定义文件中定义init-method(局部)

2Bean定义文件中定义destroy-method(局部)

3配置文件定义:

default-init-method和default-destroy-method(全局)

注意init-methoddestroy-method不要和prototype一起用!

6.Bean定义的继承

需要在抽象Bean的定义中加入:

abstract=”true”

在实现Bean的定义中加入:

parent=“ParentBeanName”。

7.Bean的依赖设置

两种基本注入方式:

1SetterInjection(重要)

2ConstructionInjection(不重要用的很少)

8.自动绑定

四种方法:

1在Bean的定义中使用标签定义基本类型值;

2是用标签指定参考至其他Bean实例;

3使用标签指定”class”属性,指定依赖对象;

4隐式的自动绑定

绑定方式:

byName(名称)byType(类型)将某个实例绑定在其他Bean属性上。

9.集合注入

不重要,很少用,看例子!

什么是AOP

1.面向切面编程Aspect-OrientedProgramming

A)是对面向对象编程的有力补充。

(顺,横)

2.当我们需要在业务逻辑前后加入一些与业务逻辑无关的系统逻辑(如:

日志,权限检查,安全设置,运行时间等)内容时,我们把这些逻辑独立出来变成一个对象,成为Aspect。

3.好处:

可以动态的添加和删除在切面上的逻辑而不影响原来的执行代码。

4.AOP实现方法:

代理(Proxy)

Spring会自动生成代理对象,现简述代理机制

SpringAOP的配置与应用

1.代理机制:

A)静态代理

1代理对象和被代理对象需要实现同一个接口

2代理对象可以实现日志相关服务

3被代理对象保留业务逻辑职责。

4缺点:

一个代理接口对应一种服务,若代理方法很多,无法胜任。

B)动态代理

1使用Handler服务于各个对象,Handler实现InvocationHandler接口。

2使用Handler的bind()方法,绑定被代理对象。

2.AOP的相关概念

a)JoinPoint切点

b)PointCut切点的集合

c)Aspect切面

d)Advice切面逻辑

e)Target被代理对象

f)Weave织入

3.实现AOP的两种方式:

A)使用annotation

B)使用xml

4.Annotation的AOP配置

a)加上对应的xsd文件spring-aop.xsd和Jar包

b)beans.xml

aspectj-autoproxy/>

c)此时就可以解析对应的Annotation了

d)建立我们的拦截类

e)用@Aspect注解这个类

f)建立处理方法

g)用@Before来注解方法

h)写明白切入点(execution…….)

i)让spring对我们的拦截器类进行管理@Component,初始化这个切面

5.常见的Annotation:

(用@做注解)

a)@Pointcut切点的集合,使写法简介一点点

b)@Before在方法前织入

c)@AfterReturning==(finally)在抛出异常后依旧织入

d)@AfterThrowing在抛出异常前织入

e)@Around在方法前后环绕织入

6.xml配置AOP

①把interceptor对象初始化

config

aspect…..

pointcut

before

Struts+Spring+Hibernate

1.架构的历史

A)两层架构

B)三层架构

Spring整合Hibernate

1.架构思路(DI)

A)UserDAO注入SessionFactory

B)DataSource的注入

C)SessionFactory的注入

Spring整合Struts

1.架构思路

A)建立webproject,加入相关的jar包

B)struts对象继承Action类,让Spring来管理Action对象,让Action对象也成为Spring管理的Bean,即使用Spring产生Action对象。

(HelloAction.java)

C)UserChecker模拟业务层权限检查对象,可以使用Spring做依赖注入(UserChecker.java,beans-config.xml)

D)对Struts-config.xml使用Plug-in标签,指定Spring的Bean名称和位置。

注意:

Struts-config.xml中Action名称与beans-config.xml中Bean的名称要对应,Struts生成的代理对象(DelegatingActionProxy)是通过名称进行匹配和完成消息传递的。

Struts2.1.6+Spring2.5.6+Hibernate3.3.2

1.需要的jar包列表

jar包名称

所在位置

说明

antlr-2.7.6.jar

hibernate/lib/required

解析HQL

aspectjrt

spring/lib/aspectj

AOP

aspectjweaver

..

AOP

cglib-nodep-2.1_3.jar

spring/lib/cglib

代理,二进制增强

common-annotations.jar

spring/lib/j2ee

@Resource

commons-collections-3.1.jar

hibernate/lib/required

集合框架

commons-fileupload-1.2.1.jar

struts/lib

struts

commons-io-1.3.2

struts/lib

struts

commons-logging-1.1.1

单独下载,删除1.0.4(struts/lib)

struts

spring

dom4j-1.6.1.jar

hibernate/required

解析xml

ejb3-persistence

hibernate-annotation/lib

@Entity

freemarker-2.3.13

struts/lib

struts

hibernate3.jar

hibernate

hibernate-annotations

hibernate-annotation/

hibernate-common-annotations

hibernate-annotation/lib

javassist-3.9.0.GA.jar

hiberante/lib/required

hibernate

jta-1.1.jar

..

hibernatetransaction

junit4.5

mysql-

ognl-2.6.11.jar

struts/lib

slf4j-api-1.5.8.jar

hibernate/lib/required

hibernate-log

slf4j-nop-1.5.8.jar

hibernate/lib/required

spring.jar

spring/dist

struts2-core-2.1.6.jar

struts/lib

xwork-2.1.2.jar

struts/lib

struts2

commons-dbcp

spring/lib/jarkata-commons

commons-pool.jar

..

struts2-spring-plugin-2.1.6.jar

struts/lib

2.BestPractice:

a)将这些所有的jar包保存到一个位置,使用的时候直接copy

3.步骤

a)加入jar包

b)首先整合Spring+Hibernate

i.建立对应的package

1.dao/dao.impl/model/service/service.impl/test

ii.建立对应的接口与类框架

1.S2SH_01

iii.建立spring的配置文件(建议自己保留一份经常使用的配置文件,以后用到的时候直接copy改)

iv.建立数据库

v.加入Hibernate注解

1.在实体类上加相应注解@Entity@Id等

2.在beans配置文件配置对应的实体类,使之受管

vi.写daoservice的实现

vii.加入Spring注解

1.在对应Service及DAO实现中加入@Component,让spring对其初始化

2.在Service上加入@Transactional或者使用xml方式(此处建议后者,因为更简单)

3.在DAO中注入sessionFactory

4.在Service中注入DAO

5.写DAO与Service的实现

viii.写测试

c)整合Struts2

i.结合点:

Struts2的Action由Spring产生

ii.步骤:

1.修改web.xml加入struts的filter

2.再加入spring的listener,这样的话,webapp一旦启动,spring容器就初始化了

3.规划struts的action和jsp展现

4.加入struts.xml

a)修改配置,由spring替代struts产生Action对象

5.修改action配置

a)把类名改为bean对象的名称,这个时候就可以使用首字母小写了

b)@Scope(“prototype”)不要忘记

iii.struts的读常量:

1.struts-default.xml

2.struts-plugin.xml

3.struts.xml

4.struts.properties

5.web.xml

iv.中文问题:

1.Struts2.1.8已经修正,只需要改i18n.encoding=gbk

2.使用spring的characterencoding

3.需要严格注意filter的顺序

4.需要加到Struts2的filter前面

v.LazyInitializationException

1.OpenSessionInViewFilter

2.需要严格顺序问题

3.需要加到struts2的filter前面

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1