spring 实现AOP是依赖JDK动态代理和CGLIB代理实现的。
以下是JDK动态代理和CGLIB代理简单介绍
    JDK动态代理:其代理对象必须是某个接口的实现,它是通过在运行期间创建一个接口的实现类来完成对目标对象的代理。
    CGLIB代理:实现原理类似于JDK动态代理,只是它在运行期间生成的代理对象是针对目标类扩展的子类。CGLIB是高效的代码生成包,底层是依靠ASM(开源的Java字节码编辑类库)操作字节码实现的,性能比JDK强。

1)定义接口:

view plaincopy to clipboardprint?
package com.beckham.dao;   
import com.beckham.model.Person;   
public interface PersonDAO {   
    public void add(Person person) ;   
}  
package com.beckham.dao;
import com.beckham.model.Person;
public interface PersonDAO {
 public void add(Person person) ;
}

2)实现接口:

view plaincopy to clipboardprint?
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());   
    }   
}  
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)服务层:

view plaincopy to clipboardprint?
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) ;   
    }   
}  
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)切面类:

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

5)XML配置切面

view plaincopy to clipboardprint?
<?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>  
<?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)测试:

view plaincopy to clipboardprint?
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());   
    }   
}  
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

方法结束.......

spring AOP原理的更多相关文章

  1. 面试问烂的 Spring AOP 原理、SpringMVC 过程(求求你别问了)

    Spring AOP ,SpringMVC ,这两个应该是国内面试必问题,网上有很多答案,其实背背就可以.但今天笔者带大家一起深入浅出源码,看看他的原理.以期让印象更加深刻,面试的时候游刃有余. Sp ...

  2. spring ioc 原理 spring aop原理

    大家一直都说spring的IOC如何如何的强大,其实我倒觉得不是IOC如何的强大,说白了IOC其实也非常的简单.我们先从IOC说起,这个概念其实是从我们平常new一个对象的对立面来说的,我们平常使用对 ...

  3. spring aop原理分析

    持续更新... aop跟java代理模式有关. java.lang.reflect.Proxy java.lang.reflect.InvocationHandler 工厂模式用到java反射. ao ...

  4. Spring AOP原理(续)

    十二.AOP 1. 说出Spring的通知类型有哪些? spring共提供了五种类型的通知: 通知类型 接口 描述 Around 环绕通知 org.aopalliance.intercept.Meth ...

  5. spring aop 原理学习

    @EnableAspectJAutoProxy: @Import(AspectJAutoProxyRegistrar.class) 实际是创建了一个以org.springframework.aop.c ...

  6. Spring Boot -- Spring AOP原理及简单实现

    一.AOP基本概念 什么是AOP,AOP英语全名就是Aspect oriented programming,字面意思就是面向切面编程.面向切面的编程是对面向对象编程的补充,面向对象的编程核心模块是类, ...

  7. spring aop原理和实现

    一.aop是什么 1.AOP面向方面编程基于IoC,是对OOP的有益补充: 2.AOP利用一种称为"横切"的技术,剖解开封装的对象内部,并将那些影响了 多个类的公共行为封装到一个可 ...

  8. 【spring 注解驱动开发】Spring AOP原理

    尚学堂spring 注解驱动开发学习笔记之 - AOP原理 AOP原理: 1.AOP原理-AOP功能实现 2.AOP原理-@EnableAspectJAutoProxy 3.AOP原理-Annotat ...

  9. Spring AOP 原理的理解

    >AOP基本概念 1)通知(Advice):织入到目标类连接点上的一段程序代码.通知分为五种类型: - Before:在方法被调用之前调用 - After:在方法完成后调用通知,无论方法是否执行 ...

  10. Spring AOP原理及拦截器

    原理 AOP(Aspect Oriented Programming),也就是面向方面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP将应用系统分为两部分,核心业务逻辑(Core bu ...

随机推荐

  1. awk内引用shell变量【自己手动加精】

    题目 [root@localhost ~]# cat 1.txt iii sss ddd 执行命令 [root@localhost ~]# A=0 [root@localhost ~]# awk '{ ...

  2. MACOS关闭指定端口

    因为用IDEA写项目的时候,有的时候结束Jetty导致端口没有释放,所以会出现占用的情况. MacOS结束端口占用进程的命令,和Linux的一样.先执行如下命令: lsof -i:8080 会有类似下 ...

  3. JDK并发包

    JDK5之后引进了并发包java.util.concurrent,让并发的开发更加可控,更加简单.所以有必要好好学习下,下面从同步控制.并发容器.线程池三部分来详细了解它. 1. 各种同步控制工具的使 ...

  4. 关于CSS样式优先级学习心得

    1.未重复时候,只要有都有格式显示 2.重复时,看权值: 权值:标签 1 <类10< ID 100 PS:(*权值 > 继承(表格属性一般无法继承,有些浏览器也不支持表格继承父标签) ...

  5. MongoDB--初始

    指定启动目录,以服务形式启动 Mongod --dbpath=XXXXXX --logpath=XXXXXXXX --logappend --serviceName "XXXXX" ...

  6. An abandoned sentiment from past

    An abandoned sentiment from past time limit per test 1 second memory limit per test 256 megabytes in ...

  7. Web 前端代码规范

    Web 前端代码规范 最后更新时间:2017-06-25 原始文章链接:https://github.com/bxm0927/web-code-standards 此项目用于记录规范的.高可维护性的前 ...

  8. 前端UI组件复用工具

    "懒"是第一生产力. 代码复用总是程序员喜闻乐见的,前端组件化的最终目的就是复用,今天我们就将深入探讨如何实现UI组件的复用. 通常我们所说的组件往往是包含业务逻辑的前端组件,而这 ...

  9. Let's Encrypt 免费SSL证书

    Let's Encrypt免费又好用的证书,废话不多说.    假设我的域名为:163.org   1.克隆代码 git clone https://github.com/letsencrypt/le ...

  10. Matlab: 作图

    控制图的大小 figure('position',[x0,y0,dx,dy]); figure(fig number); 显示图例 legend('leg1','leg2') depend on ho ...