Spring学习笔记8:AOP

一、什么是AOP

AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理
实现程序功能的统一维护的一种技术。

Spring的Aop就是将公共的业务 (日志 , 安全等) 和领域业务结合起来 , 当执行领域业务时 , 
将会把公共业务加进来 . 实现公共业务的重复利用 . 领域业务更纯粹 , 程序猿专注领域业务 , 
其本质还是动态代理 .

AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程
的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的
耦合度降低,提高程序的可重用性,同时提高了开发的效率。

二、AOP在Spring中的作用

提供声明式事务;允许用户自定义切面。

实现AOP的三种方式:

<dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjweaver</artifactId>
   <version>1.9.4</version>
</dependency>

1.通过Spring API 实现

1.接口、实现类

public interface UserService {

   public void add();
//delete、update、search
}
public class UserServiceImpl implements UserService{

   @Override
   public void add() {
       System.out.println("增加用户");
  }
//delete、update、search
}

2.增强类

public class Log implements MethodBeforeAdvice {

   //method : 要执行的目标对象的方法
   //objects : 被调用的方法的参数
   //Object : 目标对象
   @Override
   public void before(Method method, Object[] objects, Object o) throws Throwable {
       System.out.println( o.getClass().getName() + "的" + method.getName() + "方法被执行了");
  }
}
public class AfterLog implements AfterReturningAdvice {
   //returnValue 返回值
   //method被调用的方法
   //args 被调用的方法的对象的参数
   //target 被调用的目标对象
   @Override
   public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
       System.out.println("执行了" + target.getClass().getName()
       +"的"+method.getName()+"方法,"
       +"返回值:"+returnValue);
  }
}

3.注入bean、配置aop

<?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-->
   <bean id="userService" class="com.service.UserServiceImpl"/>
   <bean id="log" class="com.log.Log"/>
   <bean id="afterLog" class="com.log.AfterLog"/>

   <!--aop的配置-->
   <aop:config>
       <!--切入点 expression:表达式匹配要执行的方法-->
       <aop:pointcut id="pointcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
       <!--执行环绕; advice-ref执行方法 . pointcut-ref切入点-->
       <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
       <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
   </aop:config>

</beans>

4.test

public class MyTest {
   @Test
   public void test(){
       ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
       UserService userService = (UserService) context.getBean("userService");
       userService.search();
  }
}

2.自定义类实现AOP

1.切入类

public class DiyPointcut {
   public void before(){System.out.println("---------方法执行前---------");}
   public void after(){System.out.println("---------方法执行后---------");} 
}

2.配置

<!--注册bean-->
<bean id="diy" class="com.config.DiyPointcut"/>

<!--aop的配置-->
<aop:config>
   <!--第二种方式:使用AOP的标签实现-->
   <aop:aspect ref="diy">
       <aop:pointcut id="diyPonitcut" expression="execution(* com.service.UserServiceImpl.*(..))"/>
       <aop:before pointcut-ref="diyPonitcut" method="before"/>
       <aop:after pointcut-ref="diyPonitcut" method="after"/>
   </aop:aspect>
</aop:config>

3.注解实现AOP

1.增强类

@Aspect
public class AnnotationPointcut {
   @Before("execution(* com.service.UserServiceImpl.*(..))")
   public void before(){
       System.out.println("---------方法执行前---------");
  }

   @After("execution(* com.service.UserServiceImpl.*(..))")
   public void after(){
       System.out.println("---------方法执行后---------");
  }

   @Around("execution(* com.service.UserServiceImpl.*(..))")
   public void around(ProceedingJoinPoint jp) throws Throwable {
       System.out.println("环绕前");
       System.out.println("签名:"+jp.getSignature());
       //执行目标方法proceed
       Object proceed = jp.proceed();
       System.out.println("环绕后");
       System.out.println(proceed);
  }
}

2.配置

<bean id="annotationPointcut" class="com.kuang.config.AnnotationPointcut"/>
<aop:aspectj-autoproxy/>

三、名词了解

1.通知(Advice):就是会在目标方法执行前后执行的方法

@Before("execution(* com.service.UserServiceImpl.*(..))")
   public void before(){
       System.out.println("---------方法执行前---------");
  }

这个方法就是通知,目标方法是UserDao类的UserServiceImpl()。

通过通知和目标方法的执行顺序我们可以把通知分为五种:

前置通知(before):在目标方法执行之前执行。

后置通知(after):在目标方法执行之后执行。

后置返回通知(after returning):在目标方法返回之后执行,先执行后置通知再执行后置返回通知。

这三种通知的执行顺序如下:

try{
    try{
        //@Before
        method.invoke(..);
    }finally{
        //@After
    }
    //@AfterReturning
}catch(){
    //@AfterThrowing
}

异常通知(after throwing):在目标方法抛出异常时执行。

环绕通知(around):在目标函数执行中执行。

2.切入点(PointCut)应用通知进行增强的目标方法

@Before("execution(* com.service.UserServiceImpl.*(..))")

"execution(* com.service.UserServiceImpl.*(..))",就是用来描述需要应用通知的方法的。这里的含义是com.service包UserServiceImpl类中的参数任意,返回值任意的*方法。

3.连接点(JointPoint):连接点就是可以应用通知进行增强的方法

因为Spring Aop只能针对方法进行增强,所以这里的连接点指的就是方法,一旦连接点被增强,它就成为了切入点。

   public void before(){
       System.out.println("---------方法执行前---------");
  }
   public void after(){
       System.out.println("---------方法执行后---------");
  }

4.切面(ASPECT):是切入点和通知的结合,它是一个类。

@Aspect
public class AnnotationPointcut {

}

5.织入:就是通过动态代理对目标对象方法进行增强的过程

6.代理(Proxy):向目标对象应用通知之后创建的对象。

7.目标(Target):被通知对象。

8.横切关注点:跨越应用程序多个模块的方法或功能。如日志 , 安全 , 缓存 , 事务等等 ....

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值