Spring (五) AOP第二章使用方式

来源:互联网 发布:nginx 411 编辑:程序博客网 时间:2024/06/07 00:16

前面我们讲到了AOP的使用方法,其实AOP使用方式有三种,今天我们来讲一讲第二种方法

前面第一种我们使用了implement Spring 里的通知类来实现。今天的我们通过spring配置文件beans.xml来实现

首先。我们的业务类(真实角色的代码不用变),我们相爱log里面的方法


package cn.sxt.log;public class Log {public void before(){System.out.println("-----方法执行前-----");}public void after(){System.out.println("-----方法执行后-----");}}

发现我们不用实现什么接口,直接是两个方法就可以了,然后配置beans.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 id="userService" class="cn.sxt.service.impl.UserServiceImpl"/><bean id="log" class="cn.sxt.log.Log"/><aop:config>  <aop:aspect ref="log">  <aop:pointcut expression="execution(* cn.sxt.service.impl.*.*(..))" id="pointcut"/>  <aop:before method="before" pointcut-ref="pointcut"/>  <aop:after method="after" pointcut-ref="pointcut"/>  </aop:aspect>  </aop:config>  </beans>

来解析一下  :首先<aop:config>标签,这个没什么好说的。


<as:aspect ref="log">这个标签就是你要引用的通知类。在上面我们已经把Log类配合好了。

<aop:pointcut>这个标签就是使用的范围了   id为这个pointcut 引用

<aop:before>标签就是配置执行前的痛知。method  里面的参数表示Log类里的before方法。 在后面引用id为pointcut 的aop:pointcut   意思就是说  在执行我的业务方法是。在之前切入Log类的before方法。

<aop:after> 就是指执行后的切入了

是不是很简单? 下面我们来测试一下:


public class Test {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");UserService userService = (UserService)ac.getBean("userService");userService.delete();}}

结果肯定是如我们所愿的切入了!


下面介绍AOP使用的第三种方式了。

使用注解方法:

直接修改Log类

package cn.sxt.log;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;@Aspectpublic class Log {@Before("execution(* cn.sxt.service.impl.*.*(..))")public void before(){System.out.println("-----方法执行前-----");}@After("execution(* cn.sxt.service.impl.*.*(..))")public void after(){System.out.println("-----方法执行后-----");}@Around("execution(* cn.sxt.service.impl.*.*(..))")public Object aroud(ProceedingJoinPoint jp) throws Throwable{System.out.println("环绕前");System.out.println("签名:"+jp.getSignature());//执行目标方法 Object result = jp.proceed();System.out.println("环绕后");return result;}}

解析一下上面的代码:

在类的上面我们使用了一个@Aspect 注解,表示该类为通知类

在方法上注解:

我们再before方法上使用了一个@Before ("execution=(* cn.sxt.service.impl.*.*(..)") 执行前的通知

意思是说,该方法允许 该包下(cn.sxt.service.impl.)的所有类,所有方法 执行前都切入before方法

@After 跟@Before差不多,我就不讲了。

在这里我要讲一下这个@Around 环绕通知。发现这个方法传入了一个参数。我们调用了jp.proceed();意思是我们在这个方法的前后可以执行我们的切入方法。在中间调用jp.proeed()方法,该方法执行就相当于执行了我们真实角色的方法了。这样就实现了环绕、

其实AOP并不是很难理解的。

AOP我就介绍到这些,我要想想下次讲给大家带来什么样的知识呢。

0 0