SpringAop的准备工作
1、导包 4+2+2+2
2、准备目标对象UserServiceImpl类
public class UserServiceImpl implements UserService {
@Override
public void save() {
// TODO Auto-generated method stub
System.out.println("保存用户");
}
@Override
public void delete() {
// TODO Auto-generated method stub
System.out.println("删除用户");
}
@Override
public void update() {
// TODO Auto-generated method stub
System.out.println("更新用户");
}
@Override
public void find() {
// TODO Auto-generated method stub
System.out.println("查询用户");
}
}
3、准备通知
前置通知 目标方法运行之前调用
后置通知 目标方法运行之后调用 (出现异常后不调用)
环绕通知 目标方法之前和之后都调用
异常拦截通知 如果出现异常就会调用
后置通知 目标方法运行之后调用 (出不出现异常都调用)
//通知类
public class Myadvice {
//前置通知 目标方法运行之前调用
//后置通知 目标方法运行之后调用 (出现异常后不调用)
//环绕通知 目标方法之前和之后都调用
//异常拦截通知 如果出现异常就会调用
//后置通知 目标方法运行之后调用 (出不出现异常都调用)
//前置通知 目标方法运行之前调用
public void before(){
System.out.println("这是前置通知");
}
//后置通知 目标方法运行之后调用 (出现异常后不调用)
public void afterReturning(){
System.out.println("后置通知,异常不调用");
}
public Object around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("这是环绕通知之前的部分");
Object proceed = pjp.proceed();
System.out.println("这是环绕通知之后的部分");
return proceed;
}
//异常拦截通知 如果出现异常就会调用
public void afterException(){
System.out.println("出现异常了");
}
//后置通知 目标方法运行之后调用 (出现异常后不调用)
public void after(){
System.out.println("后置通知,异常也调用");
}
}
4、将通知织入目标对象
4.1导入AOP(约束命名空间)
配置目标对象
配置通知对象
配置将通知织入目标对象
配置切点
配置切面
<?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:p="http://www.springframework.org/schema/p" 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 ">
<!-- 1配置目标对象 -->
<bean name="userService" class="com.itheima.service.UserServiceImpl"></bean>
<!-- 2配置通知 -->
<bean name="myAdvice" class="com.itheima.springaop.Myadvice"></bean>
<!-- 3将通知织入目标对象 -->
<aop:config>
<!-- 配置切入点
public void com.itheima.service.UserServiceImpl.save()
void com.itheima.service.UserServiceImpl.save()
* com.itheima.service.UserServiceImpl.save()
* com.itheima.service.UserServiceImpl.*()
* com.itheima.service.UserServiceImpl.*(..)
* com.itheima.service.*ServiceImpl.*(..)
点点连子包都找
* com.itheima.service..*ServiceImpl.*(..)
-->
<aop:pointcut expression="execution(* com.itheima.service.*ServiceImpl.*(..))" id="pc"/>
<!-- 指定名为before的方法作为前置通知织入到切入点pc -->
<aop:aspect ref="myAdvice">
<!-- 前置通知 -->
<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、测试
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.itheima.domain.User;
import com.itheima.service.UserService;
//帮我们创建容器
@RunWith(SpringJUnit4ClassRunner.class)
//指定创建容器时使用哪个配置文件
@ContextConfiguration("classpath:com/itheima/springaop/applicationContext.xml")
public class TestDemo {
//将名为user的对象注入u中
@Resource(name="userService")
private UserService us;
@Test
public void fun(){
us.save();
}
}
输出结果
AOP面向切面编程思想
横向重复,纵向抽取
动态代理
1通过动态代理可以体现AOP思想
2对象目标中的方法进行增强
SprIng aop开发
Spring封装了动态代理代码。我们就不需要手写动态代理代码
因为封装了cglib代理,所以可以对任何类进行增强