Spring aop例子

2017-06-30  本文已影响0人  秦仙云

引入jar包

jar包管理

公共接口

public interface PersonDao {
    public void savePerson();
}

目标类

public class PersonDaoImpl implements PersonDao{
    public void savePerson() {
        // TODO Auto-generated method stub
        System.out.println("save person");
    }
}

切面

import java.lang.reflect.Method;

import org.junit.Test;

public class Transaction {
    public void beginTransaction(){
        System.out.println("begin transaction");
    }
    
    public void commit(){
        System.out.println("commit");
    }
}

Spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
   <!-- 
          导入目标类
          导入切面
          进行aop的配置
    -->
   <bean id="personDao" class="com.itheima11.spring.aop.xml.transaction.PersonDaoImpl"></bean>
   <bean id="transaction" class="com.itheima11.spring.aop.xml.transaction.Transaction"></bean>
   
   <aop:config>
        <!-- 
            切入点表达式
              符合切入点表达式的类要产生代理对象
              expression就是切入点表达式
              id 标示符
         -->
        <aop:pointcut 
            expression="execution(* com.itheima11.spring.aop.xml.transaction.PersonDaoImpl.*(..))" 
            id="perform"/>
        <!-- 
            ref属性指向切面
         -->
        <aop:aspect ref="transaction">
            <!-- 
                前置通知
                   在目标方法执行之前
             -->
            <aop:before method="beginTransaction" pointcut-ref="perform"/>
            <!-- 
                后置通知
             -->
            <aop:after-returning method="commit" pointcut-ref="perform"/>
        </aop:aspect>
   </aop:config>
</beans>

测试方法

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * aop的原理
 *     1、启动spring容器
 *     2、spring容器会为personDao和transaction实例化
 *     3、spring容器解析aop:config的配置
 *         当spring容器解析到切入点表达式的时候,就会把切入点表达式解析出来
 *            会让切入点表达式的类和spring容器中的类进行匹配
 *               如果匹配成功,则会为该对象创建代理对象
 *                  代理对象的方法形成=通知+目标方法
 *               如果匹配不成功,则会报错
 *     4、在客户端context.getBean时,如果当前的对象有代理对象,则返回代理对象
 *              如果没有代理对象返回对象的本身
 *     5、在spring内部会检查目标类有没有实现接口
 *           如果目标类实现了接口,则采用jdkproxy产生代理对象
 *           如果目标类没有实现接口,则采用cglibproxy产生代理对象
 *
 */
public class PersonDaoTest {
    @Test
    public void testSpring(){
        ApplicationContext context = 
                new ClassPathXmlApplicationContext("applicationContext.xml");
        PersonDao personDao = (PersonDao)context.getBean("personDao");
        personDao.savePerson();
    }
}

上一篇下一篇

猜你喜欢

热点阅读