Spring AOP实例
2015-07-22 10:43
459 查看
先写一个接口
在继承实现它
注意在bean中,studentservice依赖注入的class一定要写接口的地址。否则会报各种莫名其妙的错。
还有在bean中,抛出异常的通知一定要加上throwing="ex"。否则会报各种莫名其妙的错。
<span style="font-size:18px;">package com.shuhui.impl; public interface StudentService { public void addStudent(String name); } </span>
在继承实现它
<span style="font-size:18px;">package com.shuhui.impl; public class StudentServiceImpl implements StudentService{ @Override public void addStudent(String name) { System.out.println("添加学生"+name); } } </span>切入层
<span style="font-size:18px;">package com.shuihui.Aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; public class StudentServiceAspect { public void doBefore(JoinPoint jp){ System.out.println("doBefore开始添加学生"); System.out.println("类名"+jp.getTarget().getClass().getName()); System.out.println("方法名"+jp.getSignature().getName()); } public void doAfter(JoinPoint jp){ System.out.println("doAfter添加学生结束"); System.out.println("类名"+jp.getTarget().getClass().getName()); System.out.println("方法名"+jp.getSignature().getName()); } public Object doAround(ProceedingJoinPoint pjp) throws Throwable{ System.out.println("doAround开始添加学生"); Object obj = pjp.proceed(); //若目标方法有返回值,则obj就是目标方法的返回值 System.out.println("doAround添加学生结束"); return obj; } public void doAfterThrowing(JoinPoint jp,Throwable ex){ System.out.println("异常信息"+ex.getMessage()); } } </span>bean
<span style="font-size:18px;"><?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <aop:aspectj-autoproxy/> <bean id="StudentServiceAspect" class="com.shuihui.Aspect.StudentServiceAspect"></bean> <bean id="studentService" class="com.shuhui.impl.StudentServiceImpl"></bean> <aop:config> <aop:aspect id="StudentServiceAspect" ref="StudentServiceAspect"> <aop:pointcut expression="execution(* com.shuhui.impl.*.*(..))" id="businessService" /> <aop:before method="doBefore" pointcut-ref="businessService"/> <aop:after method="doAfter" pointcut-ref="businessService"/> <aop:around method="doAround" pointcut-ref="businessService"/> <aop:after-throwing method="doAfterThrowing" pointcut-ref="businessService" throwing="ex"/> </aop:aspect> </aop:config> </beans></span>测试
<span style="font-size:18px;">package com.shuhui.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.shuhui.impl.StudentService; public class Test { public static void main(String[] args) { @SuppressWarnings("resource") ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); StudentService s = (StudentService) ac.getBean("studentService"); s.addStudent("张三"); } } </span>
注意在bean中,studentservice依赖注入的class一定要写接口的地址。否则会报各种莫名其妙的错。
还有在bean中,抛出异常的通知一定要加上throwing="ex"。否则会报各种莫名其妙的错。
<span style="font-size:18px;">after-throwing</span>
<span style="font-size:18px;">after-throwing</span>
相关文章推荐
- Java图形用户界面布局控制Layout练习
- java常用语句
- java最简单实现LRUCache
- Spring笔记
- 【Java】两个排序后的数组A和B,其中A的末端有足够的缓冲容纳B。编写一个方法,将B合并入A并排序。
- myEclipse2015启动myEclipse时发现了以元素"d:disk"的无效内容
- JAVA之BigInteger
- JAVA Calendar详解
- Java Web乱码分析及解决方案(三)——响应乱码
- Eclipse ADT启动报“fail to load the jni shared library”问题解决方案
- Java 应用
- 如何在一台新电脑上配置JAVA开发环境
- Java对象的序列化和反序列化
- java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/TransactionA
- (转)java中静态代码块的用法 static用法详解
- eclipse内存不足解决方案
- Java并发编程之AQS
- JAVA基础之数组
- 安装jdk出现问题:Error opening registry key'software\Javasoft\Java Runti
- 模板模式和策略模式的区别【转】