1、导包(4个spring包+2个aop包)
(1)spring需要第三方aop包
(2)spring的aop包
2、准备目标对象
package cn.itcast.service;
public class UserServiceImpl implements UserService {
@Override
public void save() {
System.out.println("保持用户");
}
@Override
public void delete() {
System.out.println("删除用户");
}
@Override
public void update() {
System.out.println("更新用户");
}
@Override
public void select() {
System.out.println("查找用户");
}
}
3、准备通知
(1)前置通知: ----------->目标方法运行之前调用
(2)后置通知(如果出现异常,将不会调用): ----------->在目标方法运行之后调用
(3)环绕通知: ----------->在目标方法之前和之后度调用
(4)异常拦截通知: ----------->如果出现异常都会调用
(5)后置通知(无论是否出现异常都会调用): ----------->在目标方法运行之后调用
package cn.itcast.e_annotationaop;
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;
import org.aspectj.lang.annotation.Pointcut;
//作为通知类
@Aspect //不是该类是一个通知类
public class MyAdvice {
@Pointcut("execution(* cn.itcast.service.*ServiceImpl.*(..))")
private void pc(){}
//前置通知
//指定该方法是前置通知,并指定切入点
@Before("MyAdvice.pc()")
public void before(){
System.out.println("这是前置通知!!!!!");
}
//后置通知
@AfterReturning("MyAdvice.pc()")
public void afterReturning(){
System.out.println("这是后置通知!(如果出现异常,将不会调用)!!!!");
}
//环绕通知
@Around("MyAdvice.pc()")
public Object around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("这是环绕通知之前部分!!!!!");
Object proceed = pjp.proceed(); //调用目标方法
System.out.println("这是前置通知之后部分!!!!!");
return proceed;
}
//异常通知
@AfterThrowing("MyAdvice.pc()")
public void afterException(){
System.out.println("出事了,抛异常了!!!!");
}
//后置通知
@After("MyAdvice.pc()")
public void after(){
System.out.println("这是后置通知!(无论是否出现异常都会调用)!!!!");
}
}
4、配置进入织入,将通知织入目标对象中,在applicationContext.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
<!-- 准备工作:导入AOP(约束)命名空间 -->
<!-- 1、配置目标对象 -->
<bean name="userService" class="cn.itcast.service.UserServiceImpl"></bean>
<!-- 2、配置通知对象 -->
<bean name="myAdvice" class="cn.itcast.d_springaop.MyAdvice"></bean>
<!-- 3、配置对通知织入目标对象 -->
<aop:config>
<!-- 配置切入点
public void cn.itcast.service.UserServiceImpl.save()
void cn.itcast.service.UserServiceImpl.save()
* cn.itcast.service.UserServiceImpl.save()
* cn.itcast.service.UserServiceImpl.*()
* cn.itcast.service.UserServiceImpl.*(..)
最终形态
* cn.itcast.service.*ServiceImpl.*(..)
* cn.itcast.service..*ServiceImpl.*(..)
-->
<aop:pointcut expression="execution(* cn.itcast.service.*ServiceImpl.*(..))" id="pc"/>
<aop:aspect ref="myAdvice">
<!-- 指定名为before方法作为前置通知 -->
<aop:before method="before" pointcut-ref="pc"/>
<!-- 后置通知(如果出现异常,将不会调用) -->
<aop:after-returning method="afterReturning" pointcut-ref="pc"/>
<!-- 环绕通知 -->
<aop:around method="around" pointcut-ref="pc"/>
<!-- 异常拦截通知 -->
<aop:after-throwing method="afterException" pointcut-ref="pc"/>
<!-- 后置通知(无论是否出现异常都会调用)-->
<aop:after method="after" pointcut-ref="pc"/>
</aop:aspect>
</aop:config>
</beans>
5、测试类
package cn.itcast.d_springaop;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.itcast.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class) //帮我们创建容器
@ContextConfiguration("classpath:cn/itcast/d_springaop/applicationContext.xml") //制定创建容器时使用哪个配置文件
public class Demo {
@Resource(name="userService") //将名为user的对象注入到u变量中
private UserService us;
@Test
public void fun1() {
us.save();
}
}
6、测试结果
这是前置通知!!!!!
这是环绕通知之前部分!!!!!
保持用户
这是后置通知!(无论是否出现异常都会调用)!!!!
这是前置通知之后部分!!!!!
这是后置通知!(如果出现异常,将不会调用)!!!!