您好,欢迎访问一九零五行业门户网

Java--简单的Spring AOP配置以及AOP事物管理,JDK/GCLib动态代理

一、看一下简单的通过xml的aop配置
1.首先创建一个简单的student类
public class student { private integer age; private string name; public void setage(integer age) { this.age = age; } public integer getage() { system.out.println(age : + age); return age; } public void setname(string name) { this.name = name; } public string getname() { system.out.println(name : + name); return name; } public void printthrowexception() { system.out.println(exception raised); throw new illegalargumentexception(); } }
2.创建一个简单的aspect切面class
public class logging {/** * this is the method which i would like to execute * before a selected method execution. */public void beforeadvice() { system.out.println(going to setup student profile.); } /** * this is the method which i would like to execute * after a selected method execution. */public void afteradvice() { system.out.println(student profile has been setup.); } /** * this is the method which i would like to execute * when any method returns. */ public void afterreturningadvice(object retval) { system.out.println(returning: + retval.tostring()); } /** * this is the method which i would like to execute * if there is an exception raised. */ public void afterthrowingadvice(illegalargumentexception ex) { system.out.println(there has been an exception: + ex.tostring()); } }
3.springaop.xml配置
【切面class】 【切点】 【方法执行之前触发切面class的beforeadvice方法】 【方法执行之后触发切面class的afteradvice方法】
分析一下这个execution(* com.seeyon.springbean.aop.student.get*(..))切点表达式:
(1)第一个*代表方法的返回值是任意的
(2)get*代表以get开头的所有方法
(3)(..)代表方法的参数是任意个数
4.main方法
public class test { public static void main(string[] args) { applicationcontext context = new classpathxmlapplicationcontext(springaop.xml); student student = (student) context.getbean(student); student.getname(); // student.getage(); // student.printthrowexception(); } }
5.输出结果
going to setup student profile. name : yangyu student profile has been setup.
二、spring aop注解的使用。
1.首先创建一个简单的student类(同一.1中,请看上面
其它类似信息

推荐信息