Spring的AOP简单介绍和常用配置(2)
来源:互联网 发布:中国中小企业数据库 编辑:程序博客网 时间:2024/05/22 13:51
在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());@Overridepublic 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/aophttp://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...
- Spring的AOP简单介绍和常用配置(2)
- Spring的AOP简单介绍和常用配置(1)
- Spring的AOP简单介绍和常用配置(3)-事务配置
- Spring的AOP简单介绍和常用配置(3)-事务配置
- Spring AOP的简单介绍
- Spring AOP简单的配置(注解和xml配置)
- Spring AOP简单的配置(注解和xml配置)
- Spring框架AOP的配置和实现的简单例子
- spring aop 简单介绍
- Spring笔记之四:Spring AOP介绍和常用的增强类
- Spring中IOC和AOP的介绍
- Spring AOP编程简单介绍
- spring aop介绍与配置
- Spring AOP的理解和简单实现
- Spring 简单的IOC和AOP
- spring的AOP介绍
- Spring的AOP介绍
- Spring的aop介绍
- Sys文件系统分析 --kobject,kset
- 想说什么2
- Tomcat6.0+Spring+JNDI+C3P0连接oracle数据库
- VC++深入详解笔记——12. 文件操作
- 配置Tomcat的访问日志格式化输出
- Spring的AOP简单介绍和常用配置(2)
- OpenStack对象存储管理手册(5) OpenStack对象存储系统管理-3
- oracle 中建自动自长列方法
- 结构体作函数参数
- PM是一个事实
- C++ Primer 第四章
- 面向对象概念:重载和覆盖
- 工厂模式
- VC和InstallShield工具