Spring的AOP简单介绍和常用配置(2)
2013-06-08 16:21
513 查看
在1中我简单的介绍了基于AOP标签配置的一种形式,并没有将前,后Advice以及异常Advice等配置罗列出来。
还是重复唠叨一下,基础AOP标签配置的好处和坏处都是什么:
好处:自动代理,不需要自定义代理对象。配置简单。
坏处:不易理解。
信息: 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...
还是重复唠叨一下,基础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...
相关文章推荐
- Spring的AOP简单介绍和常用配置(3)-事务配置
- Spring的AOP简单介绍和常用配置(3)-事务配置
- Spring的AOP简单介绍和常用配置(1)
- 基于xml配置的Spring AOP切面的几点总结,以及简单的例子
- Spring aop 使用aspectjrt配置切面+ehcache简单配置
- spring rabbitmq 配置文件简单介绍
- Spring AOP 配置文件的简单理解
- Spring AOP面向切面编程一个简单例子和在配置过程中出现错误
- Spring 配置log4j和简单介绍Log4J的使用
- Spring AOP三种配置详细介绍
- spring-context.xml 文件配置的简单介绍
- Spring 配置log4j和简单介绍Log4J的使用
- 05 Spring Aop实例(AOP 如此简单)@Aspect、@Around 注解方式配置
- SpringBoot常用配置 application.yml /application.properties常用配置介绍
- SpringBoot常用配置介绍
- Spring Aop配置使用示例及实现过程和原理介绍
- 一个超级简单的dwr配置文件,介绍了dwr最常用的几个标签(转)
- spring aop介绍与配置
- 05 Spring Aop实例(AOP 如此简单)@Aspect、@Around 注解方式配置
- SpringBoot日志配置简单介绍