对于AOP的这种编程思想,有不少框架或者组件进行了实现,spring实现AOP就是其中的一种。java
AspectJ也实现了AOP,并且实现方式更为简单,使用起来更为方便,因此spring将AspectJ对于AOP的实现引入了本身的框架。spring
AspectJ是一个面向切面的框架,它定义了AOP的一些语法,有一个专门的字节码生成器来生成遵照java规范的class文件。express
AspectJ的通知类型:编程
切入点表达式做用:标识切面织入到哪些类的哪些方法当中。app
public boolean com.steven.spring.service.impl.StudentService.addStudent(Student student) execution( modifiers-pattern? //访问权限匹配 如public、protected 问号表明能够省略 ret-type-pattern //返回值类型匹配 declaring-type-pattern? //全限定性类名 name-pattern(param-pattern) //方法名(参数名) throws-pattern? //抛出异常类型 )
注意:中间以空格隔开,有问号的属性能够省略框架
a: * 表明0到多个任意字符 b: .. 放在方法参数中 ,表明任意个参数 ,放在包名后面表示当前包及其全部子包路径 c: + 放在类名后,表示当前类及其子类,放在接口后,表示当前接口及其实现类
a:execution(public * *(..)) 表示任意的public方法 b:execution(* set*(..)) 表示任意包含以set字符开头的方法 c:execution(* com.steven.spring.service.impl.*.*(..)) 表示com.steven.spring.service.impl的任意类的任意方法 d:execution(* com.steven.spring.service..*.*(..)) 表示com.steven.spring.service包下面的全部方法以及全部子包下面的全部方法 e:execution(* com.steven.spring.service.IStudentService+.*(..)) f:execution(* add(String,int)) 带包任意返回类型的add方法 有两个参数,类型分别为String,int g:execution(* add(String,*))
引入jar包jsp
经测试,能够不用另外加上spring-aspects-4.2.1.RELEASE.jar。但aspectjweaver必定不能少。ide
引入aop的约束测试
实现步骤:this
实例:
1)实体类
package com.steven.spring.sysmanage.entity; /** * 学生实体类 * @author Administrator * */ public class Student implements java.io.Serializable{ private static final long serialVersionUID = -3875695558042397898L; private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
2)服务类接口
package com.steven.spring.sysmanage.service; import java.util.List; import com.steven.spring.sysmanage.entity.Student; /** * 用于对外提供学生服务类的增删改查接口 * @author Administrator * */ public interface IStudentService { public boolean addStudent(Student student); public boolean delStudent(Integer studentId); public boolean updateStudent(Student student); public List<Student> getStudentList(); }
3)服务类
package com.steven.spring.sysmanage.service.impl; import java.util.ArrayList; import java.util.List; import com.steven.spring.sysmanage.entity.Student; import com.steven.spring.sysmanage.service.IStudentService; /** * 用于对外提供学生服务类的增删改查实现 * @author Administrator * */ public class StudentService implements IStudentService{ @Override public boolean addStudent(Student student) { //System.out.println("进行权限验证"); System.out.println("执行增长功能"); //System.out.println("进行日志记录"); return true; } @Override public boolean delStudent(Integer studentId) { //System.out.println("进行权限验证"); int i= 1/0; System.out.println("执行删除功能"); //System.out.println("进行日志记录"); return true; } @Override public boolean updateStudent(Student student) { //System.out.println("进行权限验证"); System.out.println("执行修改功能"); //System.out.println("进行日志记录"); return true; } @Override public List<Student> getStudentList() { //System.out.println("进行权限验证"); System.out.println("执行查询功能"); //System.out.println("进行日志记录"); return new ArrayList<Student>(); } }
4)定义通知
package com.steven.spring.sysmanage.aspect; import org.aspectj.lang.ProceedingJoinPoint; /** * 基于xml配置方式实现aspectJ 定义通知 * @author chenyang * */ public class MyAspect { //定义前置通知 public void beforeAdvice(){ System.out.println("这是一个前置通知,应该在目标方法以前打印出来"); } //定义后置通知 public void afterAdvice(){ System.out.println("这是一个后置通知,应该在目标方法以后打印出来"); } //定义后置通知,包含返回值 public void afterAdvice(Object result){ System.out.println("这是一个后置通知,应该在目标方法以后打印出来;目标方法的返回值为:" + result.toString()); } //定义环绕通知 public void aroundAdvice(ProceedingJoinPoint pjp) throws Throwable{ System.out.println("这是一个环绕通知,应该在目标方法以前打印出来"); Object result = pjp.proceed(); System.out.println("这是一个环绕通知,应该在目标方法以后打印出来"); } //定义异常通知 public void afterThrowingAdvice(){ System.out.println("这是一个异常通知,应该在目标方法出现异常以后打印出来"); } //定义异常通知,包含返回值 public void afterThrowingAdvice(Exception ex){ System.out.println("这是一个异常通知,应该在目标方法出现异常以后打印出来,异常为:" + ex); } //定义最终通知 public void lastAdvice(){ System.out.println("这是一个最终通知,不管如何都会执行"); } }
5)配置文件
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 注册目标类 --> <bean id="studentService" class="com.steven.spring.sysmanage.service.impl.StudentService"></bean> <!-- 注册切面(通知) --> <bean id="myAspect" class="com.steven.spring.sysmanage.aspect.MyAspect"></bean> <!-- AspectJ的aop配置 --> <aop:config> <aop:pointcut expression="execution(* add*(..))" id="beforePointCut"/> <aop:pointcut expression="execution(* update*(..))" id="afterPointCut"/> <aop:pointcut expression="execution(* get*(..))" id="aroundPointCut"/> <aop:pointcut expression="execution(* del*(..))" id="afterThrowingPointCut"/> <aop:pointcut expression="execution(* del*(..))" id="lastPointCut"/> <aop:aspect ref="myAspect"> <aop:before method="beforeAdvice" pointcut-ref="beforePointCut"/> <aop:after-returning method="afterAdvice" pointcut-ref="afterPointCut" /> <!-- 方法参数必须是全路径类名 返回值参数名必须与方法中参数名一致 --> <aop:after-returning method="afterAdvice(java.lang.Object)" pointcut-ref="afterPointCut" returning="result" /> <aop:around method="aroundAdvice" pointcut-ref="aroundPointCut"/> <aop:after-throwing method="afterThrowingAdvice" pointcut-ref="afterThrowingPointCut"/> <aop:after-throwing method="afterThrowingAdvice(java.lang.Exception)" pointcut-ref="afterThrowingPointCut" throwing="ex" /> <aop:after method="lastAdvice" pointcut-ref="lastPointCut"/> </aop:aspect> </aop:config> </beans>
6)测试
package com.steven.spring.sysmanage.test; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.steven.spring.sysmanage.entity.Student; import com.steven.spring.sysmanage.service.IStudentService; public class AspectXmlTest { private ApplicationContext ac = null; private IStudentService studentService = null; @Before public void init(){ ac = new ClassPathXmlApplicationContext("applicationContext.xml"); studentService = (IStudentService) ac.getBean("studentService"); } //测试前置通知 @Test public void testBeforeAdvice(){ studentService.addStudent(new Student()); } //测试后置通知 @Test public void testAfterAdvice(){ studentService.updateStudent(new Student()); } //测试环绕通知 @Test public void testAroundAdvice(){ studentService.getStudentList(); } //测试异常通知 @Test public void testAfterThrowingAdvice(){ studentService.delStudent(1); } //测试最终通知 @Test public void testLastAdvice(){ studentService.delStudent(1); } }
注解方式有必定的侵入性(在代码中加入本来不属于代码的部分)。
实现步骤:
实例:
在6.4.1实例的基础上更改通知类和配置文件,最后在测试类中更改配置文件名称便可。
1)通知类
package com.steven.spring.sysmanage.aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; /** * 基于xml配置方式来实现AspectJ 定义通知 * @author Administrator * */ @Aspect public class MyAspectAnnotation { //定义前置通知 @Before(value="execution(* add*(..))") public void beforeAdvice(){ System.out.println("这是一个前置通知,应该在目标方法以前打印出来"); } //定义后置通知 @AfterReturning(value="execution(* update*(..))") public void afterAdvice(){ System.out.println("这是一个后置通知,应该在目标方法以后打印出来"); } //定义后置通知包含返回值 @AfterReturning(value="execution(* update*(..))",returning="result") public void afterAdvice(Object result){ System.out.println("这是一个后置通知,应该在目标方法以后打印出来"); System.out.println("后置通知获得目标方法的返回值="+result.toString()); } //定义环绕通知 @Around(value="execution(* get*(..))") public void aroundAdvice(ProceedingJoinPoint pjp) throws Throwable{ System.out.println("这是一个环绕通知,应该在目标方法以前打印出来"); Object result = pjp.proceed(); System.out.println("这是一个环绕通知,应该在目标方法以后打印出来"); } //定义异常通知 @AfterThrowing(value="execution(* del*(..))") public void afterThrowingAdvice(){ System.out.println("这是一个异常通知,应该在目标方法出现异常时候打印出来"); } //定义异常通知 @AfterThrowing(value="execution(* del*(..))",throwing="ex") public void afterThrowingAdvice(Exception ex){ System.out.println("这是一个异常通知,应该在目标方法出现异常时候打印出来 ex="+ex); } //定义最终通知 finally @After(value="execution(* del*(..))") public void lastAdvice(){ System.out.println("这是一个最终通知,不管如何会执行 "); } }
2)配置文件
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--注册studentService --> <bean id = "studentService" class = "com.steven.spring.sysmanage.service.impl.StudentService"> </bean> <!--注册切面 --> <bean id= "myAspect" class = "stevenm.tz.spring.sysmanage.aspect.MyAspectAnnotation"></bean> <aop:aspectj-autoproxy/> </beans>