Spring知识点小结(三)


一、aop的简介


aop:面向切面编程
aop是一种思想,面向切面编程思想,Spring内部提供了组件对aop进行实现
aop是在运行期间使用动态代理技术实现的思想
aop是oop延续
面向过程:C
面向对象:Java
面向切面:AOP
面向服务架构:SOA

aop的底层实现:动态代理
基于JDK:目标对象必须有接口(proxy是接口的实现)
基于cglib:目标对象不需要有接口(proxy是目标子类)


aop的基本概念、术语
目标对象(target)
代理对象(proxy)
连接点(joinpoint): 可以被增强的方法
切(入)点(pointcut): 真正可以被增强的方法
增强/通知(advice):功能增强的方法
切面(aspect):切点+增强
织入(weaver):将切点和增强结合的过程

二、基于xml方式的aop配置

开发步骤:
1、导入aop的jar
spring-aop-4.2.4.RELEASE spring的aop核心包
spring-aspects-4.2.4.RELEASE spring的切面包
com.springsource.org.aopalliance-1.0.0 aop联盟包
com.springsource.org.aspectj.weaver-1.6.8.RELEASE aspectj的织入包

2、定义目标(目标内部有切点)、定义切面(增强在切面内部)
public class Target implements TargetInterface
public class MyAspect
3、配置目标和切面到spring容器中
<!-- 配置目标 -->
<bean id="target" class="com.ghdu.aop.Target"></bean>
<!-- 配置切面 -->
<bean id="myAspect" class="com.ghdu.aop.MyAspect"></bean>
4、配置aop的织入
导入aop的命名空间

xml配置代码:
<!-- 配置目标 -->
<bean id="target" class="com.ghdu.aop.Target"></bean>
<!-- 配置切面 -->
<bean id="myAspect" class="com.ghdu.aop.MyAspect"></bean>

<!-- 配置aop的织入 -->
<aop:config>
<!-- 指定切面对象是谁 -->
<aop:aspect ref="myAspect">
<!-- 切面=切点+增强 -->
<!-- 细节2:切点表达式的写法
expression写法
示例:execution(public void com.ghdu.aop.Target.show())
语法:execution([访问修饰符] 返回值 包.类.方法(参数类型列表))

注意:其中
访问修饰符可以省略
返回值、包、类、方法 可以使用*作为通配符代表任意
参数类型列表 可以使用..作为通配符代表任意

示例:
* com.ghdu.service.impl.CustomerServiceImpl.*(..) CustomerServiceImpl的任意方法
* com.ghdu.service.impl.*.*(..) 对impl包下的任意类的任意方法
* com.ghdu.service.*.*.*(..) 对service包下的任意子包下的任意类的任意方法
* com.ghdu.service..*.*.*(..) 对service包下的任意后代包下的任意类的任意方法


-->
<!-- <aop:pointcut expression="execution(public void com.ghdu.aop.Target.*(..))" id="myPointcut"/>
<aop:pointcut expression="execution(public void com.ghdu.aop.Target.show(..))" id="myPointcut2"/> -->
<!-- 细节1:aop的增强/通知有哪些
aop:before 前置增强
aop:after-returning 后置增强
aop:around 环绕增强
aop:after-throwing 异常抛出增强
aop:after 最终增强
-->
<!-- <aop:before method="before" pointcut-ref="myPointcut"/>
<aop:after-returning method="afterReturning" pointcut-ref="myPointcut"/>
<aop:around method="around" pointcut-ref="myPointcut"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="myPointcut"/>
<aop:after method="after" pointcut-ref="myPointcut2"/> -->

<aop:around method="around" pointcut="execution(* com.ghdu.aop.*.*(..))"/>
<aop:after method="after" pointcut="execution(* com.ghdu.aop.*.*(..))"/>

</aop:aspect>
</aop:config>

三、基于注解方式的aop配置

开发步骤:
1、导入aop的jar
spring-aop-4.2.4.RELEASE spring的aop核心包
spring-aspects-4.2.4.RELEASE spring的切面包
com.springsource.org.aopalliance-1.0.0 aop联盟包
com.springsource.org.aspectj.weaver-1.6.8.RELEASE aspectj的织入包

2、定义目标(目标内部有切点)、定义切面(增强在切面内部)
public class Target implements TargetInterface
public class MyAspect
3、配置目标和切面到spring容器中
@Component("target")
public class Target implements TargetInterface

@Component("myAspect")
public class MyAspect



4、配置aop的织入
@Component("myAspect")
//<aop:aspect ref="myAspect">
@Aspect
public class MyAspect {

//<aop:before method="before" pointcut="execution(* com.ghdu.aop.*.*(..))"/>
//@Before("execution(* com.ghdu.anno.*.*(..))")
public void before(){
System.out.println("前置增强....");
}

注意:
在配置文件中配置aop的自动代理
<!-- 开启aop的自动代理 -->
<aop:aspectj-autoproxy/>

  

全注解: 在原始注解的基础上,创建配置类

  //声明
  @Configuration

  //扫描包
  @ComponentScan("com.gudf.allanno")

  //设置aop自动代理
  @EnableAspectJAutoProxy
  public class SpringConfiguration {
  }

有关aop的知识扩充

aop的底层实现

aop、spring、aspectj三者的关系

动态代理

全盘委托机制

aop应用场景

方法性能测试
日志控制
事务控制......

优质内容筛选与推荐>>
1、9.13笔记
2、harbor 镜像删除与空间回收
3、序列化和初始化
4、【poj 1182】 食物链
5、python之学习摘抄-python 中变量的命名规范


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号