<六>AOP面向切面——注解方式声明切面(附源码)
来源:互联网 发布:日文游戏汉化软件 编辑:程序博客网 时间:2024/06/03 17:03
1、beans.xml文件:
<?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">//新加入 <aop:aspectj-autoproxy/>//新加入,开启spring的拦截配置 <bean id="myInterceptor" class="cn.itcast.service.MyInterceptor"/> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean></beans>
2、service层以及实现层如下:
PersonService.java
package cn.itcast.service;public interface PersonService {public void save(String name);public void update(String name, Integer id);public String getPersonName(Integer id);}PersonServiceBean.java
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 "ning";}public void save(String name) {//throw new RuntimeException("我爱例外");System.out.println("我是save()方法");}public void update(String name, Integer id) {System.out.println("我是update()方法");}}3、切面
声明一个切面,还需要在bean.xml文件中声明如下语句 <bean id="myInterceptor" class="cn.itcast.service.MyInterceptor"/>,表示将该切面交给spring管理
package cn.itcast.service;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;/** * 切面 * *///声明一个切面,还需要在bean.xml文件中声明如下语句// <bean id="myInterceptor" class="cn.itcast.service.MyInterceptor"/>//表示将该切面交给spring管理@Aspectpublic class MyInterceptor {//声明一个切入点,execution代表执行,执行业务方法时候进行拦截//* cn.itcast.service..*.*(..)//第一个*表示返回值类型,没有具说明就是所有类型; //cn.itcast.service表示包名,对该包下的类进行拦截;接下来两个点..代表子包//第二个*表示类,对该类进行拦截,没有具体指定则用*号表示,代表所有类//第三个*代表方法,对该方法进行拦截,没有具体指定则用*号表示,代表所有方法//最后两点..表示方法的参数//以下一句表示,任意返回值,对cn.itcast.service.impl包下的PersonServiceBean类的*所有方法进行拦截@Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean.*(..))")private void anyMethod() {}//声明一个切入点//前置通知,拦截方法执行前,先执行该方法。若要自己加参数,则使用&&符号,//这意味着在cn.itcast.service.impl.PersonServiceBean类中的方法,只会执行符合参数定义的方法//通过在SpringAOPTest中的save("chenning")方法会让前置通知得到该参数@Before("anyMethod() && args(name)")public void doAccessCheck(String name) {System.out.println("前置通知:"+name);}//后置通知@AfterReturning(pointcut="anyMethod()",returning="chen")public void doAfterReturning(String chen){System.out.println("后置通知"+chen);}//最终通知@After("anyMethod()")public void doAfter(){System.out.println("最终通知");}//例外通知@AfterThrowing(pointcut="anyMethod()",throwing="e")public void doAfterThrowing(Exception e){System.out.println("例外通知"+e);}////环绕通知@Around("anyMethod()")public Object doBasicProfilling(ProceedingJoinPoint pjp) throws Throwable{System.out.println("进入方法");Object result=pjp.proceed();System.out.println("退出方法");return result;}}
4、测试SpringAOPTest.java
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 {@BeforeClasspublic static void setUpBeforeClass() throws Exception {}@Test public void interceptorTest(){ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");PersonService personService = (PersonService)cxt.getBean("personService");personService.save("chenning");personService.getPersonName(2);}}5、控制台输出:
前置通知:chenning进入方法我是save()方法后置通知null最终通知退出方法
进入方法我是getPersonName()方法后置通知ning最终通知退出方法
完成。
源码下载:
点击打开链接
0 0
- <六>AOP面向切面——注解方式声明切面(附源码)
- <七>AOP面向切面——配置bean.xml文件方式声明切面(附源码)
- aop—面向切面编程
- aop—面向切面编程
- Spring面向切面编程——Spring实现AOP方式——通过注解实现
- Spring——AOP(面向切面编程)@AspectJ注解方式
- Spring——使用Spring的注解方式实现面向切面技术AOP
- Spring----AOP面向切面编程例子解析(附源码)
- Spring----AOP面向切面编程例子解析(附源码)
- Spring面向切面编程--AOP,AspectJ,基于注解方式。
- spring aop, aspectj 注解声明切面
- 面向切面编程——Aop
- AOP-面向切面编程——AspectJ
- AOP——面向切面编程
- 面向切面编程(AOP)应用,日志切面,基于注解
- Spring 面向切面(AOP)编程,注解
- 注解方式实现AOP切面编程
- 注解方式实现aop事务切面
- Scale Out将是未来的企业架构
- Python打包的艺术(二)- Packaging Tools的昨天,今天,明天
- 【UML】3.类图的读和画
- 《数据结构》第六章 图 教学设计
- 关于LINUX C库函数 中的 fprintf
- <六>AOP面向切面——注解方式声明切面(附源码)
- Sublime Text 2 使用心得
- hdu 2835 Operating system (模拟)
- 重载bign常用运算符
- 关于安装TortoiseSVN时遇到无法获得安装权限的
- 2014-J2EE开发工作中遇到的异常问题及解决方法总结
- PMD规则之Design Rules
- IAR创建工程文件快速建立之igBee CC2530
- perl 使用过自定义lib目录