面向接口(抽象)编程的概念与好处
IOC/DI的概念与好处
inversion of control
dependency injection
AOP的概念与好处
Spring简介
Spring应用IOC/DI(重要)
xml
annotation
Spring应用AOP(重要)
xml
annotation
Struts2.1.6 + Spring2.5.6 + Hibernate3.3.2整合(重要)
opensessionInviewfilter(记住,解决什么问题,怎么解决)
Spring JDBC
场景:用户添加
Spring_0100_AbstractOrientedProgramming
不是AOP:AspectOriented Programming
好处:灵活
把自己new的东西改为由容器提供
初始化具体值
装配
好处:灵活装配
倚赖注入:谁调用谁,把后面的谁设置到前面的谁就是依赖注入。
项目名称:Spring_0200_IOC_Introduction
环境搭建
只用IOC
spring.jar , jarkata-commons/commons-loggin.jar
IOC容器
实例化具体bean
动态装配
AOP支持
安全检查
管理transaction
FAQ:不给提示:
window – preferences – myeclipse – xml – xml catalog
User Specified Entries – add
Location: D:\share\0900_Spring\soft\spring-framework-2.5.6\dist\resources\spring-beans-2.5.xsd
URI: file:///D:/share/0900_Spring/soft/spring-framework-2.5.6/dist/resources/spring-beans-2.5.xsd
Key Type: Schema Location
Key: http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
注入类型
Spring_0300_IOC_Injection_Type
setter(重要)
构造方法(可以忘记)
接口注入(可以忘记)
id vs. name
Spring_0400_IOC_Id_Name
name可以用特殊字符
简单属性的注入
Spring_0500_IOC_SimpleProperty
<property name=… value=….>
<bean 中的scope属性
Spring_0600_IOC_Bean_Scope
singleton 单例
proptotype 每次创建新的对象
集合注入
Spring_0700_IOC_Collections
很少用,不重要!参考程序
自动装配
Spring_0800_IOC_AutoWire
byName
byType
如果所有的bean都用同一种,可以使用beans的属性:default-autowire
生命周期
Spring_0900_IOC_Life_Cycle
lazy-init (不重要)
init-method destroy-methd 不要和prototype一起用(了解)
Annotation第一步:
修改xml文件,参考文档<context:annotation-config/>
@Autowired
默认按类型bytype
如果想用byName,使用@Qulifier
写在privatefield(第三种注入形式)(不建议,破坏封装)
如果写在set上,@qualifier需要写在参数上
@Resource(重要)
加入:j2ee/common-annotations.jar
默认按名称,名称找不到,按类型
可以指定特定名称
推荐使用
不足:如果没有源码,就无法运用annotation,只能使用xml
@Component @Service @Controller @Repository
初始化的名字默认为类名首字母小写
可以指定初始化bean的名字
@Scope
@PostConstruct = init-method; @PreDestroy = destroy-method;
面向切面编程Aspect-Oriented-Programming
是对面向对象的思维方式的有力补充
Spring_1400_AOP_Introduction
好处:可以动态的添加和删除在切面上的逻辑而不影响原来的执行代码
Filter
Struts2的interceptor
概念:
JoinPoint:@Before("execution(voidcom.yangunilay.dao.impl.UserDaoImpl.add(com.yangunilay.model.User))"),指save执行之前。
PointCut:@Pointcut(“execution(*com.yangunilay.*.*(..))”),连接点的集合
Aspect(切面):@Ascept,所放的那个类就是一个切面
Advice:@Before,@After等等,加在切入点上的逻辑
Target:被代理对象
Weave:支路
两种方式:
使用Annotation
使用xml
Annotation
加上对应的xsd文件spring-aop.xsd
beans.xml <aop:aspectj-autoproxy />
此时就可以解析对应的Annotation了
建立我们的拦截类
用@Aspect注解这个类
建立处理方法
用@Before来注解方法
写明白切入点(execution…….)
让spring对我们的拦截器类进行管理@Component
常见的Annotation:
@Pointcut
@Before
@AfterReturning
@AfterThrowing
@After
@Around
织入点语法
void !void
参考文档(*..)
xml配置AOP
把interceptor对象初始化
<aop:config
<aop:aspect …..
<aop:pointcut
<aop:before
Spring 指定datasource
参考文档,找dbcp.BasicDataSource
c3p0
dbcp
proxool
在DAO或者Service中注入dataSource
在Spring中可以使用PropertyPlaceHolderConfigure来读取Properties文件的内容
Spring整合Hibernate
<bean .. AnnotationSessionFactoryBean>
<property dataSource
<annotatedClasses
引入hibernate系列jar包
User上加Annotation
UserDAO或者UserServie注入SessionFactory
jar包问题一个一个解决
声明式的事务管理
事务加在DAO层还是Service层?
annotation
加入annotation.xsd
加入txManagerbean
<tx:annotation-driven
在需要事务的方法上加:@Transactional
需要注意,使用SessionFactory.getCurrentSession不要使用OpenSession
@Transactional详解
什么时候rollback
运行期异常,非运行期异常不会触发rollback
必须uncheck(没有catch)
不管什么异常,只要你catch了,spring就会放弃管理
事务传播特性:propagation_required
read_only
xml(推荐,可以同时配置好多方法)
<bean txmanager
<aop:config
<aop:pointcut
<aop:advisor pointcut-ref advice-ref
<tx:advice: id transaction-manager =
HibernateTemplate、HibernateCallback、HibernateDaoSupport(不重要)介绍
设计模式:TemplateMethod
Callback:回调/钩子函数
第一种:(建议)
在spring中初始化HibernateTemplate,注入sessionFactory
DAO里注入HibernateTemplate
save写getHibernateTemplate.save();
第二种:
从HibernateDaoSupport继承
必须写在xml文件中,无法使用Annotation,因为set方法在父类中,而且是final的
spring整合hibernate的时候使用packagesToScan属性,可以让spring自动扫描对应包下面的实体类
需要的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 |
.. |
hibernate transaction |
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 |
|
BestPractice:
将这些所有的jar包保存到一个位置,使用的时候直接copy
步骤
加入jar包
首先整合Spring+ Hibernate
建立对应的package
dao / dao.impl / model / service / service.impl/ test
建立对应的接口与类框架
S2SH_01
建立spring的配置文件(建议自己保留一份经常使用的配置文件,以后用到的时候直接copy改)
建立数据库
加入Hibernate注解
在实体类上加相应注解@Entity@Id等
在beans配置文件配置对应的实体类,使之受管
写daoservice的实现
加入Spring注解
在对应Service及DAO实现中加入@Component,让spring对其初始化
在Service上加入@Transactional或者使用xml方式(此处建议后者,因为更简单)
在DAO中注入sessionFactory
在Service中注入DAO
写DAO与Service的实现
写测试
整合Struts2
结合点:Struts2的Action由Spring产生
步骤:
修改web.xml加入struts的filter
再加入spring的listener,这样的话,webapp一旦启动,spring容器就初始化了
规划struts的action和jsp展现
加入struts.xml
修改配置,由spring替代struts产生Action对象
修改action配置
把类名改为bean对象的名称,这个时候就可以使用首字母小写了
@Scope(“prototype”)不要忘记
struts的读常量:
struts-default.xml
struts-plugin.xml
struts.xml
struts.properties
web.xml
中文问题:
Struts2.1.8已经修正,只需要改i18n.encoding= gbk
使用spring的characterencoding
需要严格注意filter的顺序
需要加到Struts2的filter前面
LazyInitializationException
OpenSessionInViewFilter
需要严格顺序问题
需要加到struts2的filter前面
struts2-spring-plugin问题
/*@Resource(name ="memberServiceImpl")不起作用了,action归struts2-spring-plugin插件管,不归spring管,spring容器产生负责给action用的对象
* 做法:1。把@Resource(name= "projectServiceImpl")写在private属性上
* 2。在action上什么都不写,默认按照名字自动注入,把名字都约定好,而ServiceImpl上写上@Componentaction对应service属性的名字就行
* 3.按照类型注入,将struts2-spring-plugin改成按类型注入,则ServiceImpl上写上@Component也不用写了
* */