Spring简述二

其他Spring相关文章:
Spring和jdbctemplate、mybatis整合
Spring简述一

AOP面向切面(底层是动态代理实现的)

作用:
不通过修改原代码来实现拓展功能。
AOP采用了横向抽取机制,代替传统纵向继承体系,来减少代码量
适用场景:
事务管理,缓存,性能监控,安全检查
纵向继承机制:
第一种实现方式:在有接口的情况下,使用JDK自带的动态代理实现——InvocationHander的实现类,实现invoc方法;
第二种实现方式:在没有接口的情况下,使用CGlib的动态代理实现——通过扩展一个子类,拦截父类方法,实现动态代理;

AOP术语介绍:

连接点(joinpoint):指的是被拦截的点,在spring中指的是方法,类中的方法可以被增强(新增加功能)
切入点(pointcut):指的是对链接点进行拦截的定义(类中的很多方法可以被增强,实际被增强的方法被增强的叫做切入点)
增强/通知(Advice):增强值得是拦截到PointCut点之后要做的事情称之为增强(新增功能称为增强)
前置通知:执行方法之前进行的添加
后置通知:执行方法之后进行的添加
环绕通知:前置通知和后置通知加在一起,即在方法执行的前后都进行通知
最终通知:在后置通知之后执行
异常通知:在异常的情况下执行
切面(Aspect):切入点和通知的结合(将增强应用到切入点的过程)

Spring中AOP的使用

在Spring中是通过Aspectj实现AOP功能的Aspectj不属于Spring,是一个单独的面向切面的框架,Spring是需要结合Aspectj整合完成Aop的功能。用Aspectj实现AOP有两种方式——配置方式和注解方式

配置方式使用步骤

1、引入AOP依赖

<!--AOP相关jar包-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>4.1.7.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.6.0</version>
</dependency>
<dependency>
  <groupId>aopalliance</groupId>
  <artifactId>aopalliance</artifactId>
  <version>1.0</version>
</dependency>

2、创建配置文件,引入AOP约束条件 通过execution函数,可以定义切点的方法切入
1、切入点:实际增强的方法
2、常用的表达式:
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
(1)execution(* com.bean.Book.show(…)) 表类里面的某一个方法
(2)execution(* com.bean.Book.(…)) 表类某个包里类所有方法
(3)execution(
.(…)) 表示所有

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">

</beans> 

<!--配置AOP操作-->
<aop:config>
    <!--
    配置切入点
    实际被增强的方法称之为切入点
    expression属性:通过execution函数配置切入点
    -->
    <aop:pointcut id="pointcut1" expression="execution(* com.bean7.Person.add(..))"/>

    <!--
    配置切面
    将增强应用到切入点的过程
    aop:before标签:前置通知
    aop:after标签:后置通知
    aop:around标签:环绕通知
    aop:after-returning标签:最终通知
    aop:after-throwing标签:异常通知

    -->
    <aop:aspect ref="log">
        <!--配置前置增强-->
        <!--<aop:before method="before" pointcut-ref="pointcut1"/>-->
        <aop:around method="around" pointcut-ref="pointcut1"/>
    </aop:aspect>

</aop:config>

3、创建代增强类

public class Person {
    public void add() {
        //添加日志功能
        System.out.println("Person.add...");
    }
}

4、增强类实现

/前置通知
    public void before(){
        System.out.println("前置通知");
    }

    //环绕通知
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕通知前");
        //指定真正执行的时机
        joinPoint.proceed();
        System.out.println("环绕通知后");
    }
   

注解方式使用步骤
1、打开AOP的注解开关<aop>

<!--打开AOP的注解开发-->
<aop:aspectj-autoproxy/>

2、在增强类上添加相关的注解

@Aspect
public class Log {
    //前置通知
    @Before(value = "execution(* com.bean7.Person.add(..))")
    public void before(){
        System.out.println("前置通知");
    }
}

Spring其他内容未完待续。。。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值