Aop全称是Aspect oriented programming,即面向切面编程。
Spring中实现的Aop,主要利用了Java中的AspectJ框架(一种专门进行Aop编程的框架)实现的Aop
Spring实现Aop有两种方式,一种是注解的方式,另一种是利用xml配置的方式(使用更普遍)。
下面讲解@annotation实现的Aop编程,并给出几个例子
项目的源码:http://pan.baidu.com/s/1eS16hlK
项目结构:
Spring中Aop一些重要的概念,请自行参考文档
a.JoinPoint
b.PointCut
c.Aspect
d.Advice
e.Target
f.Weave
1.首先,要使Spring支持Aop注解开发的方式,要配置xml文件:
(1)配置xsd的命名空间
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
(2) 声明注解方式
<aop:aspectj-autoproxy/>
常用的注解
@PointCut 定义切点,即切入到那个包的那个类方法中
@Before 在执行被切入的方法之前执行
@AfterReturning 方法正常执行之后执行
@AfterThrowing 方法抛出异常之后执行
@After 相当于finally,方法执行之后总是被执行
@Around 在方法之前执行一次,方法之后执行剩余的逻辑
AspectJ表达式中常用的适配符
* 可以代表任意类型的一个参数,函数名,返回值类型等等
(..) 表示任意个参数
.. 表示文件目录中的任意深度
.
2.利用注解开发Aop切面
示例
package com.szh.interceptor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogInterceptor {
@Pointcut("execution(public * com.szh.dao..*.*(..))")
public void myMethod(){};
@Before("myMethod()")
public void before() {
System.out.println("method before");
}
@Around("myMethod()")
public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("method around start");
pjp.proceed();
System.out.println("method around end");
}
}
代码解读
@Pointcut("execution(public * com.szh.dao..*.*(..))")
public void myMethod(){};
定义了一个切点,切点的位置是com.szh.dao下的所有类的所有方法,参数为任意类型
任意返回值类型
@Around("myMethod()")
引用之前定义的切点
完整的代码:
beans.xml Spring配置
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.szh"></context:component-scan>
<aop:aspectj-autoproxy/></beans>
aop切入类
package com.szh.interceptor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogInterceptor {
@Pointcut("execution(public * com.szh.dao..*.*(..))")
public void myMethod(){};
@Before("myMethod()")
public void before() {
System.out.println("method before");
}
@Around("myMethod()")
public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("method around start");
pjp.proceed();
System.out.println("method around end");
}
}
并利用Junit4进行了测试
package test.szh.service;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.szh.bean.User;
import com.szh.service.UserService;
public class UserServiceTest {
private ClassPathXmlApplicationContext applicationContext;;
@Before
public void setUp() throws Exception {
this.applicationContext = new ClassPathXmlApplicationContext("beans.xml");
}
@Test
public void testAddUser() {
UserService test1 = (UserService)this.applicationContext.getBean("userService");
User u = new User();
test1.addUser(u);
UserService test2 = (UserService)this.applicationContext.getBean("userService");
System.out.println(test1==test2 ? "singleton":"prototype");
}
@After
public void destroy(){
this.applicationContext.destroy();
}
}
测试结果