spring Ioc comment configuration
Do not use xml, pure java configuration Ioc, mave first create a project, import dependence jar
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.4</version>
</dependency>
</dependencies>
We first create a profile class Config.java
@EnableAspectJAutoProxy //开启自动代理
@Configuration
@ComponentScan(basePackages = "org.youyuan")
public class Config {
}
@EnableAspectJAutoProxy indicate on automatic proxy, which is equivalent to application.xml
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
@Configuration indicates that the current configuration is a class
@ComponentScan (basePackages = "org.youyuan") indicates that all of the current position of the package under the category scan code
Creating a class, which intercept method
Calculate.java
@Component
public class Calculate {
public int add(int a,int b){
return a+b;
}
}
Class defining aspects
aspect.java
@Component
@Aspect
public class aspect {
/*统一定义切点*/
@Pointcut("execution(* org.youyuan.aop.Calculate.add(..))")
public void pointcut(){
}
/*前置通知*/
@Before(value ="pointcut()")
public void before(JoinPoint point){
System.out.println("before");
String name = point.getSignature().getName();
System.out.println(name);
}
/*后置通知*/
@After(value = "pointcut()")
public void after(JoinPoint joinPoint){
System.out.println("after");
String name = joinPoint.getSignature().getName();
System.out.println(name);
}
/*返回通知*/
@AfterReturning(value = "pointcut()" ,returning = "res")
public void afterReturning(JoinPoint point,int res){
System.out.println("afterReturning----->结果为="+res);
String name = point.getSignature().getName();
System.out.println(name);
}
/*异常通知*/
@AfterThrowing(value = "pointcut()",throwing = "e")
public void afterThrowing (JoinPoint point,Exception e) {
System.out.println("afterThrowing");
System.out.println("异常为-->"+e);
String name = point.getSignature().getName();
System.out.println(name);
}
/*环绕通知*/
@Around(value = "pointcut()")
public int around (ProceedingJoinPoint joinPoint){
System.out.println("around1");
try {
Object proceed = joinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("around2");
return 4;
}
}
@Aspect represents the current class is a class section
Run the program
@Test
public void test6(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
Calculate bean = context.getBean(Calculate.class);
bean.add(1,2);
}
result
We can see from the results which surround notification Object proceed = joinPoint.proceed (); foregoing method is equivalent to the pre-notification, a post-notification corresponding to the back, and since the execution of the return time around execution of the return notification method than add of late, all the result of 3 to overwrite the original, so the return notification of the result is 4.