1. Around Advice Type
- org.zcs.spring.zcs.springAop - AopMain.java
package org.zcs.springAop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.zcs.springAop.service.ShapeService;
public class AopMain {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
ShapeService shapeservice = ctx.getBean("shapeService", ShapeService.class);
shapeservice.getCircle() ;
}
}
- org.zcs.springAop.aspect - LoggingAspect.java
package org.zcs.springAop.aspect;
import org.aspectj.lang.JoinPoint;
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;
import org.zcs.springAop.model.Circle;
@Aspect
public class LoggingAspect {
@Before("allCircleMethods()")
public void LoggingAdvice(JoinPoint joinPoint) {
}
@AfterReturning(pointcut="args(name)", returning="returnString")
public void getMethodsarguments(String name, String returnString) {
System.out.println("A method that takes string argument be called . The Value is : " + name +",The return String is :" + returnString);
}
@AfterThrowing(pointcut="args(name)", throwing = "ex")
public void exceptionAdvice(String name, RuntimeException ex) {
System.out.println("An execption has been thrown." + ex);
}
@Around("allGetters()")
public Object myAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) {
Object returnvalue = null;
try {
System.out.println("Before advice");
returnvalue = proceedingJoinPoint.proceed();
System.out.println("After Returning.");
} catch (Throwable e) {
System.out.println("After Throwing");
}
System.out.println("After finally.");
return returnvalue;
}
@Pointcut("execution( public * get*())")
public void allGetters() {
}
@Pointcut("within(org.zcs.springAop.model.Circle)")
public void allCircleMethods() {}
}
- org.zcs.springAop.model - Triangle.java
package org.zcs.springAop.model;
public class Triangle {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- org.zcs.springAop.model - Circle.java
package org.zcs.springAop.model;
public class Circle {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
System.out.println(" Circle's setter be called." );
throw(new RuntimeException());
}
public String setNameReturn(String name) {
this.name = name;
System.out.println(" Circle's setter be called." );
return name;
}
}
- org.zcs.springAop.service - ShapeService.java
package org.zcs.springAop.service;
import org.zcs.springAop.model.Circle;
import org.zcs.springAop.model.Triangle;
public class ShapeService {
private Circle circle;
private Triangle triangle;
public Circle getCircle() {
return circle;
}
public void setCircle(Circle circle) {
this.circle = circle;
}
public Triangle getTriangle() {
return triangle;
}
public void setTriangle(Triangle triangle) {
this.triangle = triangle;
}
}
<?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></aop:aspectj-autoproxy>
<bean name="triangle" class = "org.zcs.springAop.model.Triangle">
<property name="name" value = "Triangle name"></property>
</bean>
<bean name = "circle" class = "org.zcs.springAop.model.Circle">
</bean>
<bean name="shapeService" class="org.zcs.springAop.service.ShapeService" autowire = "byName"></bean>
<bean name= "loggingAspect" class = "org.zcs.springAop.aspect.LoggingAspect"></bean>
</beans>
2.学习记录
- 学习了Around注释的执行过程,了解了其执行流程。