spring AOP原理

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring AOP原理相关的知识,希望对你有一定的参考价值。

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原理的主要内容,如果未能解决你的问题,请参考以下文章

java编程,spring里ioc和aop用啥原理实现的

Spring框架AOP的使用及个人对底层原理的理解

spring aop 原理

spring ioc aop 的原理是啥啊,面试时我怎么回答才好呢?说说面试技巧好吗

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

Spring框架系列 - Spring AOP实现原理详解之AOP切面的实现