Spring源码深度解析:学习笔记

1、AOP:html

         补充:spring

                使用Spring AOP,要成功运行起代码,只用Spring提供给开发者的jar包是不够的,须要额外上网下载两个jar包:app

                    一、aopalliance.jar测试

                    二、aspectjweaver.jarthis

网上看到一个写的很好的博客,博文地址:http://www.cnblogs.com/xrq730/p/4919025.html代理

2、AOP核心概念xml

一、横切关注点htm

对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点对象

二、切面(aspect)blog

类是对物体特征的抽象,切面就是对横切关注点的抽象

三、链接点(joinpoint)

被拦截到的点,由于Spring只支持方法类型的链接点,因此在Spring中链接点指的就是被拦截到的方法,实际上链接点还能够是字段或者构造器

四、切入点(pointcut)

对链接点进行拦截的定义

五、通知(advice)

所谓通知指的就是指拦截到链接点以后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类

六、目标对象

代理的目标对象

七、织入(weave)

将切面应用到目标对象并致使代理对象建立的过程

八、引入(introduction)

在不修改代码的前提下,引入能够在运行期为类动态地添加一些方法或字段。

3、代码演示:

       一、 准备bean:

                

package cn.lsp.bean;

/**
 * @author lisp
 * @ClassName TestBean
 * @Date 2019/5/22 12:18
 */
public class TestBean {
   private String testStr = "testStr";

   public String getTestStr() {
      return testStr;
   }

   public void setTestStr(String testStr) {
      this.testStr = testStr;
   }

   public void test() {
      System.out.println("test");
   }
}

    二、切面类

        

package cn.lsp.bean;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * @author lisp
 * @ClassName AspectJTest
 * @Date 2019/5/22 12:20
 */
@Aspect
public class AspectJTest {

   @Pointcut("execution(* *.test(..))")
   public void test(){

   }

   @Before("test()")
   public void beforeTest(){
      System.out.println("beforeTest");
   }

   @After("test()")
   public void afterTest(){
      System.out.println("afterTest");
   }

   @Around("test()")
   public Object arountTest(ProceedingJoinPoint p) {
      System.out.println("before1");
      Object object = null;
      try {
         object = p.proceed();
      } catch (Throwable throwable) {
         throwable.printStackTrace();
      }
      System.out.println("after1");
      return object;
   }

}

    三、配置文件  application-context.xml

            

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans.xsd
             http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd
   ">
    <!-- spring 是否支持注解的 AOP 是由一个配置文件控制的,也就是 <aop:aspectj-autoproxy/> 当配置文件中声明了这句配置的时候
        spring就会支持注解的 AOP (解析 AOP 的源码能够从这切入) -->
   <aop:aspectj-autoproxy/>
   <bean id="test" class="cn.lsp.bean.TestBean"/>
   <bean class="cn.lsp.bean.AspectJTest"/>
</beans>

    四、启动测试类

        

package cn.lsp.bean;

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

/**
 * @author lisp
 * @ClassName AspecTest
 * @Date 2019/5/23 8:55
 */
public class AspecTest {
   public static void main(String[] args) {
      ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml");
      TestBean testBean = (TestBean) applicationContext.getBean("test");
      testBean.test();

   }
}
相关文章
相关标签/搜索