本文主要通过一些实例,一步步逐渐完善一个AOP的例子:
1.简单的AOP日志实现-某方法之前的前后记录日志;
2.简单的AOP日志实现-需要记录方法的运行时间;
3.简单的AOP日志实现-增加检查订单功能;
以上这些例子的AOP相关的配置均使用XML配置实现,bean的配置均使用Java代码实现,稍后会给出AOP注解配置的实现方案。
蛋糕类:仅仅有name属性和get、set方法。
package spring.ch3.topic1;
/**
* Created by louyuting on 17/1/20.
* 注入属性,记得属性必须要写setter方法 不然就会抛出异常,注入失败.
*/
public class Cake {
private String name = "";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
烤炉类:
package spring.ch3.topic1;
/**
* Created by louyuting on 17/1/21.
*/
public class Oven {
private String name = "";
@Override
public String toString() {
return name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
厨师类:厨师类里面有使用的烤炉类以及做的蛋糕类。厨师类里面只加了一个额外方法,就是制作一个蛋糕的方法。
package spring.ch3.topic1;
/**
* Created by louyuting on 17/1/20.
*/
public class Chief {
private static int index = 0;
public static int getIndex() {
return index;
}
public static void setIndex(int index) {
Chief.index = index;
}
private Cake cake = null;
private Oven oven = null;
private final int id = index++;
private String name = "";
public Cake getCake() {
return cake;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public Oven getOven() {
return oven;
}
public void setCake(Cake cake) {
this.cake = cake;
}
public void setName(String name) {
this.name = name;
}
public void setOven(Oven oven) {
this.oven = oven;
}
public void makeOneCake() {
System.out.println(getName() + " make " + getCake().getName());
}
}
日志类:
package spring.ch3.topic1;
/**
* Created by louyuting on 17/1/24.
*/
public class Log {
public void washOven() {
System.out.println("washOven,logging.....");
}
public void prepare() {
System.out.println("prepare,logging.....");
}
public void after() {
System.out.println("after someting to do,logging.....");
}
}
日志类里面添加了三个方法,头两个是调用方法前执行的,后面一个是调用方法后执行的。
package spring.ch3.topic1;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by louyuting on 17/1/24.
*/
@Configuration
public class SpringBeans {
@Bean
public Chief jack(){
Chief chief = new Chief();
chief.setName("jack");
chief.setOven(oven());
chief.setCake(cake());
return chief;
}
@Bean
public Oven oven() {
Oven oven = new Oven();
oven.setName("big oven");
return oven;
}
@Bean
public Cake cake() {
Cake cake = new Cake();
cake.setName("blueberryCheeseCake");
return cake;
}
@Bean
public Log log() {
return new Log();
}
}
关于切面的配置:(基于XML)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
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">
<context:component-scan
base-package="spring.ch3.topic1" />
<aop:config>
<aop:aspect ref="log">
<aop:pointcut
expression="(execution(* spring.ch3.topic1.Chief.*(..)))"
id="chiefPointCut" />
<aop:before method="washOven" pointcut-ref="chiefPointCut" />
<aop:before method="prepare" pointcut-ref="chiefPointCut" />
<aop:after method="after" pointcut-ref="chiefPointCut" />
aop:aspect>
aop:config>
beans>
重点就是这个AOP的配置文件,解释一下配置文件的东西:
(1)我们使用
标签 定义aop;
(2)然后定义通过
定义切面;
(3)再通过
定义切入点
(4)最后通过
等定义切点的前后方法。
在这里解释一下expression里面的内容 execution(* spring.ch3.topic1.Chief.*(..))
(1)第一颗星表示返回任意类型;
(2)spring.ch3.topic1代表包名;
(3)Chief代表那个类;
(4)*(..)代表任意方法,里面的..表示任何参数
(5)当然我们也可以简单点,全是用*这个东西来标注,不过一般不这样做,因为每一个切点对应的应该是不同的方法。
package spring.ch3.topic1;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Created by louyuting on 17/1/20.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring/ch3/topic1/ApplicationContext-test.xml"})
public class ChiefTest {
@Autowired
private ApplicationContext applicationContext;
@Test
public void testChief(){
Chief jack = (Chief)applicationContext.getBean("jack");
jack.makeOneCake();
}
}
washOven,logging.....
prepare,logging.....
jack make blueberryCheeseCake
after someting to do,logging.....
基于AOP的实例实现了。
上一节我们只是在做蛋糕的前后记录了一下日志,这个不够,我们需要记录做蛋糕需要的时间,这里就需要引入
标签。
下面通过改进上一节的代码来实现,下面我只给出改进的地方的代码:
基本不变,只在Log类中增加一个around方法:
package spring.ch3.topic1;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* Created by louyuting on 17/1/24.
*/
public class Log {
public void washOven() {
System.out.println("washOven,logging.....");
}
public void prepare() {
System.out.println("prepare,logging.....");
}
public void after() {
System.out.println("after someting to do,logging.....");
}
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
washOven();
prepare();
long startTime = System.currentTimeMillis();//记录开始时间
//类似于before执行前
joinPoint.proceed();
//类似于after执行后
long endTime = System.currentTimeMillis();
System.out.println("use time:" + (endTime - startTime));
after();
}
}
由于需要记录做蛋糕的时间,我们这里引入around方法,在arond方法这里,我们需要关注的就是ProceedingJoinPoint ,我们看到里面有一句joinPoint.proceed();
,其实就是代表厨师类里面makeOneCake这个方法的执行。
配置文件里面只有切面AOP的配置改变了:
<aop:config>
<aop:aspect ref="log">
<aop:pointcut
expression="(execution(* spring.ch3.topic1.Chief.*(..)))"
id="chiefPointCut" />
<aop:around method="around" pointcut-ref="chiefPointCut"/>
aop:aspect>
aop:config>
配置文件里面引入了around 的标签,他代表的意义是,围绕着某个方法的执行。
(3)测试类不变:
washOven,logging.....
prepare,logging.....
jack make blueberryCheeseCake
use time:16
after someting to do,logging....
在上一个节的基础上加上一个检查订单功能,也就是获取制作的蛋糕的蛋糕名,
这里有一个需要改变的类是Chief类,因为AOP要获取到执行的方法的参数,从而获取到Cake的name。源码如下:
package spring.ch3.topic1;
/**
* Created by louyuting on 17/1/20.
*/
public class Chief {
private static int index = 0;
public static int getIndex() {
return index;
}
public static void setIndex(int index) {
Chief.index = index;
}
private Cake cake = null;
private Oven oven = null;
private final int id = index++;
private String name = "";
public Chief() {
}
public Cake getCake() {
return cake;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public Oven getOven() {
return oven;
}
public void setCake(Cake cake) {
this.cake = cake;
}
public void setName(String name) {
this.name = name;
}
public void setOven(Oven oven) {
this.oven = oven;
}
public void makeOneCake(Cake cake) {
System.out.println(getName() + " make " + cake.getName());
}
}
上面的代码对比之前两节,唯一的区别就在于在makeOneCake()方法里面加了参数。
日志类:
package spring.ch3.topic1;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* Created by louyuting on 17/1/24.
*/
public class Log {
public void washOven() {
System.out.println("washOven,logging.....");
}
public void prepare() {
System.out.println("prepare,logging.....");
}
public void checkOrder(JoinPoint joinpoint) {
System.out.println("checkOrder");
for (Object item : joinpoint.getArgs()) {
if (item instanceof Cake) {
Cake cake = (Cake) item;
System.out.println("做的蛋糕的名字是:"+cake.getName());
}
}
}
public void after() {
System.out.println("after someting to do,logging.....");
}
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
washOven();
prepare();
long startTime = System.currentTimeMillis();//记录开始时间
//类似于before执行前
joinPoint.proceed();
//类似于after执行后
long endTime = System.currentTimeMillis();
System.out.println("use time:" + (endTime - startTime));
after();
}
}
这里bean的Java配置不变,只需要更改AOP的xml配置:
<context:component-scan
base-package="spring.ch3.topic1" />
<aop:config>
<aop:aspect ref="log">
<aop:pointcut
expression="execution(* spring.ch3.topic1.Chief.*(..))"
id="chiefPointCut"/>
<aop:before method="checkOrder" pointcut-ref="chiefPointCut"/>
aop:aspect>
aop:config>
package spring.ch3.topic1;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Created by louyuting on 17/1/20.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring/ch3/topic1/ApplicationContext-test.xml"})
public class ChiefTest {
@Autowired
private ApplicationContext applicationContext;
@Test
public void testChief(){
Chief jack = (Chief)applicationContext.getBean("jack");
Cake cake = (Cake)applicationContext.getBean("cake");
cake.setName("blueberryCheeseCake");
jack.makeOneCake(cake);
}
}
checkOrder
做的蛋糕的名字是:blueberryCheeseCake
jack make blueberryCheeseCake
从输出可以得知,AOP实现订单检查成功。
总结:上面的AOP,也就是在定义了切点的类中,当被定义切点的类中有的方法执行了的时候,就被被AOP发现,并执行对应的before,around,after 方法。并且类中方法的参数可以通过JoinPoint获得。
本文全部代码可以通过github获得:
github地址:https://github.com/louyuting/nettyRPC/tree/master/Spring/spring/spring/ch3/topic1