三、面向切面编程AOP(Aspect oriented Programming)

本文介绍了动态代理、Spring中AOP的注解及配置方式,包括动态代理类实现InvocationHandler接口,Spring AOP中AspectJ的注解配置,以及通过XML配置实现的AOP等方法。

1. 动态代理

动态代理类需要实现InvocationHandler接口并重写其中的invoke()方法

实例如下:

实例其功能是在调用执行目标对象的方法之前调用执行checkSecurity方法

publicclass SecurityHandler implements InvocationHandler {

private Object targetObject;

public Object newProxy(Object targetObject) {

this.targetObject = targetObject;

return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(),

targetObject.getClass().getInterfaces(),

this);

}

@Override

public Object invoke(Object proxy, Method method, Object[] args)

throws Throwable {

checkSecurity();

Object returnValue = null;

try {

returnValue = method.invoke(this.targetObject, args);

} catch (Exception e) {

e.printStackTrace();

thrownew RuntimeException(e);

}

return returnValue;

}

privatevoidcheckSecurity() {

System.out.println("SecurityHandler.checkSecurity()");

}

}

SecurityHandler handler = new SecurityHandler();

IUserManager userManager = (IUserManager)handler.newProxy(new UserManagerImpl());

userManager.addUser("zhaoTeacher", "123");

2. SpringAnnotation方式进行AOP

需要在代理类中以注解的方式进行标记

实例如下:

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.Pointcut;

//定义Aspect

@Aspect

publicclass SecurityHandler {

/**

*定义Pointcut,Pointcut的名称就是allAddMethod

*此方法不能有返回值和参数,

*该方法只是一个标识

*/

@Pointcut("execution(* add*(..)) || execution(* del*(..))")

privatevoid allAddMethod() {}

/**

*下面注解意思是在所有add方法之前执行checkSecurity()

*/

@Before("allAddMethod()")

privatevoid checkSecurity() {

System.out.println("SecurityHandler.checkSecurity()");

}

}

配置方法如下:

<aop:aspectj-autoproxy/><!-- 启用Annotation方式切入 -->

<bean id="userManager" class="com.xasxt.spring.UserManagerImpl"/>

<bean id="security" class="com.xasxt.spring.SecurityHandler"/>

3. 配置方式切入(@AspectJ支持)

配置实例如下:

具体实现效果同上面的注解方式以及动态代理方式。

<bean id="userManager" class="com.xasxt.spring.UserManagerImpl"/>

<bean id="securityHandler" class="com.xasxt.spring.SecurityHandler"/>

<aop:config>

<aop:aspect id="security" ref="securityHandler">

<aop:pointcut id="allAddMethod" expression="execution(* com.xasxt.spring.UserManagerImpl.add*(..))"/>

<aop:before method="checkSecurity" pointcut-ref="allAddMethod"/>

</aop:aspect>

</aop:config>

4. SpringAOP的支持

I、 SpringAdvice如何接收方法的参数:

l Aspect默认情况下不用实现接口,但对于目标对象(UserManagerImpl.java),在默认情况下必须实现接口;如果没有实现接口必须引入CGLIB库

l 我们可以通过在Advice中添加一个JoinPoint参数,这个值会由spring自动传入,从JoinPoint中可以取得参数值、方法名等等。

II、 如何选择代理:

l 如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP

l 如果目标对象实现了接口,也可以强制使用CGLIB实现AOP

l 如果目标对象没有实现接口,则必须采用CGLIB库,spring会自动在JDK动态代理和CGLIB之间转换

III、 如何强制使用CGLIB实现AOP

Ø 添加CGLIB库,SPRING_HOME/cglib/*.jar

Ø spring配置文件中加入下面配置:

<aop:aspectj-autoproxy proxy-target-class="true"/>

IV、 JDK动态代理和CGLIB字节码生成的区别

l JDK动态代理只能对实现了接口的类生成代理,而不能针对未实现接口的类

l CGLIB是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法;因为是继承,所以该类或方法最好不要声明成final

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值