beans.xml文件的配置
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns=""
- xmlns:xsi=""
- xmlns:context=""
- xmlns:aop=""
- xsi:schemaLocation="
- /spring-beans-2.5.xsd
- /spring-context-2.5.xsd
- /spring-aop-2.5.xsd">
- <aop:aspectj-autoproxy/>
- <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean>
- <bean id="aspetbean" class="cn.itcast.service.MyInterceptor"/>
- <aop:config>
- <aop:aspect id="asp" ref="aspetbean">
- <aop:pointcut id="mycut" expression="execution(* cn.itcast.service..*.*(..))"/>
- <aop:before pointcut-ref="mycut" method="doAccessCheck"/>
- <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
- <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>
- <aop:after pointcut-ref="mycut" method="doAfter"/>
- <aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
- </aop:aspect>
- </aop:config>
- </beans>
<aop:pointcut id="mycut" expression="execution(* cn.itcast.service..*.*(..))"/>
声明一个切入点,表达式表示拦截所有的返回类型,service文件下的所有子文件的类,的所有方法
切面就是一个pojo类
- package cn.itcast.service;
- import org.aspectj.lang.ProceedingJoinPoint;
- /**
- * 切面
- *doBasicProfiling(ProceedingJoinPoint pjp)这个方法可以不要
- *就不用引入相关的包
- */
- public class MyInterceptor {
- public void doAccessCheck() {
- System.out.println("前置通知");
- }
- public void doAfterReturning() {
- System.out.println("后置通知");
- }
-
- public void doAfter() {
- System.out.println("最终通知");
- }
-
- public void doAfterThrowing() {
- System.out.println("例外通知");
- }
-
- public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
- System.out.println("进入方法");
- Object result = pjp.proceed();
- System.out.println("退出方法");
- return result;
- }
-
- }
被切入的类
- package cn.itcast.service.impl;
- import cn.itcast.service.PersonService;
- public class PersonServiceBean implements PersonService {
- public String getPersonName(Integer id) {
- System.out.println("我是getPersonName()方法");
- return "xxx";
- }
- public void save(String name) {
- //throw new RuntimeException("我爱例外");
- System.out.println("我是save()方法");
- }
- public void update(String name, Integer id) {
- System.out.println("我是update()方法");
- }
- }
测试一下
- package junit.test;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import cn.itcast.service.PersonService;
- public class SpringAOPTest {
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- }
- @Test public void interceptorTest(){
- ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
- PersonService personService = (PersonService)cxt.getBean("personService");
- personService.getPersonName(2);
- personService.update("king", 2);
- }
- }
- log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
前置通知
进入方法
我是getPersonName()方法
后置通知
最终通知
退出方法
前置通知
进入方法
我是update()方法
后置通知
最终通知
退出方法
阅读(467) | 评论(0) | 转发(0) |