四。动态代理
实现InvocationHandler接口
public Object bind(Object delegate) {
this.delegate=delegate;
return Proxy.newProxyInstance(
delegate.getClass().getClassLoader(),
delegate.getClass().getInterfaces(),
this);
}
实现Invoke方法,完成代理的操作
Object result=null;
。。。。
return result;
使用时;
new Handler实例 调用Handler实例的bind方法绑定对象,返回对象接口
public class TransactionProxy implements InvocationHandler{
private Object delegate;
public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {
Object ret=null;
System.out.println("开启事物");
// ret=method.invoke(proxy, args);
//执行这个与上面那个无关了
ret=method.invoke(delegate, args);
System.out.println("提交事物");
return ret;
}
五。AOP观念与术语
Cross-cutting concern 横切
Aspect 切面(方面)
Advice Aspect切面的具体实现
Joinpoint 连接点
pointcut 切入点
Target 目标
Introduction 看起来 好像是 为一个现有的类 添加 方法
proxy 代理
weave 织入 将切面的具体实现按照相应的连接点应用到目标上的过程
六 Advice
由于将Advice织入Target的时机不同,spring提供了一下几种不同额Advice
1》Before Advice
定义接口
定义实现类
定义Advice类实现MethodBeforeAdvice接口实现before方法完成切入的功能
配置文件:注入Advice实现类,注入ProxyFactoryBean,并配置如下属性。
proxyInterfaces接口、target实现类、interceptorNames《list》---拦 截器
Advice实现类使用时:获取ProxyFactoryBean,强转接口使用
2》After Advice
基本同上,定义Advice 类实现AfterReturningAdvice接口
实现AfterReturning方法完成切入的功能。
3>Around Advice
基本同上,定义Advice/Interceptor类 实现MethodInterceptor接口,实现invoke方法,
此时和动态处理是一样,就是调用原来的方法不一样。
Object result=null;
。。。。
result=methodInvocation.proceed();
.......
4>Throw Advice(不常用)
基本同上,定义Advice类,实现ThrowsAdvice接口,写一个
afterThrowing(Method,Object【】 args,Object【】 target,Throwable subclass)方法实现类发生 异常时的处理。
七 Pointcut,Advisor
可以定义更细部的织入时机,pointcut定义了Advice的应用时机,在Spring中,使用
PointcutAdvisor将Pointcut和Advice结合成一个对性,Spring内建的Pointcut都有 对应的PointcutAdvisor。
1.NameMatchMethodPointcutAdvisor
配置文件:直接注入NameMatchMethodPointcutAdvisor,配置属性如下:
mappedName value目标短方法名的匹配或
mappedNames list value
advice应用已经注入Advice实现类。
修改已注入的ProxyFactoryBean的属性interceptorNames<list>值为上面注入的 PointcutAdvisor
使用时:获取ProxyFactoryBean,强转为接口使用。如果访问的方法符合
PointcutAdvisor中的mappedName的匹配,则切入Advice实现类的方法。
2. RegexpMethodPointcutAdvisor(不常用)
同上,唯一的区别就是它使用正则匹配 完整方法名称,而不是已经注入的bean id名 称匹配。
.任意单一字符
+一次/多次
*零次/多次
\转义
配置文件:
直接注入RegexpMethodPointcutAdvisor,配置如下:
pattern value 匹配方法名称的正则表达式(注意方法名可能很长,因为包含名和类名)
或者patterns list value
advice引用已注入的Advice实现类。
八。Introduction
在Spring中,Introduction是一种特殊的Advice,它可以直接介入整个对象的行为,,就好像给对象凭空增加了一些行为,可以理解为方法。
1 IntroductionInterceptor(不常用)
定义接口,定义实现类,定义包含新增方法的接口
定义Introduction实现类,实现 判断参数的class是否来自于 包含新增主法接口类别的定义如下:
return clazz.isAssignableFrom(class);
修改invoke方法如下:
if (implementsInterface(arg0.getMethod().getDeclaringClass())) {
return arg0.getMethod().invoke(this, arg0.getArguments());
}else {
return arg0.proceed();
}
配置文件:注入实现类,注入Introduction实现类,注入DefaultIntroductionAdvisor,配置构造器参数如下:
0引用 已注入Introduction实现类
1包含新增方法的接口
注入ProxyFactoryBean,配置属性如下:
proxyInterfaces接口
target引用已注入实现类
InterceptorNames《list》已注入的DefaultIntroductionAdvisor
2.DelegatingIntroductionInterceptor
它是Spring aop中为IntroductionInterceptor提供的实现类,用起来更为方便
定义接口
定义实现类
定义包含新增的方法接口
定义Introduction实现类 继承DelegatingIntroductionInterceptor并实现包含新增方法的接口。
实现新增方法体即可。
配置文件同上
九。自动代理
BeanNameAutoProxyCreator
十。spring整合struts
1,在struts配置文件中加载插件
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml"/>
</plug-in>
2,将struts Action请求委托Spring框架代理
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller>
3在Spring配置文件中注入struts action相应的类
<bean...
十ssh
1在Spring文件中配置如下,transactionMananger
<bean id="htm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="hibernateSessionFactory"></ref>
</property>
</bean>
<bean id="ti" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="htm"></ref>
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*ServiceImpl</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>ti</value>
</list>
</property>
</bean>
2 让spring管理session————OpenSessionInView
<filter>
<filter-name>sessionFilter</filter-name>
<filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>hibernateSessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>sessionFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
3 struts加载application有第二种方法
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>