在1中我简单的介绍了基于AOP标签配置的一种形式,并没有将前,后Advice以及异常Advice等配置罗列出来。
还是重复唠叨一下,基础AOP标签配置的好处和坏处都是什么:
好处:自动代理,不需要自定义代理对象。配置简单。
坏处:不易理解。
下面是实例:
步骤:
1、业务接口定义
package com.techbirds.spring.aop_two;
public interface ICar {
public void go() throws Exception;
}
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() throws Exception {
logger.info("the bus go...");
throw new Exception();
}
}
3、Adivce(插入代码)实现
package com.techbirds.spring.aop_two;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.aspectj.lang.JoinPoint;
public class LogAspectCar {
private Logger logger=Logger.getLogger(this.getClass().getName());
/**
* 执行后
* @param jp
*/
public void afterCar(JoinPoint jp){
logger.info("do ... after the car "+jp.getSignature().getName());
}
/**
* 执行前
* @param jp
*/
public void beforeCar(JoinPoint jp){
logger.info("do ... before the car "+jp.getSignature().getName());
}
/**
* 执行异常信息获取
* @param jp
* @param ta
*/
public void throwExceptionOfCar(JoinPoint jp,Throwable ta){
logger.log(Level.WARNING,"log "+ta+" " +
"Exception was thrown in "+jp.getSignature().getDeclaringTypeName()+"."+jp.getSignature().getName());
}
}
4、spring对应配置
<?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">
<bean id="bus" class="com.techbirds.spring.aop_two.Bus"></bean>
<bean id="logAspectCar" class="com.techbirds.spring.aop_two.LogAspectCar"></bean>
<aop:config>
<aop:aspect ref="logAspectCar">
<aop:before method="beforeCar" pointcut="execution (* com.techbirds.spring.aop_two.ICar.*(..))"/>
<aop:after-returning method="afterCar" pointcut="execution (* com.techbirds.spring.aop_two.ICar.*(..))" />
<aop:after-throwing method="throwExceptionOfCar" throwing="ta" pointcut="execution (* com.techbirds.spring.aop_two.ICar.*(..))" />
</aop:aspect>
</aop:config>
<!--
这个配置跟上面的目的一样,比较方便一点
<aop:config>
<aop:aspect ref="logAspectCar">
<aop:pointcut id="car_pc" expression="execution (* com.techbirds.spring.aop_two.ICar.*(..))" />
<aop:before method="beforeCar" pointcut-ref="car_pc"/>
<aop:after-returning method="afterCar" pointcut-ref="car_pc"/>
</aop:aspect>
</aop:config>
-->
</beans>
5、测试
package com.techbirds.spring.aop_two;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopTest {
//public Logger logger=Logger.getLogger(this.getClass().getName());
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");
// 执行方法
try {
car.go();
} catch (Exception e) {
System.out.println("go error...");
}
}
}
6、测试结果
2013-6-8 16:05:59 com.techbirds.spring.aop_two.LogAspectCar beforeCar
信息: do ... before the car go
2013-6-8 16:05:59 com.techbirds.spring.aop_two.Bus go
信息: the bus go...
2013-6-8 16:05:59 com.techbirds.spring.aop_two.LogAspectCar throwExceptionOfCar
警告: log java.lang.Exception Exception was thrown in com.techbirds.spring.aop_two.ICar.go
go error...