

org.springframework spring-aspects 5.3.1
jar org.springframework spring-context 5.3.1 junit junit 4.12 test org.springframework spring-aspects 5.3.1
com.atguigu.spring.aop.annotation


把模块spring-proxy里面的Calculator和CalculatorImpl复制到spring-aop模块的com.atguigu.spring.aop.annotation中
具体代码如下:
- package com.atguigu.spring.aop.annotation;
-
- public interface Calculator {
-
- int add(int i, int j);
- int sub(int i, int j);
- int mul(int i, int j);
- int div(int i, int j);
-
- }
实现类: CalculatorPureImpl
- package com.atguigu.spring.aop.annotation;
-
-
- public class CalculatorImpl implements Calculator {
- @Override
- public int add(int i, int j) {
- int result = i + j;
- System.out.println("方法内部,result:"+result);
- return result;
- }
-
- @Override
- public int sub(int i, int j) {
- int result = i - j;
- System.out.println("方法内部,result:"+result);
- return result;
- }
-
- @Override
- public int mul(int i, int j) {
- int result = i * j;
- System.out.println("方法内部,result:"+result);
- return result;
- }
-
- @Override
- public int div(int i, int j) {
- int result = i / j;
- System.out.println("方法内部,result:"+result);
- return result;
- }
- }



- package com.atguigu.spring.aop.annotation;
-
- import org.springframework.stereotype.Component;
-
- /**
- * Date:2022/7/4
- * Author:ybc
- * Description:
- */
- @Component
- public class CalculatorImpl implements Calculator {
- @Override
- public int add(int i, int j) {
- int result = i + j;
- System.out.println("方法内部,result:"+result);
- return result;
- }
-
- @Override
- public int sub(int i, int j) {
- int result = i - j;
- System.out.println("方法内部,result:"+result);
- return result;
- }
-
- @Override
- public int mul(int i, int j) {
- int result = i * j;
- System.out.println("方法内部,result:"+result);
- return result;
- }
-
- @Override
- public int div(int i, int j) {
- int result = i / j;
- System.out.println("方法内部,result:"+result);
- return result;
- }
- }

- package com.atguigu.spring.aop.annotation;
- import org.springframework.stereotype.Component;
- @Component
- public class LogAspect {
-
-
- }
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
-
-
-
-
- <context:component-scan base-package="com.atguigu.spring.aop.annotation"/>
-
- beans>

以上执行是切面类(LogAspect)和目标类(CalculatorImpl)都需要交给IOC容器管理
因为对于IOC容器来说 @Component是一个普通的组件 现在要告诉IOC 他是一个切面组件
所以这时我们要加上@Aspect将当前组件标识为切面

- package com.atguigu.spring.aop.annotation;
-
-
- import org.aspectj.lang.annotation.Aspect;
- import org.springframework.stereotype.Component;
-
- @Component
- @Aspect
- public class LogAspect {
-
-
- }