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

一、AOP:

         补充:

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

                    1、aopalliance.jar

                    2、aspectjweaver.jar

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

二、AOP核心概念

1、横切关注点

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

2、切面(aspect)

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

3、连接点(joinpoint)

被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器

4、切入点(pointcut)

对连接点进行拦截的定义

5、通知(advice)

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

6、目标对象

代理的目标对象

7、织入(weave)

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

8、引入(introduction)

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

三、代码演示:

       1、 准备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");
   }
}

    2、切面类

        

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;
   }

}

    3、配置文件  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>

    4、启动测试类

        

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();

   }
}

 

 

转载于:https://my.oschina.net/u/3211737/blog/3052923

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值