<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 开启自动扫描,来支持Bean通过注解给spring管理 --> <context:component-scan base-package="cn.itcast"></context:component-scan> <!-- 开启自动代理实现AOP --> <aop:aspectj-autoproxy /> </beans>
package cn.itcast.service; public interface IPersonService { void save(String name); void update(String name, Integer id); String getPersonName(Integer id); }
package cn.itcast.service.impl; import org.springframework.stereotype.Service; import cn.itcast.service.IPersonService; @Service public class PersonServiceBean implements IPersonService { public void save(String name){ System.out.println("我是Save方法()"+name); throw new RuntimeException("ssss"); } public void update(String name,Integer id){ System.out.println("我是update方法()"+id+name); } public String getPersonName(Integer id){ System.out.println("我是getPersonName方法()"+id); return "lisi"; } }
package cn.itcast.service.impl; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Aspect //定义切面,要交给spring管理 @Component public class MyInterceptor { @Pointcut("execution(* cn.itcast.service.impl.*.*(..))") private void anyMethod(){} //定义切入点,像个方法 @Before("anyMethod() && args(name)") //要求方法里的参数只有一个 public void doBefore(String name){ System.out.println("前置通知"+name); } @AfterReturning(pointcut="anyMethod()",returning="result") public void doAfterReturning(String result) { System.out.println("后置通知"+result); } @After("anyMethod()") public void doAfter() { System.out.println("最终通知"); } @AfterThrowing(pointcut="anyMethod()",throwing="e") public void doAfterThrowing(Exception e){ System.out.println("抛出意外"); e.printStackTrace(); } @Around("anyMethod()") public Object deAround(ProceedingJoinPoint pjp)throws Throwable{ Object result; System.out.println("进入方法"); // if(true){ result=pjp.proceed(); // } System.out.println("退出方法"); return result; } }
package junit.test; import cn.itcast.service.*; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringAOPTest { @BeforeClass public static void setUpBeforeClass() throws Exception { } @Test public void interceptorTest(){ ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml"); IPersonService personService=(IPersonService)ctx.getBean("personServiceBean"); personService.getPersonName(2); personService.save("zhangsan"); } }