zoukankan      html  css  js  c++  java
  • Spring课程 Spring入门篇 5-6 introductions应用

    课程链接:

    1    解析

    1.1  aop:declare-parents 标签简介

    1.2  标签使用样式

    2    代码演练

    2.1  introductions标签应用

    1    解析

    1.1  aop:declare-parents 标签简介

    允许一个切面声明一个实现指定接口的通知对象,并且提供了一个接口实现类来代表这些对象

    不同于aop:before等其他标签,元素声明该元素用于声明所匹配的类型拥有一个新的parent(父类)。所以测试类getBean后可以强转

    1.2  标签使用样式

     <aop:declare-parents types-matching=" com.imooc.aop.schema.advice.biz.*(..))"   <!--包-->
    
                implement-interface="com.imooc.aop.schema.advice.Fit"   <!--接口-->
    
                default-impl="com.imooc.aop.schema.advice.FitImpl"    <!-- 实现类-->  />  

    2    代码演练

    2.1  introductions标签应用

    实体类:

    package com.imooc.aop.schema.advice.biz;
    
    public class AspectBiz {
        
        public void biz(){
            System.out.println("MoocAspect biz");
    //        throw new RuntimeException();
        }
        
        public void init(String bizName,int times){
            System.out.println("AspectBiz init:"+ bizName + " "+times);
        }
    }

    测试类:

    package com.imooc.test.aop;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.BlockJUnit4ClassRunner;
    
    import com.imooc.aop.schema.advice.Fit;
    import com.imooc.aop.schema.advice.biz.AspectBiz;
    import com.imooc.test.base.UnitTestBase;
    
    @RunWith(BlockJUnit4ClassRunner.class)
    public class TestAOPSchemaAdvice extends UnitTestBase {
        
        public TestAOPSchemaAdvice(){
            super("classpath:spring-aop-schema-advice.xml");
        }
        
        @Test
        public void testBiz(){
            AspectBiz aBiz = super.getbean("AspectBiz");
            aBiz.biz();
        }
        
        @Test
        public void testInit(){
            AspectBiz aBiz = super.getbean("AspectBiz");
            aBiz.init("moocService",3);
        }
    
        @Test
        /**
         * 配合spring-aop-schema-advice.xml 使用,调的是接口类,但是实际使用的是实现类
         */
        public void testFit(){
            Fit fit = (Fit)super.getbean("AspectBiz");
            fit.filter();
        }
    }

    配置文件:

    <?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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
    
    
    <bean id = "moocAspect" class = "com.imooc.aop.schema.advice.MoocAspect"></bean>
    <bean id = "AspectBiz" class = "com.imooc.aop.schema.advice.biz.AspectBiz"></bean>
    
    <aop:config>
        <aop:aspect  id="moocAspectAOP" ref="moocAspect">
    <!--     声明切入点:从哪里开始执行 -->
    <!--     执行com.imooc.aop.schema.advice.biz包下的所有Biz结尾的java类中的所有方法时 -->
    <!--         <aop:pointcut expression="execution(* com.imooc.aop.schema.advice.biz.*Biz.*(..))" id="moocPointCut"/> -->
    <!--         <aop:before method="before" pointcut-ref="moocPointCut"/> -->
    <!--         <aop:after-returning method="afterreturning" pointcut-ref="moocPointCut"/> -->
    <!--         <aop:after-throwing method="throwing" pointcut-ref="moocPointCut"/> -->
    <!--         <aop:after method="after" pointcut-ref="moocPointCut"/> -->
    <!--         <aop:around method="around" pointcut-ref="moocPointCut"/> -->
    
    <!--         <aop:around method="aroundInit" pointcut="execution(* com.imooc.aop.schema.advice.biz.AspectBiz.init(String,int)) and args(bizName,times)"/> -->
            
    <!--             type-matching 要匹配的是这个包下的所有方法,(后便注释是我自己解释的,看不懂可以不看,报错了可以仔细想一下)
    注意:type-matching 一定要匹配这个包下的所有类(包含我要执行的接口,实现类,以及super.getBean到的类) -->

     <aop:declare-parents types-matching=" com.imooc.aop.schema.advice.biz.*(..))"

                implement-interface="com.imooc.aop.schema.advice.Fit" 

                default-impl="com.imooc.aop.schema.advice.FitImpl"/>

     

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

    接口:

    package com.imooc.aop.schema.advice;
    
    public interface Fit {
        void filter();
    }

    实现类:

    package com.imooc.aop.schema.advice;
    
    public class FitImpl implements Fit {
    
        @Override
        public void filter() {
            System.out.println("FitImpl 执行中");
        }
    
    }

    打印日志:

    四月 20, 2019 9:50:09 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6e1d4150: startup date [Sat Apr 20 09:50:09 CST 2019]; root of context hierarchy
    四月 20, 2019 9:50:09 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from class path resource [spring-aop-schema-advice.xml]
    FitImpl 执行中
    四月 20, 2019 9:50:09 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
    信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@6e1d4150: startup date [Sat Apr 20 09:50:09 CST 2019]; root of context hierarchy
  • 相关阅读:
    .netcore利用DI实现级联删除
    识别手写数字增强版100%
    嗨!请查收这道有趣的面试题
    理解TCP/IP协议栈之HTTP2.0
    基于Redis的分布式锁和Redlock算法
    从生日悖论谈哈希碰撞
    Redis面试热点工程架构篇之数据同步
    Redis面试热点之底层实现篇(续)
    saltstack安装+基本命令
    25个iptables常用示例
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/10740000.html
Copyright © 2011-2022 走看看