spring的aop底层的实现方式

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();

}

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值