1、Spring4、SpringBoot1
1.1 代码实现
public interface Calculator { int div(int a,int b); }
@Component public class CalculatorImpl implements Calculator{ @Override public int div(int a, int b) { int result = a/b; System.out.println("计算结果为:"+result); return result; } }
@Aspect @Component public class CalculatorAspect { @Before(value = "execution(* com.jixian.springboot1.CalculatorImpl.*(..))") public void beforeNotify(){ System.out.println("@Before...前置通知"); } @After(value = "execution(* com.jixian.springboot1.CalculatorImpl.*(..))") public void afterNotify(){ System.out.println("@After...后置通知"); } @AfterReturning(value = "execution(* com.jixian.springboot1.CalculatorImpl.*(..))") public void afterReturningNotify(){ System.out.println("@AfterReturning...返回通知"); } @AfterThrowing(value = "execution(* com.jixian.springboot1.CalculatorImpl.*(..))") public void afterThrowingNotify(){ System.out.println("@AfterThrowing...异常通知"); } @Around(value = "execution(* com.jixian.springboot1.CalculatorImpl.*(..))") public Object aroundNotify(ProceedingJoinPoint joinPoint) throws Throwable{ Object res = null; System.out.println("@Around...环绕通知之前A"); res = joinPoint.proceed(); System.out.println("@Around...环绕通知之后B"); return res; } }
1.2 测试
@SpringBootTest @RunWith(SpringRunner.class) public class SpringBoot1ApplicationTests { @Autowired private Calculator calculator; @Test public void contextLoads() { System.out.println("Spring版本:"+ SpringVersion.getVersion()+"\t"+"SpringBoot版本:"+ SpringBootVersion.getVersion()); calculator.div(5,2); } }
2、Spring5、SpringBoot2
3、Spring6、SpringBoot3
标签:...,int,Spring,System,版本,AOP,println,public,out From: https://www.cnblogs.com/jixian/p/17162747.html