简单写一下切面实现:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface MyPointcut {
}
注解切点
@Aspect
@Component
public class MyAdvice {
@Pointcut("@annotation(com.example.demo4.MyPointcut)")
private void MyPointcut(){};
@Around("MyPointcut()")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕前通知");
Object proceed = proceedingJoinPoint.proceed();
System.out.println("环绕后通知");
return proceed;
}
}
切面实现
@Component
public class TestAspect {
@MyPointcut
public void test1(){
System.out.println("testaspect....");
}
}
应用实现
@SpringBootApplication
public class Demo4Application {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Demo4Application.class, args);
TestAspect testAspect = context.getBean(TestAspect.class);
testAspect.test1();
}
}
运行实现
2019-06-11 11:01:45.261 INFO 7000 --- [ restartedMain] com.example.demo4.Demo4Application : Started Demo4Application in 3.782 seconds (JVM running for 9.369)
环绕前通知
testaspect....
环绕后通知
2019-06-11 11:01:45.294 INFO 7000 --- [ Thread-10] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
结果展示