AOP的作用:
日志的记录
权限的校验
性能的检测(查看某个方法执行了多长时间)
事务的管理
AOP这种思想是由AOP联盟组织提出来的一种思想,spring是把这种思想实现的最好的框架之一
Aop的两种实现方式:
Jdk的动态代理:只能对有接口的实现类进行增强
Cglib的动态代理:可以对类进行增强,这个类不需要实现任何接口
第一步:创建maven工程,解决两个问题
第二步:导入jar包
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- spring的注解开发必须要依赖的jar包 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.4.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
第一种方式:通过jdk的动态代理实现增强的功能
定义接口与实现类
定义我们的增强类
实现我们的代理类
public class PersonProxy implements InvocationHandler{
private PersonDao personDao;
public PersonProxy(PersonDao personDao){
this.personDao = personDao;
}
public PersonDao createPersonDaoProxy(){
/**
* 第一个参数:类加载器
* 第二个参数:所有实现的接口
* 第三个参数:invocationHandler
*
*/
PersonDao newProxyInstance = (PersonDao) Proxy.newProxyInstance(personDao.getClass().getClassLoader(), personDao.getClass().getInterfaces(), this);
return newProxyInstance;
}
/**
* 实现了invocationHandler接口之后就必须要实现一个方法,叫做invoke方法
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if(method.getName().equals("add")){
PersonStrong strong = new PersonStrong();
strong.checkPrivilege();
return method.invoke(personDao, args);
}
return method.invoke(personDao, args);
}
}
测试用例
/**
*
* @throws Exception
*/
@Test
public void getJdkProxy() throws Exception {
//接口指向实现类,多态
PersonDao dao = new PersonDaoImpl();
PersonProxy proxy = new PersonProxy(dao);
PersonDao createPersonDaoProxy = proxy.createPersonDaoProxy();
createPersonDaoProxy.add();
}