AOP(面向切面变成):不改变原有代码的情况下,对代码进行功能添加
1.一些概念
抽取出的方法:通知
原始方法:成为连接点(可以是程序执行中的任意位置),对应原始的一个个方法
要追加功能的方法:定义为切入点,没有追加通知的就不是切入点
切面:通知和切入点对应的关系(在那个切入点上执行哪些通知)
囊括通知的类:通知类
2.小案例:
1.坐标
java
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
2.配置文件springConfig
java
package viki.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan("viki")
@EnableAspectJAutoProxy//告诉程序要用注解开发AOP
public class springConfig {
}
3.设置切入点,连接点(通知类)
java
@Component
@Aspect//声明
public class cutPointMethod {
@Pointcut("execution(void viki.Dao.DaoImp.bookDaoImp.updata())")
private void pc(){}//定义切入点
@Before("pc()")//连接点
public void time(){
System.out.println(System.currentTimeMillis());
}
}
4.一些类
java
import org.springframework.stereotype.Repository;
import viki.Dao.bookDao;
@Repository
public class bookDaoImp implements bookDao {
@Override
public void save() {
System.out.println(System.currentTimeMillis());
System.out.println("Bookdao save");
}
@Override
public void updata() {
System.out.println("BookDAOUPDATA");
}
}
3.AOP工作流程



4.切入点表达式

两种描述都可以


识别时可反向识别 ..未知包 **.**此处*一定是类或接口 . 此处*一定是方法()
例子:

给所有业务层查找方法增加AOP

4.AOP通知类型





当原方法有返回值时,使用Around的最后,需要抛出返回值,类型和原方法相同

1.设定obj类型返回值
2.使用ProceedingJoinPoint类型的参数实现对原始方法的调用
3.接受返回值

5.小案例(接口层万次效率)

1.在SpringConfig中打开AOP

2.创建切面类,切入点
PointCut("execute ( *给业务层所有方法加统计,返回值设置为* com.packages.services.*services.*(..)) ")

改良,加上具体是哪个方法

6.AOP通知获取数据
1.参数

可以实现,使用一个AOP,获取到参数时,判断或修改获取到的参数,实现参数合法化
2.返回值

3.异常

小总结:

7.总结



