AOP的作用:
日志的记录
权限的校验
性能的检测(查看某个方法执行了多长时间)
事务的管理
AOP这种思想是由AOP联盟组织提出来的一种思想,spring是把这种思想实现的最好的框架之一
Jdk的动态代理:只能对有接口的实现类进行增强
Cglib的动态代理:可以对类进行增强,这个类不需要实现任何接口
第一步:创建maven工程,解决两个问题
第二步:导入jar包
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.2.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>4.2.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>4.2.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-expressionartifactId>
<version>4.2.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>4.2.4.RELEASEversion>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.1.0version>
<scope>providedscope>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>4.2.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>4.2.4.RELEASEversion>
<scope>testscope>
dependency>
<dependency>
<groupId>aopalliancegroupId>
<artifactId>aopallianceartifactId>
<version>1.0version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.6.8version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>4.2.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>4.2.4.RELEASEversion>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.1version>
<configuration>
<source>1.8source>
<target>1.8target>
<encoding>utf-8encoding>
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();
}