package com.elf.spring.aop.aspectj;
public interface UsbInterface {
public void work();
}
package com.elf.spring.aop.aspectj;
import org.springframework.stereotype.Component;
@Component
public class Phone implements UsbInterface{
@Override
public void work() {
System.out.println("手机开始工作了....");
}
}
package com.elf.spring.aop.aspectj;
import org.springframework.stereotype.Component;
@Component
public class Camera implements UsbInterface {
@Override
public void work() {
System.out.println("相机开始工作...");
}
}
package com.elf.spring.aop.aspectj;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Order(value = 2)
@Aspect
@Component
public class SmartAnimalAspect {
@Pointcut(value = "execution(public float com.elf.spring.aop.aspectj.SmartDog.getSum(float, float)))")
public void myPointCut() {
}
@Before(value = "myPointCut()")
public void showBeginLog(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
System.out.println("SmartAnimalAspect-切面类showBeginLog()[使用的myPointCut()]-方法执行前-日志-方法名-" + signature.getName() + "-参数 "
+ Arrays.asList(joinPoint.getArgs()));
}
@AfterReturning(value = "myPointCut()", returning = "res")
public void showSuccessEndLog(JoinPoint joinPoint, Object res) {
Signature signature = joinPoint.getSignature();
System.out.println("SmartAnimalAspect-切面类showSuccessEndLog()-方法执行正常结束-日志-方法名-" + signature.getName() + " 返回的结果是=" + res);
}
@AfterThrowing(value = "myPointCut()", throwing = "throwable")
public void showExceptionLog(JoinPoint joinPoint, Throwable throwable) {
Signature signature = joinPoint.getSignature();
System.out.println("SmartAnimalAspect-切面类showExceptionLog()-方法执行异常-日志-方法名-" + signature.getName() + " 异常信息=" + throwable);
}
@After(value = "myPointCut()")
public void showFinallyEndLog(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
System.out.println("SmartAnimalAspect-切面类showFinallyEndLog()-方法最终执行完毕-日志-方法名-" + signature.getName());
}
@Before(value = "execution(public void com.elf.spring.aop.aspectj.UsbInterface.work())")
public void hi(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
System.out.println("切面类的hi()-执行的目标方法-" + signature.getName());
}
@Before(value = "execution(public void Car.run())")
public void ok1(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
System.out.println("切面类的ok1()-执行的目标方法-" + signature.getName());
}
@AfterReturning(value = "execution(public void Car.run())")
public void ok2(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
System.out.println("切面类的ok2()-执行的目标方法-" + signature.getName());
}
@AfterThrowing(value = "execution(public void Car.run())")
public void ok3(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
System.out.println("切面类的ok3()-执行的目标方法-" + signature.getName());
}
@After(value = "execution(public void Car.run())")
public void ok4(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
System.out.println("切面类的ok4()-执行的目标方法-" + signature.getName());
joinPoint.getSignature().getName();
joinPoint.getSignature().getDeclaringType().getSimpleName();
joinPoint.getSignature().getDeclaringTypeName();
joinPoint.getSignature().getModifiers();
Object[] args = joinPoint.getArgs();
joinPoint.getTarget();
joinPoint.getThis();
}
}
package com.elf.spring.aop.aspectj;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopAspectjTest {
@Test
public void smartDogTestByProxy() {
ApplicationContext ioc =
new ClassPathXmlApplicationContext("beans08.xml");
SmartAnimalable smartAnimalable =
ioc.getBean(SmartAnimalable.class);
smartAnimalable.getSum(10, 2);
System.out.println("smartAnimalable运行类型="
+ smartAnimalable.getClass());
System.out.println("=============================");
}
@Test
public void smartDogTestByProxy2() {
ApplicationContext ioc =
new ClassPathXmlApplicationContext("beans08.xml");
UsbInterface phone = (UsbInterface) ioc.getBean("phone");
UsbInterface camera = (UsbInterface) ioc.getBean("camera");
phone.work();
System.out.println("==================");
camera.work();
}
@Test
public void test3() {
ApplicationContext ioc =
new ClassPathXmlApplicationContext("beans08.xml");
Car car = ioc.getBean(Car.class);
System.out.println("car的运行类型=" + car.getClass());
car.run();
}
@Test
public void testDoAround() {
ApplicationContext ioc =
new ClassPathXmlApplicationContext("beans08.xml");
SmartAnimalable smartAnimalable =
ioc.getBean(SmartAnimalable.class);
smartAnimalable.getSum(10, 2);
}
}