Day1大纲
开发环境:
jar:spring.jar,jakarta-commons\*.jar
第一个程序:
IOC:
控制反转?
对象必须协作才能完成某种业务
以前:对象自己寻找协作对象
现在:协作对象由外部实体提供。
IOC又叫依赖注入
装配:
容器根据配置文件将协作对象实例化,建立这些对象之间的关系。
容器:(PPT39)
org.springframework.beans.factory.BeanFactory
Bean工厂
提供最基本的Bean的生命周期管理和装配。
直到调用getBean方法时,才真正实例化Bean。
getBean(Bean'sid)
org.springframework.beans.factory.xml.XmlBeanFactory
它可以依据Resource找到相应的配置文件
Resourcers = new ClassPathResource(配置文件的相对路径)
BeanFactoryfactory = new XmlBeanFactory(rs);
org.springframework.context.ApplicationContext
应用上下文
应用上下文提供更多的功能,包括资源获取、国际化等等。
对于有单例要求的实例,在加载的时候就已经实例化。
org.springframework.context.support.ClassPathXmlApplicationContext
ApplicationContext实现,它可以依据Classpath查找配置文件
ApplicationContextctx
= new ClassPathXmlApplicationContext(配置文件的相对路径)
作用,使用方式,区别
组件:
组件的生命周期(见图)
基本装配:
IOC的实现方式:
(1)基于接口 type1:要求组件实现容器提供的特定的接口,这种方案
属于浸入式方案,耦合性大,现在不再使用。
(2)基于set方法 type2
(3)基于构造器 type3
set方式:
基本类型:8种基本类型+String
<property><value>值</...>
Object:
<property><refbean="bean的id"/></...>
<property><ref local="bean的id"/></...>
local要求两个Bean在同一个配置文件里
<property><beanid.....></..>
该Bean只能被当前Bean使用
集合:(P24)
list,set,map,properties
null:
<property><null/>
构造器方式:
比较set与构造器方式:
构造器方式优点:可以保证所有要使用的属性一定初始化了。
自动装配:(PPT34)
让容器依据某种规则,自动地对组件实施装配
autowire=""
byName:匹配属性名与bean的名字
byType:寻找配置文件,匹配属性的类型与bean 的类型,成功就设置
如果匹配的个数超过一个,报错。
constructor:匹配构造器,看构造器的参数类型与bean的类型是否一致,
成功就设置,如果匹配的个数超过一个,报错。
autodetect:(自动检测)先按照constructor(构造器方式),然后byType(方式)
自动装配于手动装配可以混合使用!手动的优先级要高
生产环境不能使用自动装配的缺点:⑴配置不够清晰,维护不方便
⑵易出错
示例:
检查装配情况:
dependency-check=""
simple:检查基本类型属性是否装配成功
object:检查Object类型装配是否成功
all:全检查
dependency-check与autowire配合使用。
尽量少用autowire,在原型开发的时候用于快速开发。
装配中的其它情况:
单例:
scope=""
singleton(单例),prototype(原型)
初始化与销毁
init-method,destroy-method
Bean(是一个抽象的bean类不能够被实例化)定义的继承:
继承抽象:abstract="true",parent=""
也可直接继承
工厂方式创建实例:(ppt30)
静态工厂
factory-method=""
工厂
factory-bean=""
factory-method=""
Day2大纲
特殊的Bean装配:
1、后处理Bean
接口:org.springframework.beans.factory.config.BeanPostProcessor
示例:所有Bean的字符串属性改成大写
练习:
Spring已经实现该接口的BeanPostProcessor(不用再注册)
ApplicationContextAwareProcessor:
把应用上下文传递给所用实现了ApplicationContextAware接口的Bean
ApplicationContextAware接口使用举例,可参照事件监听机制
DefaultAdvisorAutoProxyCreator(自动对Bean应用切面)
2、Bean工厂后处理(只能在应用上下文中使用)
接口:org.springframework.beans.factory.config.BeanFactoryPostProcessor
(Bean工厂后处理)
--在容器读入培植文件 Bean还没有实例化之前调用。
示例:统计载入到工厂的Bean的个数
Spring内部接口实现:
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer--实现了BeanFactoryPostProcessor.
分散配置(PPT42)
示例:
练习:
写一个数据库的配置
属性编辑
org.springframework.beans.factory.config.CustomEditorConfigurer
示例:
练习:
CustomerInfo("fname-lname-address")
3、事件监听
1)自定义事件,通过继承org.springframework.context.ApplicationEvent
2)自定义监听器,实现 org.springframework.context.ApplicationListener,并注册
3)发布事件,为得到应用上下文,
必须实现org.springframework.context.ApplicationContextAware接口
示例:
练习:
AOP:
几个概念:
(1)目标对象(target)
就是被代理的对象,也就是具体的业务逻辑。比如OrderService
(2)切面 (Aspect)
交叉业务,也就是通用的业务逻辑,比如日志、事务。
(3)连接点 (Jointpoint)
切面可以插入的地点,主要有方法、属性
(4)切入点 (Pointcut)
指定哪些连接点可以应用切面/通知
(5)通知(Advice)
切面的具体实现
Advice:
切面的实现
类型:
(1)org.springframework.aop.MethodBeforeAdvice
在方法调用之前,做处理。
不能够改变返回值
不能够改变目标方法的流程,也不能中断流程的处理过程(除非抛出异常)
(2)org.springframework.aop.AfterReturningAdvice
在方法调用之后,做处理。
不能够改变返回值
不能够改变目标方法的流程,也不能中断流程的处理过程(除非抛出异常)
(3)org.aopalliance.intercept.MethodInterceptor--aop联盟定义的接口。
在方法调用之前以及之后,做处理。
可以改变返回值,也可以改变流程。
(4)org.springframework.aop.ThrowsAdvice--里面没有定义任何方法。
在方法抛出异常后,做处理。
当该通知处理完异常后,会简单地将异常再次抛出给目标调用方法。
内置的创建代理类:
org.springframework.aop.framework.ProxyFactoryBeanppt55
proxyInterfaces,interceptorNames
配置过程:
(1)配置目标对象
(2)配置通知
(3)利用ProxyFactoryBean将通知织入到目标对象,形成一个动态代理对象
(4)客户端使用动态代理来访问目标对象的方法。
在默认情况下,通知会应用到所有的方法之上。
Pointcut:ppt48
根据方法和类决定在什么地方织入通知
Advisor
将Pointcut与Advice结合到一起。
自定义切入点:
步骤:
1)实现org.springframework.aop.ClassFilter
2)实现org.springframework.aop.MethodMatcher
3)实现org.springframework.aop.Pointcut
4)实现org.springframework.aop.PointcutAdvisor
注意:
在此可定义
privateAdvice advice;
privatePointcut pointcut;
在配置文件中,将自定义的切入点与通知绑订到一起
5)利用ProxyFactoryBean将advisor织入到目标对象
预定义切入点:
静态切入点:
org.springframework.aop.support.NameMatchMethodPointcutAdvisor
根据方法名称的特点进行匹配
mappedName/mappedNames,advice(ref)
mappedNames
<list>
doOne
doTwo
doThree
</list>
org.springframework.aop.support.RegexpMethodPointcutAdvisor
pattern,advice(ref)
动态切入点:
org.springframework.aop.support.ControlFlowPointcut
切入点的交叉与合并:
Pointcuts.union
pointcuts.union(pointA,pointB)返回是一个point.
Introduction
一种特殊类型的Advice
为类动态增加方法和属性
编程步骤:
1)实现org.springframework.aop.IntroductionInterceptor或
继承org.springframework.aop.support.DelegatingIntroductionInterceptor
2)使用org.springframework.aop.support.DefaultIntroductionAdvisor
自动代理:
Spring在生成代理对象的时候,默认情况下,会使用被代理对象的接口来生成
代理对象。
如果被代理对象没有实现接口,此时,Spring会使用CGLIB生成代理对象,此时
该代理对象是被代理对象的子类。
org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator
根据类的名称来为符合相应名称的类生成相应代理对象。
beanNames(list),interceptorNames
org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator
自动将Advisor与匹配的Bean进行绑定
只能与Advisor配合使用
练习:
检测方法执行时间
IOC:
BEAN生命周期
后处理BEAN
工厂的后处理
事件机制
BEAN的继承
AOP
AOP概念
pointcut
advice
joinpoint
pointcut
introduction
wavein
target
proxy
几个通知
proxyFactory
切入点
Day3大纲
一、数据访问
1、Spring的数据访问设计思想(DAO、模板方法)PPT77
2、数据源配置:
方式一:Spring内置实现 DriverManagerDataSource
<beanid ="dataSource" class
="org.springframework.jdbc.datasource.DriverManagerDataSource">
<propertyname="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/hibdb</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>windows</value>
</property>
</bean>
方式二:DBCP提供的BasicDataSource
<beanid="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<propertyname="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<propertyname="url">
<value>jdbc:mysql://localhost:3306/hibdb</value>
</property>
<propertyname="username">
<value>root</value>
</property>
<propertyname="password">
<value>windows</value>
</property>
</bean>
方式三:JNDI数据源 (在讲解SSH整合时再说明)
JNDI数据源:(mysql5,tomcat5.5)
step1:
在server.xml中:
<Resourcename="jdbc/mydatasource" auth="Container"
description="DB Connection"
type="javax.sql.DataSource"username="root"
password="windows"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/tarena"maxActive="5" />
step2:
在context.xml中(conf\context.xml):
<ResourceLink name="jdbc/mydatasource"
global="jdbc/mydatasource" type="javax.sql.DataSourcer"/>
step3:
在beans-config.xml:
<beanid="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/mydatasource</value>
</property>
</bean>
3、JDBC支持:
step1: 配置数据源
step2:配置JdbcTemplate
<beanid="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<propertyname="dataSource">
<refbean="dataSource" />
</property>
</bean>
step3:配置DAO
<beanid="orderDao" class="lab5.OrderDAOImpl">
<propertyname="jt"><ref bean="jdbcTemplate"/></property>
</bean>
注意: 查询时,使用RowMapper
4、hibernate支持:
step1: 配置数据源
step2: 配置sessionfactory
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<propertyname="mappingResources">
<list>
<value>lab6/Order.hbm.xml</value>
</list>
</property>
<propertyname="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop
key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
step3: 配置DAO
<bean id="orderDao"class="lab6.OrderDAOHibernateImpl">
<property name="sessionFactory">
<ref bean="mySessionFactory" />
</property>
</bean>
注意:以上配置是要求dao 继承HibernateDaoSupport
二、事务:
1、Spring事务机制
声明式事务、事务管理器
2、hibernate事务p72
step1: 配置数据源
step2:配置sessionfactory(同上)
step3:配置事务管理器
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="mySessionFactory" />
</property>
</bean>
step4:创建事务服务代理
<bean id="saleService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="proxyInterfaces">
<value>lab7.SaleService</value>
</property>
<property name="transactionManager">
<ref bean="myTransactionManager" />
</property>
<property name="target">
<ref bean="saleServiceTarget" />
</property>
<property name="transactionAttributes">
<props>
<propkey="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
注:
事务属性描述格式:
传播行为,隔离级别,只读事务(readonly),回滚规则
在默认情况下,Spring的容器对于非受查异常(服务模块中抛出的非受查异常
)
,会回滚事务。对于受查异常,会提交事务。
如果即使发生了某种受查异常,也要回滚事务,可以用 “- 异常类型“来声
明。
同样,对于非受查异常,如果不要求回滚事务,可以用"+异常类型"来声明
3、简化事务配置
继承、自动代理
三、Spring与struts整合:
前提:
配置ContextLoaderPlugIn插件:负责装载spring应用上下文
<plug-inclassName="org.springframework.web.struts.ContextLoaderPlugIn">
<set-propertyproperty="contextConfigLocation" value="/WEB-
INF/config/sale.xml" />
</plug-in>
方式一:通过Spring的ActionSupport类
ActionSupport类:
知道ApplicationContext的获得方式。
步骤:
1、Action直接继承ActionSupport
2、使用ApplicationContext ctx = getWebApplicationContext();取得Spring上下文
3、取得相应Bean
注意:有可能需要替换commons-attributes-compiler.jar包。
优点:
简单
缺点:
耦合高
违反IOC
无法使用多方法的Action
方式二:通过Spring的DelegatingActionProxy类
步骤:
1、Action中,使用IOC获得服务
2、配置struts-config.xml
<actionpath="/somepath"
type="org.springframework.web.struts.DelegatingActionProxy"/>
3、在Spring配置文件中
<beanname="/somepath" class="SomeAction">
<propertyname="service"><ref bean=""/>
</bean>
注意,要用bean name命名。
/somepath:Action的path
优点:
不使用Spring api编写 Action
利用了IOC装配。
可以利用容器的scope="prototype"来保证每一个请求有一个单独的Action来处理,
避免struts中Action的线程安全问题。
缺点:
struts配置文件中,所有path都映射到同一个代理类
方式三:通过Spring的DelegatingRequestProcessor类
步骤:
1、Action中,使用IOC获得服务
2、配置struts-config.xml
<controller
processorClass="org.springframework.web.struts.DelegatingRequestProcessor"/>
3、在Spring配置文件中
<beanname="/somepath" class="SomeAction">
<propertyname="service"><ref bean=""/>
</bean>
小结:
Spring与Struts整合方式只有两种:
(1)由Spring容器来管理Action(方式二,方式三)
(2)Action处于容器之外(方式一)
注意:
中文问题:
设置过滤器,设置页面编码,数据库编码