spring aop实现日志功能

来源:互联网 发布:淘宝评论怎么看不到 编辑:程序博客网 时间:2024/05/21 21:50
spring注解实现AOP利用spring实现AOP有两种方式:注解和xml文件定义。前者比较灵活,利于维护。一个小例子:一、接口PersonServicepackage com.aoptest.service;public interface PersonService{public void save(String name);public void update(String name,Integer id);public String getPersonName();}二、接口的实现类PersonServiceBean package com.aoptest.service.impl;import com.aoptest.service.PersonService;public class PersonServiceBean implements PersonService{@Overridepublic String getPersonName(){System.out.println("我是getPersonName()方法。。。");return "返回结果";} @Overridepublic void save(String name){System.out.println("我是save()方法。。。");throw new RuntimeException("异常了。。");} @Overridepublic void update(String name, Integer id){System.out.println("我是update()方法。。。");}}三、spring配置文件applicationContext.xml<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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-2.5.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><aop:aspectj-autoproxy /><bean id="myInterceptor" class="com.aoptest.service.MyInterceptor" /><bean id="personService" class="com.aoptest.service.impl.PersonServiceBean"></bean></beans>ps:1、红色部分是引入springAOP的命名空间2、绿色部分必须要有,声明需要AOP操作3、蓝色部分是做测试时用四、切面部分MyInterceptor/** * 切面 */@Aspect //声明此类为一个切面public class MyInterceptor{@Pointcut("execution(* com.aoptest.service..*.*(..))")//声明一个切入点,可以拦截具体到某个方法,即在执行此方法之前、之后、最终、异常……时可以执行的其他业务方法(通知advice);括号内的意思是:拦截某个方法,返回值是所有类型(第一个*),com.aoptest.service包及其子包下的所有类(..*),类下所有的方法(第三个.*),返回值任意(内部嵌套括号(..))//@Pointcut("execution(* com.aoptest.service.impl.PersonServiceBean.*(..))")//同上,两种都可以private void anyMethod(){}//声明一个切入点@Before("anyMethod() && args(name)")//声明前置通知public void doBefore(String name){System.out.println("前置通知");System.out.println("---"+name+"---");//可以得到参数值,此时args(name)限制条件限定了"execution(* com.aoptest.service..*.*(..))"中只拦截参数个数只有1个且类型为String的方法,而不是所有方法(该属性在当前拦截中适用,即前置通知适用)}@AfterReturning(pointcut="anyMethod()",returning="result")//声明后置通知public void doAfterReturning(String result){System.out.println("后置通知");System.out.println("---"+result+"---");//返回结果,同上}@AfterThrowing(pointcut="anyMethod()",throwing="e")//声明例外通知public void doAfterThrowing(Exception e){System.out.println("例外通知");System.out.println(e.getMessage());//获取异常信息,同上}@After("anyMethod()")//声明最终通知public void doAfter(){System.out.println("最终通知");}@Around("anyMethod()")//声明环绕通知public Object doAround(ProceedingJoinPoint pjp)throws Throwable{System.out.println("进入方法---环绕通知");Object o =  pjp.proceed();System.out.println("退出方法---环绕通知");return o;}}五、测试部分public class SpringAopTest{@Test public void interceptorTest(){ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");PersonService personService = (PersonService)cxt.getBean("personService");personService.save("xx");String result=personService.getPersonName();System.out.println(result);}}