zoukankan      html  css  js  c++  java
  • 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应用场景

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

  • 相关阅读:
    PYTHON压平嵌套列表
    linux下IPTABLES配置详解
    Python面试必须要看的15个问题
    两个实用的Python的装饰器
    Python的16个“坑”
    python实现不可修改的常量
    51nod-1322: 关于树的函数
    51nod-1310: Chandrima and XOR
    51nod-1296: 有限制的排列
    51nod-1277: 字符串中的最大值
  • 原文地址:https://www.cnblogs.com/itworkerlittlewrite/p/9486069.html
Copyright © 2011-2022 走看看