当前位置:
文档之家› 主流开源框架---Spring
主流开源框架---Spring
<value>logafter</value>
<value>logaround</value>
<value>logthrows</value>
</list> </property> </bean>
使用Advisor
Advisor是Spring框架AOP组件独有的类型 Advisor将一个Advice对象和一个切入点关联,决定在当 前实例是否应用Advice Spring的API中提供了接口Advisor,该接口拥有很多实现 类
<bean id="dataSource"
class="mons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> …… </bean> </beans>
主流开源框架
第三部分:Spring框架
IoC AOP Spring整合JDBC/Struts2/Hibernate Spring中的事务处理 整合Struts2/Hibernate/Spring
章节目录
第一章 Spring概述 第二章 IoC(控制反转)
第三章 AOP(面向切面编程)
第四章 Spring整合Struts2
– ng.reflect.Proxy类 – ng.reflect.InvocationHandler接口
章节目录
第一章 Spring概述 第二章 IoC(控制反转)
第三章 AOP(面向切面编程)
第四章 Spring整合Struts2
第五章 Spring整合JDBC
第六章 Spring整合Hibernate 第七章 Spring中的事务管理
使用RegexpMethodPointcutAdvisor
RegexpMethodPointcutAdvisor是Advisor的一个常用的实 现类 使用步骤与NameMatchMethodPointcutAdvisor类似,区别 在于Advisor的配置不同
<bean id="logafteradvisor" class="org.springframework.aop.support.RegexpMethodPointcutA dvisor"> <property name="advice">
注入的两种方式
IoC容器实例化bean对象后,需要对其注入属性值,注入 属性的方式有两种
– setter注入 – 构造方法注入
setter注入
setter注入是指使用类setXXX方法对属性进行赋值的方式
<bean id="course" class="com.etc.vo.Course">
<property name="id">
使用NameMatchMethodPointcutAdvisor
NameMatchMethodPointcutAdvisor是Advisor的一个常用 的实现类 具体使用步骤
– 配置Advice对象 – 配置NameMatchMethodPointcutAdvisor对象, 应用Advice – 在代理对象的interceptorNames中,引用Advisor 的bean
在Eclipse中开发Spring的步骤
Spring框架包含一系列的特性,可以根据应用的具体需要 而选择使用部分组件
– 添加Spring包 – 生成配置文件applicationContext.xml
章节目录第一章 Spring来自述 第二章 IoC(控制反转)
第三章 AOP(面向切面编程)
第四章 Spring整合Struts2
bean的作用域
bean有5种作用域,其中有3种只能在Web环境中使用
– singleton – prototype – request – session – global session
<bean id="dataSource" class="mons.dbcp.BasicDataSource" scope="prototype"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value>
</constructor-arg>
<constructor-arg index="1"> <value>Java</value>
</constructor-arg>
<constructor-arg index="2">
<value>1000</value>
</constructor-arg> </bean>
第八章 SSH整合
Spring整合Struts2的步骤
导入必要的类库
要使用Spring整合Struts2框架,除了需要导入Spring和 Struts2框架必要的类库外,还必须导入支持整合的特定 类库
配置web.xml
要使用Spring整合Struts2框架,需要在web.xml中进行特 殊配置
– 配置一个FilterDispatcher – 配置一个listener来完成加载Spring配置文件的 功能 – 通过context-param指定特定的配置文件
修改STRUTS2框架的ACTION类
使用Spring整合Struts2框架的核心思想就是将Struts2的 Action类实例交给Spring框架的IoC容器装配管理
IoC使用实例(教程案例)
为了能够更好理解IoC,通过修改“教程案例”的业务逻 辑部分,将其中重要对象使用IoC生成
– 服务对象 – 数据层对象
章节目录
第一章 Spring概述 第二章 IoC(控制反转)
第三章 AOP(面向切面编程)
第四章 Spring整合Struts2
第五章 Spring整合JDBC
快速入门AOP
修改“教程案例”,使用Spring AOP添加记录日志功能
– 创建目标对象(Target Object) – 创建通知(Advice) – 在IoC容器中,生成代理对象,将通知织入到目 标对象中
不同类型的Advice
在Spring的AOP组件中,拦截器有两种类型,即Advice和 Advisor。Advice一共有四种类型
<ref bean="logafter"/>
</property>
<property name="patterns">
<list>
<value>customer*</value>
</list> </property> </bean>
AOP的技术基础—代理模式
AOP的技术基础—动态代理
动态代理是在运行时实现代理模式的方法,Java的JDK对 其进行了实现 JavaSE中实现动态代理的主要类有两个
在代理对象中引用Advice
<bean id=“serviceProxy” class=“org.springframework.aop.framework.ProxyFactoryBean”> <property name="interceptorNames"> <list> <value>logbefore</value>
</property>
bean的初始化与析构
Spring API中提供了InitializingBean和DisposableBean 两个接口,可以用来修改容器中bean的行为
– InitializingBean接口中有如下方法:void afterPropertiesSet():当bean的属性被赋值后, 调用该方法进行初始化。 – DisposableBean接口中有如下方法:void destroy():bean实例销毁前调用。
需要使用IoC的对象
并不是所有对象都需要使用IoC,常常使用IoC的对象有
– 服务层对象 – 数据层对象 – 表示层对象 – 工厂类对象 – JMS的Queue、Topic对象
如何实例化Bean
IoC容器有以下几种方式实例化Bean
– 通过无参构造方法实例化 – 通过静态工厂方法实例化 – 通过非静态工厂方法实例化
IoC的参数配置
不管使用哪种方式注入属性,都是对属性进行赋值。属性 类型不同时,具体配置也不同。一般有以下三种参数。
– 基本类型和String – 其他bean – null值
集合类型参数配置
类的属性往往是集合类型,可以使用IoC配置集合类型参 数,有以下几种常用类型
– List:<list> – Set:<set> – Map:<map> – Properties:<props>
IoC的使用
IoC就是将设计好的类交给容器控制,容器负责实例化对 象,并将对象依赖的属性注入,从而得到一个可用的对象
– bean都需要在applicationContext.xml中使用 <bean>配置 – 通过ApplicationContext接口中的getBean方法, 可以获得IoC容器中的bean对象