第一次课:Spring概念、运行流程

  1. 分页源码展示、Log4J的使用
  2. Spring框架的优势 [轻量、无植入、提出框架整合的概念SSH]
  3. Spring用途: IOC(依赖注入/控制反转)  AOP(面向切面) 整合
  4. 抽象工厂模式的缺点
  5. Spring的属性、构造注入实现

 

Spring可以包容其它框架,实现框架功能的最大化

Spring IOC: 把类与类之间的依赖, 配置到Spring配置文件中,依靠Spring配置文件来创建和维护, 可以把Spring看作一个大型的 抽象工厂

 

第二次课:SpringStruts整合实现

1. Spring与Struts 手动获取方式整合(缺点)

a)         由于Action是非单例, 每次都会加载XML配置文件,和实例话一个新的业务逻辑类. 这些统统只需要一份即可

b)        如果XML配置只需要加载一次.我们可以采用application监听器 项目启动的时候加载即可

c)         也业务逻辑类,设置set方法,才可以依赖注入

2. Spring与Struts自动获取方式整合 整合流程:

a)         加载Spring与Struts集成的lib包 Spring WEB

b)        在web.xml配置监听器 和 正确的applicationContext.xml文件路径

c)         org.springframework.web.context.ContextLoaderListener.class

d)        导入struts2-spring-plugin-2.1.6.jar 此包可以告诉Action在Spring容器中去查找业务逻辑类

e)         通过如上配置XML和业务逻辑类,都只实例化一次,提高的性能

3. 监听器如何获取业务逻辑类

a)         WebApplicationContext 实现 ApplicationContext ,它是一个子接口,其实就是ApplicationContext在Struts中的应用

b)        ContextLoaderListener 它加载了ApplicationContex.xml 并且把它存储到application内置对象中. 所以WebApplicationContextUtils:才可以到application内置对象中去配置文件

sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

ContextLoaderListener 只是一个普通的application监听器,在启动加载而已

ContextLoader:此类完成了spring配置文件的加载

public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";

private static final String DEFAULT_STRATEGIES_PATH = "ContextLoader.properties";

private static final Properties defaultStrategies;

4. Spring与Struts整合流程总结:

5 .String与Hibernate整合切入点:在业务逻辑类里面有很多恒切面的代码,这些代码, 我们可以使用Spring的AOP来动态完整, 这就需要Spring来管理sessionàSessionFactoryà configuration所以我们要把hibernate的链接数据库的配置部分交给spring来管理

6. eg:

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <beans
 4 
 5 xmlns="http://www.springframework.org/schema/beans"
 6 
 7 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 8 
 9 xmlns:tx="http://www.springframework.org/schema/tx"
10 
11 xmlns:aop="http://www.springframework.org/schema/aop"
12 
13 xmlns:p="http://www.springframework.org/schema/p"
14 
15 xsi:schemaLocation="http://www.springframework.org/schema/beans
16 
17 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
18 
19 http://www.springframework.org/schema/aop
20 
21 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
22 
23 http://www.springframework.org/schema/tx
24 
25 http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
26 
27 > 
28 
29 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
30 
31 <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
32 
33 </bean>
34 
35 <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
36 
37 <property name="sessionFactory" ref="sessionFactory"></property>
38 
39 </bean>
40 
41 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
42 
43 <property name="sessionFactory" ref="sessionFactory"></property>
44 
45 </bean>
46 
47 <tx:advice id="advice" transaction-manager="transactionManager">
48 
49 <tx:attributes>
50 
51 <tx:method name="save*" propagation="REQUIRED"/>
52 
53 <tx:method name="update*" propagation="REQUIRED"/>
54 
55 <tx:method name="delete*" propagation="REQUIRED"/>
56 
57 <tx:method name="*" read-only="true"/>
58 
59 </tx:attributes>
60 
61 </tx:advice>
62 
63 <aop:config>
64 
65 <aop:advisor advice-ref="advice" pointcut="execution(* dong.shopping.impl.*.*(..))" />
66 
67 </aop:config>
68 
69 </beans>

 

第三次课:S2SH整合实现

1. Spring AOP的原理、实现

  a)  目前我们的事务是编程式事务, 如果要声明式事务则要Spring来管理configuraction

2. Spring声明事务

 

 1 <!-- 配置声明式事务 -->
 2     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
 3         <property name="sessionFactory" ref="sessionFactory" />
 4     </bean>
 5     
 6     <!-- 配置通知,告知是什么类型的事务 -->
 7     <tx:advice id="advice" transaction-manager="transactionManager">
 8         <tx:attributes>
 9             <tx:method name="save*" propagation="REQUIRED"/>
10             <tx:method name="delete*" propagation="REQUIRED"/>
11             <tx:method name="update*" propagation="REQUIRED"/>
12             <tx:method name="*" read-only="true"/>
13         </tx:attributes>
14     </tx:advice>
15     
16     <aop:config>
17         <aop:advisor advice-ref="advice" pointcut="execution(* it.shopping.service.*.*(..))" />
18     </aop:config>

 

声明式事务要注意的两个问题:

1:FactoryBean 是一个工厂类,可以用来创建你想要的对象,和设置单例模式

LocalSessionFactoryBean 本质上返回的就是SessionFactory类型

2:hibernateTemplate:封装了session,但是有些功能实现不了,就要使用hibernateTemplate回调模式来完成

 

 

 

 

posted on 2012-10-22 14:46  逍遥忘却  阅读(252)  评论(0编辑  收藏  举报