(转)Spring 实现AOP是依赖JDK动态代理和CGLIB代理实现的。

Spring实现AOP是依赖JDK动态代理和CGLIB代理实现的。

以下是JDK动态代理和CGLIB代理简单介绍

JDK动态代理:其代理对象必须是某个接口的实现,它是通过在运行期间创建一个接口的实现类来完成对目标对象的代理。

CGLIB代理:实现原理类似于JDK动态代理,只是它在运行期间生成的代理对象是针对目标类扩展的子类。CGLIB是高效的代码生成包,底层是依靠ASM(开源的java字节码编辑类库)操作字节码实现的,性能比JDK强。

1)定义接口:

package com.beckham.dao;  
import com.beckham.model.Person;  
public interface PersonDAO {  
    public void add(Person person) ;  
}

2)实现接口:

package com.beckham.daoimp;  
import com.beckham.dao.PersonDAO;  
import com.beckham.model.Person;  
public class PersonDAOImp implements PersonDAO {  
    public void add(Person person) {  
        System.out.println(person.getName());  
    }  
}

3)服务层:

package com.beckham.service;  
import com.beckham.dao.PersonDAO;  
import com.beckham.model.Person;  
public class PersonService {  
    //注入personDAO   
    private PersonDAO personDAO ;  
      
    public PersonDAO getPersonDAO() {  
        return personDAO;  
    }  
    public void setPersonDAO(PersonDAO personDAO) {  
        this.personDAO = personDAO;  
    }  
    //调用personDAOImp里面的方法   
    public void add(Person person) {  
        personDAO.add(person) ;  
    }  
}

4)切面类:

package com.beckham.aop;  
public class LogHandler {  
    public void before(){  
        System.out.println("方法开始.......");  
    }  
    public void after(){  
        System.out.println("方法结束.......");  
    }  
}

5)XML配置切面

<?xml version="1.0" encoding="UTF-8"?>  
<!--  
    Application context definition for PetClinic on JPA.  
-->  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="  
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd   
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd   
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">   
    <bean id="person" class="com.beckham.model.Person"  
        scope="prototype" />  
    <bean id="personDAO" class="com.beckham.daoimp.PersonDAOImp" />  
    <bean id="personService"  
        class="com.beckham.service.PersonService">  
        <property name="personDAO" ref="personDAO"></property>  
    </bean>  
    <bean id="user" class="com.beckham.model.User" />  
    <bean id="userDAO" class="com.beckham.daoimp.UserDAOImp" />  
    <bean id="userServiceInterfaceImpl"  
        class="com.beckham.service.UserServiceInterfaceImpl">  
        <property name="userDAO" ref="userDAO"></property>  
    </bean>  
  
    <!-- AOP配置 -->  
    <bean id="loghandler" class="com.beckham.aop.LogHandler" />  
    <aop:config>  
        <!-- 配置切入点 -->  
        <aop:pointcut id="logCut"  
            expression="execution(* com.beckham.daoimp.PersonDAOImp.*(..))" />  
        <!-- 引用该切面类 -->  
        <aop:aspect id="aspect" ref="loghandler">  
            <aop:before method="before" pointcut-ref="logCut" />  
            <aop:after method="after" pointcut-ref="logCut" />  
        </aop:aspect>  
    </aop:config>  
    <!-- AOP配置 -->  
    <bean id="timehandler" class="com.beckham.aop.TimeHandler" />  
    <aop:config>  
        <!-- 配置切入点 -->  
        <aop:pointcut id="timeCut"  
            expression="execution(* com.beckham.daoimp..*(..))" />  
        <!-- 引用该切面类 -->  
        <aop:aspect id="timAspect" ref="timehandler">  
            <aop:before method="before" pointcut-ref="timeCut" />  
            <aop:after method="after" pointcut-ref="timeCut" />  
        </aop:aspect>  
    </aop:config>  
</beans>

6)测试:

package com.test;  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
import com.beckham.model.Person;  
import com.beckham.model.User;  
import com.beckham.service.PersonService;  
import com.beckham.service.UserServiceInterface;  
public class SpringTest {  
    /** 
     * beckham Dec 28, 2009 1:21:09 PM 
     */  
    public static void main(String[] args) {  
        personManager();  
    }  
    public static void personManager() {  
        ApplicationContext ctx = new ClassPathXmlApplicationContext(  
                "applicationContext.xml");  
        Person p = (Person) ctx.getBean("person");  
        p.setName("张三");  
        PersonService ps = (PersonService) ctx.getBean("personService");  
        ps.add(p);  
    }  
    public static void userManager() {  
        ApplicationContext ctx = new ClassPathXmlApplicationContext(  
                "applicationContext.xml");  
        User u = (User) ctx.getBean("user");  
        u.setName("张三");  
        UserServiceInterface ps = (UserServiceInterface) ctx.getBean("userServiceInterfaceImpl");  
        ps.sayHello(u.getName());  
    }  
}

测试结果:

方法开始.......

1262678458703

张三

1262678458703

方法结束.......

相关推荐