Spring Aop
AOP是Aspect Oriented Programming的缩写,意思是面向方面编程,一种新兴的编程技术。
一、原理:通过 Proxy方式完成切面编程.
作用:就是为了更清晰的逻辑,可以让你的业务逻辑去关注自己本身的业务,而不去想一些其他的事情,这些其他的事情包括:安全,事物,日志等。
实现方式:
1. 声明代理类:
2. public class LogInterceptor implements InvocationHandler{//要实现InvocationHandler接口重写Invoke()方法
3. //目标对象
4. public Object target;
5. //在代理实例上处理方法调用并返回结果
6. public Object invoke(Object proxy, Method method, Object[] args)
7. throws Throwable {
8. beforeMethod(method);
9. //对带有指定参数的指定对象调用由此 Method 对象表示的底层方法
10. method.invoke(target, args);
11. return null;
12. }
13. public void beforeMethod(Method m){
14. System.out.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss ").format(new Date())+m.getName()+" AOP");
15. }
16. public Object getTarget() {
17. return target;
18. }
19. public void setTarget(Object target) {
20. this.target = target;
21. }
22.}
2.测试
public static void main(String[] args) {
UserDao u = new UserDaoImpl();
LogInterceptor li =new LogInterceptor();
li.setTarget(u);
//返回一个指定接口的代理类实例(通过实现接口来得到代理对像)
UserDao userDaoProxy=(UserDao) Proxy.newProxyInstance(u.getClass().getClassLoader(), new Class[]{UserDao.class}, li);
userDaoProxy.add("AOP Proxy");
}
二、概念
1、Advice(通知):定义在连接点做什么,为切面增强提供织入接口。(先定义要增强的功能)
有Before advice、AfterreturningAdvice、ThrowAdvice、AroundAdvice(MethodInteceptor)等
2.、JoinPoint(连接点)就是方法的前、后或者是抛出异常时。
3.切入点(Pointcut)
上面说的连接点的基础上,来定义切入点,你的一个类里,有15个方法,那就有几十个连接点了对把,但是你并不想在所有方法附近都使用通知(使用叫织入,以后再说),你只想让其中的几个,在调用这几个方法之前,之后或者抛出异常时干点什么,那么就用切点来定义这几个方法,让切点来筛选连接点,选中那几个你想要的方法。
4.切面(Aspect)
切面是通知和切入点的结合。现在发现了吧,没连接点什么事情,连接点就是为了让你好理解切点,搞出来的,明白这个概念就行了。通知说明了干什么和什么时候干(什么时候通过方法名中的before,after,around等就能知道),而切入点说明了在哪干(指定到底是哪个方法),这就是一个完整的切面定义。
5.引入(introduction)
允许我们向现有的类添加新方法属性。这不就是把切面(也就是新方法属性:通知定义的)用到目标类中吗
6.目标(target)
引入中所提到的目标类,也就是要被通知的对象,也就是真正的业务逻辑,他可以在毫不知情的情况下,被咱们织入切面。而自己专注于业务本身的逻辑。
7.代理(proxy)
怎么实现整套aop机制的,都是通过代理,这个一会给细说。
8.织入(weaving)
把切面应用到目标对象来创建新的代理对象的过程。有3种方式,spring采用的是运行时,为什么是运行时,后面解释。
三、Aop实现:
1、annotation注解的方式
1》配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 用于注解 -->
<context:annotation-config/>
<!-- 根据指定的文件容器扫描注解 -->
<context:component-scan base-package="com"/>
<!-- 注解自动产生代理 -->
<aop:aspectj-autoproxy/>
</beans>
2》实现
@Aspect
@Component
public class AopInterceptor {
/**
* 定义切入点
* 第一个*表示方法的返回值,这里使用通配符,只有返回值符合条件的才拦截,(!void表示有返回值)
* 第一个..表示com.javaeye.aop包及其子包
* 倒数第二个*表示包下的所有Java类都被拦截
* 最后一个*表示类的所有方法都被拦截
* (..)表示方法的参数可以任意多个如[(java.lang.String,java.lang.Integer)表示第一个参数是String,第二个参数是int的方法才会被拦截]
*/
@Pointcut("execution(* com.javaeye.aop..*.*(..))") //定义一个切入点,名称为pointCutMethod(),拦截类的所有方法
private void pointCutMethod() {
}
@Before("pointCutMethod()") //定义前置通知
public void doBefore() {
System.out.println("前置通知");
}
@AfterReturning("pointCutMethod()") //定义后置通知
public void doAfterReturning() {
System.out.println("后置通知");
}
@AfterThrowing("pointCutMethod()") //定义例外通知
public void doAfterException() {
System.out.println("异常通知");
}
@After("pointCutMethod()") //定义最终通知
public void doAfter() {
System.out.println("最终通知");
}
@Around("pointCutMethod()") //定义环绕通知
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("进入方法");
Object object = pjp.proceed(); //必须执行pjp.proceed()方法,如果不执行此方法,业务bean的方法以及后续通知都不执行
System.out.println("退出方法");
return object;
}
}
2、xml的方式:
参考以下配置
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <aop:aspectj-autoproxy/>
- <bean id="aspjectbean" class="com.royzhou.aop.MyInterceptor" />
- <bean id="UserBean" class="com.royzhou.aop.UserBean"></bean>
- <aop:config>
- <aop:aspect id="asp" ref="aspjectbean">
- <aop:pointcut id="mycut" expression="execution(* com.royzhou.aop..*.*(..))"/>
- <aop:before pointcut="execution(* com.royzhou.aop..*.*(..)) and args(userName)" method="doBefore" />
- <aop:after-returning pointcut-ref="mycut" method="doAfterReturning" returning="result" />
- <aop:after-throwing pointcut-ref="mycut" method="doAfterReturning" throwing="e" />
- <aop:after pointcut-ref="mycut" method="doAfter"/>
- <aop:around pointcut-ref="mycut" method="doArround"/>
- </aop:aspect>
- </aop:config>
- </beans>
6490

被折叠的 条评论
为什么被折叠?



