


Java--Simple Spring AOP configuration and AOP transaction management, JDK/GCLib dynamic proxy
Oct 13, 2016 am 10:01 AM1. Take a look at the simple AOP configuration through XML
1. First create a simple Student class
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. Create a simple 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 configuration
<bean id="student" class="com.seeyon.SpringBean.aop.Student" p:name="yangyu" p:age="27"></bean> <bean id="logging" class="com.seeyon.SpringBean.aop.Logging"></bean> <!--XML方式配置Spring AOP--> <aop:config> <aop:aspect id="log" ref="logging"> 【切面class】 <aop:pointcut id="studentMethod" expression="execution(* com.seeyon.SpringBean.aop.Student.get*(..))"/> 【切點】 <aop:before pointcut-ref="studentMethod" method="beforeAdvice"/> 【方法執(zhí)行之前觸發(fā)切面class的beforeAdvice方法】 <aop:after pointcut-ref="studentMethod" method="afterAdvice"/> 【方法執(zhí)行之后觸發(fā)切面class的afterAdvice方法】 </aop:aspect> </aop:config>
Analyze this execution(* com.seeyon.SpringBean.aop.Student.get*(..)) pointcut expression:
(1) The first * represents the return value of the method is arbitrary
(2) get* represents All methods starting with get
(3)(..) represent any number of method parameters
4.main method
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.Output results
Going to setup student profile. Name : yangyu Student profile has been setup.
2. Use of Spring AOP annotations .
1. First create a simple Student class (same as in .1, see above

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)