SpringAop实际上就是代理模式
切面=切入点+连接点+通知




该包是在spring-context依赖下的子包,所以有context就有aop

-
org.springframework -
spring-context -
5.2.10.RELEASE -
-
org.aspectj -
aspectjweaver -
1.9.4 -
- public interface BookDao {
- public void save();
- public void update();
- }
tip:@Repository 注解加在Dao层上,也就是与数据库操作的层上。
- @Repository
- public class BookDaoImpl implements BookDao {
-
- public void save() {
- System.out.println(System.currentTimeMillis());
- System.out.println("book dao save ...");
- }
-
- public void update(){
- System.out.println("book dao update ...");
- }
- }
- @Component //告诉spring要把MyAdvice 当作Bean
- //设置当前类为切面类类
- @Aspect //告诉spring要把MyAdvice 当作AOP,切面类
- //定义通知类
- public class MyAdvice {
- //设置切入点,要求配置在方法上方
- @Pointcut("execution(void com.itheima.dao.BookDao.update())")
- private void pt(){}
-
- //设置在切入点pt()的前面运行当前操作(前置通知)
- @Before("pt()")
- //制作共性通知
- public void method(){
- System.out.println(System.currentTimeMillis());
- }
- }
由图可知,@Aspect 是告诉spring这是一个aop,@Pointcut 代表 pt()方法是一个切入点,注意py()方法只需要一个空架子 然后@Before(“pt()”) 代表在pt()方法之前将共性方法切入进去。
- @Configuration
- @ComponentScan("com.itheima")
- //开启注解开发AOP功能
- @EnableAspectJAutoProxy
- public class SpringConfig {
- }
- public class App {
- public static void main(String[] args) {
- ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
- BookDao bookDao = ctx.getBean(BookDao.class);
- // bookDao.update();
- bookDao.update();
- // System.out.println(bookDao.getClass());
- }j
- }




ProceedingJoinPoint是用来获取原方法的对象,并且返回的是原方法的返回值

4.@AfterReturning
5.@AfterThrowing
