目录
一、AOP(面向切面编程)
1、概念
2、 流程
第一步:加依赖
第二步:编写连接点
第三步:编写通知类
第四步:抽取共性方法,写通知
第五步:定义切入点
第五步:切面连接切入点和通知
第六步:spring核心配置文件,加启动aspect的注解
一、AOP(面向切面编程)
1、概念
在我看来,AOP指的是在不改变原先代码设计的基础上,对原有的代码进行功能增强。
2、 流程
第一步:加依赖
aop依赖(spring框架依赖中有)和aspectj依赖
org.springframework spring-context5.2.10.RELEASE org.aspectj aspectjweaver1.9.4
第二步:编写连接点
@Repository
public class BookDaoImpl implements BookDao {
public void save() {//连接点1
System.out.println(System.currentTimeMillis());
System.out.println("book dao save ...");
}
public void update(){//连接点2
System.out.println("book dao update ...");
}
}
第三步:编写通知类
//通知类必须配置成Spring管理的bean
@Component
//设置当前类为切面类类
@Aspect
public class MyAdvice {}
第四步:抽取共性方法,写通知
//通知类必须配置成Spring管理的bean
@Component
//设置当前类为切面类类
@Aspect
public class MyAdvice {
//设置在切入点pt()的前面运行当前操作(前置通知)
// @Before("pt()")
public void method(){
System.out.println(System.currentTimeMillis());
}
}
第五步:定义切入点
//通知类必须配置成Spring管理的bean
@Component
//设置当前类为切面类类
@Aspect
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());
}
}
第五步:切面连接切入点和通知
//通知类必须配置成Spring管理的bean
@Component
//设置当前类为切面类类
@Aspect
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());
}
}
第六步:spring核心配置文件,加启动aspect的注解
@Configuration
@ComponentScan("com.itheima")
//开启注解开发AOP功能
@EnableAspectJAutoProxy
public class SpringConfig {
}
//通知类必须配置成Spring管理的bean
@Component
//设置当前类为切面类类
@Aspect
public class MyAdvice {}
第四步:抽取共性方法,写通知
//通知类必须配置成Spring管理的bean
@Component
//设置当前类为切面类类
@Aspect
public class MyAdvice {
//设置在切入点pt()的前面运行当前操作(前置通知)
// @Before("pt()")
public void method(){
System.out.println(System.currentTimeMillis());
}
}
第五步:定义切入点
//通知类必须配置成Spring管理的bean
@Component
//设置当前类为切面类类
@Aspect
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());
}
}
第五步:切面连接切入点和通知
//通知类必须配置成Spring管理的bean
@Component
//设置当前类为切面类类
@Aspect
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());
}
}
第六步:spring核心配置文件,加启动aspect的注解
@Configuration
@ComponentScan("com.itheima")
//开启注解开发AOP功能
@EnableAspectJAutoProxy
public class SpringConfig {
}
//通知类必须配置成Spring管理的bean
@Component
//设置当前类为切面类类
@Aspect
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());
}
}
第五步:切面连接切入点和通知
//通知类必须配置成Spring管理的bean
@Component
//设置当前类为切面类类
@Aspect
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());
}
}
第六步:spring核心配置文件,加启动aspect的注解
@Configuration
@ComponentScan("com.itheima")
//开启注解开发AOP功能
@EnableAspectJAutoProxy
public class SpringConfig {
}
@Configuration
@ComponentScan("com.itheima")
//开启注解开发AOP功能
@EnableAspectJAutoProxy
public class SpringConfig {
}



