一、AOP模块的认识
AOP:在我理解来,AOP就是在不增加代码耦合性的基础上,将你所要插入的代码插到合适的位置(前,后或者前后都可以)。
而其中有三个重要概念:advice,pointcut和advisor
我通俗的把:advice:所需插入的代码,pointcut:代码插入的位置,advisor:执行插入的人
二、AOP三种配置方式
1.aop配置的传统方式(便于理解背后机制-不常用,接下来会有介绍)
2.使用<aop>标签,基于xml配置,比较简洁常用
3.使用@AspectJ的Annotation支持,必须JDK5.0以上支持,注释形式。暂不介绍。
下面是摘自林信良的技术手册:
ps:主要介绍1,2两种
三、AOP配置详细
第一种(容易理解,但不实用):
1.业务接口
package com.techbirds.spring.aop_one;
public interface ISay {
public void good();
}
2.实现接口
package com.techbirds.spring.aop_one;
import java.util.logging.Logger;
public class SayImpl implements ISay {
private Logger logger=Logger.getLogger(this.getClass().getName());
@Override
public void good() {
logger.info("say good..");
}
}
3.advice实现
package com.techbirds.spring.aop_one;
import java.lang.reflect.Method;
import java.util.logging.Logger;
import org.springframework.aop.AfterReturningAdvice;
public class LogAfterAdvice implements AfterReturningAdvice {
/**
* JDK自带
*/
private Logger logger=Logger.getLogger(this.getClass().getName());
@Override
public void afterReturning(Object obj, Method method, Object[] args,
Object target) throws Throwable {
logger.info("do ...after the "+method.getName());
}
}
4.xml配置:
<bean id="logBeforeAdvice" class="com.techbirds.spring.aop_one.LogBeforeAdvice"></bean>
<bean id="logAfterAdvice" class="com.techbirds.spring.aop_one.LogAfterAdvice"></bean>
<bean id="sayImpl" class="com.techbirds.spring.aop_one.SayImpl"></bean>
<bean id="sayProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="com.techbirds.spring.aop_one.ISay" />
<property name="target" ref="sayImpl" />
<property name="interceptorNames">
<list>
<value>logBeforeAdvice</value>
<value>logAfterAdvice</value>
</list>
</property>
</bean>
5.测试:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopTest {
public static void main(String[] args) {
// ApplicationContext ctx
// = new FileSystemXmlApplicationContext("resources/aop.xml");
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/resources/aop.xml");
// 得到代理对象
ISay say = (ISay) ctx.getBean("sayProxy");
// 执行方法
say.good();
}
}
6.执行结果:
2013-6-6 14:37:57 com.techbirds.spring.aop_one.LogBeforeAdvice before
信息: do ...before the good
2013-6-6 14:37:57 com.techbirds.spring.aop_one.SayImpl good
信息: say good..
2013-6-6 14:37:57 com.techbirds.spring.aop_one.LogAfterAdvice afterReturning
信息: do ...after the good
信息: do ...before the good
2013-6-6 14:37:57 com.techbirds.spring.aop_one.SayImpl good
信息: say good..
2013-6-6 14:37:57 com.techbirds.spring.aop_one.LogAfterAdvice afterReturning
信息: do ...after the good
第二种(容易配置,比较实用):
1.业务接口
package com.techbirds.spring.aop_two;
public interface ICar {
public void go();
}
2.接口实现
package com.techbirds.spring.aop_two;
import java.util.logging.Logger;
public class Bus implements ICar {
private Logger logger=Logger.getLogger(this.getClass().getName());
@Override
public void go() {
logger.info("the bus go...");
}
}
3.advice实现
package com.techbirds.spring.aop_two;
import java.util.logging.Logger;
import org.aspectj.lang.JoinPoint;
public class LogBeforeCar {
private Logger logger=Logger.getLogger(this.getClass().getName());
public void beforeCar(JoinPoint jp){
logger.info("do ... before the car "+jp.getSignature().getName());
}
}
第一种方式的基础上加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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd ">
<?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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
详细配置
5.测试:
<bean id="bus" class="com.techbirds.spring.aop_two.Bus"></bean>
<bean id="logBeforeCar" class="com.techbirds.spring.aop_two.LogBeforeCar"></bean>
<aop:config>
<aop:aspect ref="logBeforeCar">
<aop:before method="beforeCar" pointcut="execution (* com.techbirds.spring.aop_two.ICar.*(..))"/>
</aop:aspect>
</aop:config>
public class AopTest {
public static void main(String[] args) {
// ApplicationContext ctx
// = new FileSystemXmlApplicationContext("resources/aop.xml");
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/resources/aop.xml");
// 得到代理对象
ICar car = (ICar) ctx.getBean("bus");
// 执行方法
car.go();
}
}
6.测试结果:
2013-6-6 14:46:09 com.techbirds.spring.aop_two.LogBeforeCar beforeCar
信息: do ... before the car go
2013-6-6 14:46:09 com.techbirds.spring.aop_two.Bus go
信息: the bus go...
PS:
信息: do ... before the car go
2013-6-6 14:46:09 com.techbirds.spring.aop_two.Bus go
信息: the bus go...
PS:
Pointcut表达式:
第一个*:传回值任何类型都符合
第二个*:所有方法
第三个(..):任何参数类型声明都符合
四、总结
很明显第二种方式占优,代理对象都不需要进行配置。只要执行被代理对象的方法,就可以实现aop的pointcut。
注意:使用aop时千万别忘了加入jar包: