主要内容:
AspectJ简介:
环境准备:
step1:引入依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 引入Spring的基本开发包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- AOP-->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- 引入aspectj-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.4.RELEASE </version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.4.RELEASE </version>
</dependency>
</dependencies>
step2:配置文件
<?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" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
<!-- 开启AspectJ的注解开发,自动代理-->
<aop:aspectj-autoproxy/>
</beans>
通知的类型介绍:
切入点表达式的定义:
案例:
类:
package com.imooc.aspectJ.demo1;
public class ProductDao {
public void save() {
System.out.println("保存商品...");
}
public void findOne() {
System.out.println("查找单一商品...");
}
public void findAll() {
System.out.println("查找所有商品...");
}
public void update() {
System.out.println("修改商品...");
}
public void delete() {
System.out.println("删除商品...");
}
}
增强实现类:
package com.imooc.aspectJ.demo1;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
/**
* 切面类
*/
@Aspect
public class MyAspectAnno {
//对指定类中所有方法增强
//@Before(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.*(..))")
//对指定类中指定方法增强
@Before(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.findOne(..))")
public void before(){
System.out.println("前置通知========");
}
}
配置文件:
<!-- 开启AspectJ的注解开发,自动代理-->
<aop:aspectj-autoproxy/>
<!-- 目标类-->
<bean id="productDao" class="com.imooc.aspectJ.demo1.ProductDao"></bean>
<!-- 切面类-->
<bean class="com.imooc.aspectJ.demo1.MyAspectAnno"></bean>
测试代码:
package com.imooc.aspectJ.demo1;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDemo1 {
@Resource(name = "productDao")
private ProductDao productDao;
@Test
public void demo1(){
productDao.save();
productDao.findOne();
productDao.findAll();
productDao.update();
productDao.delete();
}
}
输出结果:只对特定类的特定方法进行增强
保存商品...
前置通知========
查找单一商品...
查找所有商品...
修改商品...
删除商品...
@Before前置通知:
//对指定类中指定方法增强
@Before(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.findOne(..))")
public void before(JoinPoint joinPoint){
System.out.println("前置通知========"+joinPoint);
}
测试代码:
@Test
public void demo1(){
productDao.save();
productDao.findOne();
productDao.findAll();
productDao.update();
productDao.delete();
}
输出结果:会输出切入点的信息
保存商品...
前置通知========execution(void com.imooc.aspectJ.demo1.ProductDao.findOne())
查找单一商品...
查找所有商品...
修改商品...
删除商品...
@AfterReturing 后置通知:
对于有返回值的方法:
public String update() {
System.out.println("修改商品...");
return "hello";
}
使用后置注解:
@AfterReturning(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.update(..))",returning = "result")
public void afterReturing(Object result){
System.out.println("后置通知========"+result);
}
测试代码:
@Test
public void demo1(){
productDao.update();
productDao.delete();
}
输出结果:加上了返回值
修改商品...
后置通知========hello
删除商品...
@Around 环绕通知:
//环绕通知
@Around(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.delete(..))")
public Object around (ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕前通知========");
Object obj = joinPoint.proceed();//执行目标方法
System.out.println("环绕后通知========");
return obj;
}
测试代码:
@Test
public void demo1(){
productDao.delete();
}
输出结果:
环绕前通知========
删除商品...
环绕后通知========
@AfterThrowing 异常抛出通知:
在方法里设置异常:
public void findAll() {
System.out.println("查找所有商品...");
int i=1/0;
}
异常抛出通知:
//异常抛出通知
@AfterThrowing(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.findAll(..))",throwing = "e")
public void afterThrowing (Throwable e) {
System.out.println("异常抛出通知========"+e.getMessage());
}
测试代码:
@Test
public void demo1(){
productDao.findAll();
}
输出结果:
查找所有商品...
异常抛出通知========/ by zero
@After 最终通知:
//最终通知
@After(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.findOne(..))")
public void after () {
System.out.println("最终通知========");
}
测试代码:
@Test
public void demo1(){
productDao.findOne();
}
输出结果:
查找单一商品...
最终通知========
@Pointcut 切点命名:
package com.imooc.aspectJ.demo1;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
/**
* 切面类
*/
@Aspect
public class MyAspectAnno {
//对指定类中所有方法增强
//@Before(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.*(..))")
//前置通知
//对指定类中指定方法增强
@Before(value = "myPointcut1()")
public void before(JoinPoint joinPoint){
System.out.println("前置通知========"+joinPoint);
}
//后置通知
@AfterReturning(value = "myPointcut2()",returning = "result")
public void afterReturing(Object result){
System.out.println("后置通知========"+result);
}
//环绕通知
@Around(value = "myPointcut3()")
public Object around (ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕前通知========");
Object obj = joinPoint.proceed();//执行目标方法
System.out.println("环绕后通知========");
return obj;
}
//异常抛出通知
@AfterThrowing(value = "myPointcut4()",throwing = "e")
public void afterThrowing (Throwable e) {
System.out.println("异常抛出通知========"+e.getMessage());
}
//最终通知
@After(value = "myPointcut5()")
public void after () {
System.out.println("最终通知========");
}
//切点命名
@Pointcut(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.save(..))")
private void myPointcut1(){}
@Pointcut(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.update(..))")
private void myPointcut2(){}
@Pointcut(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.delete(..))")
private void myPointcut3(){}
@Pointcut(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.findAll(..))")
private void myPointcut4(){}
@Pointcut(value = "execution(* com.imooc.aspectJ.demo1.ProductDao.findOne(..))")
private void myPointcut5(){}
}
测试代码:
@Test
public void demo1(){
productDao.save();
productDao.findAll();
productDao.update();
productDao.delete();
productDao.findOne();
}
输出结果:
前置通知========execution(void com.imooc.aspectJ.demo1.ProductDao.save())
保存商品...
查找所有商品...
修改商品...
后置通知========hello
环绕前通知========
删除商品...
环绕后通知========
查找单一商品...
最终通知========
AspectJ的XML方式开发AOP:
配置前置通知:
package com.imooc.aspectJ.demo2;
public class MyAspectXml {
//前置通知
public void before(){
System.out.println("XML配置方式的前置通知......");
}
}
配置XML:
<!--XML的配置方式完成AOP的开发-->
<!-- 配置目标类-->
<bean id="customerDao" class="com.imooc.aspectJ.demo2.CustomerDaoImpl"/>
<!--配置切面类-->
<bean id="myAspectXml" class="com.imooc.aspectJ.demo2.MyAspectXml"></bean>
<!-- aop的相关配置-->
<aop:config>
<!-- 配置切入点-->
<aop:pointcut id="pointcut1" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.save(..))"/>
<!-- 配置aop的切面-->
<aop:aspect ref="myAspectXml">
<!-- 配置前置增强-->
<aop:before method="before" pointcut-ref="pointcut1"></aop:before>
</aop:aspect>
</aop:config>
测试代码:
package com.imooc.aspectJ.demo2;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value="classpath:applicationContext2.xml")
public class SpringDemo2 {
@Resource(name="customerDao")
private CustomerDao customerDao;
@Test
public void demo1(){
customerDao.save();
customerDao.update();
customerDao.delete();
customerDao.findOne();
customerDao.findAll();
}
}
输出结果:
XML配置方式的前置通知......
保存客户...
修改客户...
删除客户...
查询单个客户...
各种通知类型的配置:
package com.imooc.aspectJ.demo2;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyAspectXml {
//前置通知
public void before(JoinPoint joinPoint){
System.out.println("XML配置方式的前置通知......"+joinPoint);
}
//后置通知
public void afterReturing(Object result){
System.out.println("XML配置方式的后置通知......"+result);
}
//环绕通知
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("XML配置方式的环绕前通知......");
Object obj = joinPoint.proceed();
System.out.println("XML配置方式的环绕后通知......");
return obj;
}
//异常抛出通知
public void afterThrowing(Throwable e){
System.out.println("XML配置方式的异常抛出通知......"+e.getMessage());
}
//最终通知
public void after(){
System.out.println("XML配置方式的最终通知......");
}
}
配置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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
<!--XML的配置方式完成AOP的开发-->
<!-- 配置目标类-->
<bean id="customerDao" class="com.imooc.aspectJ.demo2.CustomerDaoImpl"/>
<!--配置切面类-->
<bean id="myAspectXml" class="com.imooc.aspectJ.demo2.MyAspectXml"></bean>
<!-- aop的相关配置-->
<aop:config>
<!-- 配置切入点-->
<aop:pointcut id="pointcut1" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.save(..))"/>
<aop:pointcut id="pointcut2" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.update(..))"/>
<aop:pointcut id="pointcut3" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.delete(..))"/>
<aop:pointcut id="pointcut4" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.findOne(..))"/>
<aop:pointcut id="pointcut5" expression="execution(* com.imooc.aspectJ.demo2.CustomerDao.findAll(..))"/>
<!-- 配置aop的切面-->
<aop:aspect ref="myAspectXml">
<!-- 配置前置增强-->
<aop:before method="before" pointcut-ref="pointcut1"></aop:before>
<!-- 配置后置增强-->
<aop:after-returning method="afterReturing" pointcut-ref="pointcut2"
returning="result"></aop:after-returning>
<!-- 配置环绕通知-->
<aop:around method="around" pointcut-ref="pointcut3"></aop:around>
<!-- 配置异常抛出通知-->
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut4" throwing="e"/>
<!-- 配置最终通知-->
<aop:after method="after" pointcut-ref="pointcut5"></aop:after>
</aop:aspect>
</aop:config>
</beans>
输出结果:
XML配置方式的前置通知......execution(void com.imooc.aspectJ.demo2.CustomerDao.save())
保存客户...
修改客户...
XML配置方式的后置通知......hello
XML配置方式的环绕前通知......
删除客户...
XML配置方式的环绕后通知......
XML配置方式的异常抛出通知....../ by zero