<六>AOP面向切面——注解方式声明切面(附源码)
2014-11-30 18:14
537 查看
1、beans.xml文件:
2、service层以及实现层如下:
PersonService.java
3、切面
声明一个切面,还需要在bean.xml文件中声明如下语句 <bean id="myInterceptor" class="cn.itcast.service.MyInterceptor"/>,表示将该切面交给spring管理
4、测试SpringAOPTest.java
完成。
源码下载:
点击打开链接
<?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管理 @Aspect public 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 { @BeforeClass public 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 最终通知 退出方法
完成。
源码下载:
点击打开链接
相关文章推荐
- <七>AOP面向切面——配置bean.xml文件方式声明切面(附源码)
- AOP实现方式3——通过<aop:config>来配置(纯POJO切面)
- Spring学习4-面向切面(AOP)之aspectj注解方式
- 关于spring切面使用<aop:aspectj-autoproxy/>报错的解决方法
- Spring学习4-面向切面(AOP)之aspectj注解方式
- AOP实现_AOP中的概念_注解方式_XML配置方式声明切面
- 面向切面(AOP)之Spring接口方式 schema配置方式 aspectj注解方式
- Spring——使用Spring的注解方式实现面向切面技术AOP
- Spring(十一)使用Spring的注解方式实现面向切面技术AOP
- Spring学习一:IOC(控制反转)和AOP(面向切面)的xml配置和注解方式
- Spring学习4-面向切面(AOP)之aspectj注解方式
- spring学习笔记7--使用spring进行面向切面的(AOP)编程(1)注解方式实现
- Spring面向切面编程——Spring实现AOP方式——通过注解实现
- <六>访问权限控制
- Spring AOP 面向切面编程相关注解
- Java乔晓松-基于注解的面向AOP(切面)编程
- Java基础加强<二>_内省、注解、泛型
- <Linux+Qt>一种移植qt可执行代码的方式
- <mvc:annotation-driven />注解意义
- 黑马程序员 java基础加强<六> 泛型