课程链接:

1    解析

1.1  aop:declare-parents 标签简介

1.2  标签使用样式

2    代码演练

2.1  introductions标签应用

1    解析

1.1  aop:declare-parents 标签简介

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

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

1.2  标签使用样式

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

2    代码演练

2.1  introductions标签应用

实体类:

  1. package com.imooc.aop.schema.advice.biz;
  2.  
  3. public class AspectBiz {
  4.  
  5. public void biz(){
  6. System.out.println("MoocAspect biz");
  7. // throw new RuntimeException();
  8. }
  9.  
  10. public void init(String bizName,int times){
  11. System.out.println("AspectBiz init:"+ bizName + " "+times);
  12. }
  13. }

测试类:

  1. package com.imooc.test.aop;
  2.  
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.junit.runners.BlockJUnit4ClassRunner;
  6.  
  7. import com.imooc.aop.schema.advice.Fit;
  8. import com.imooc.aop.schema.advice.biz.AspectBiz;
  9. import com.imooc.test.base.UnitTestBase;
  10.  
  11. @RunWith(BlockJUnit4ClassRunner.class)
  12. public class TestAOPSchemaAdvice extends UnitTestBase {
  13.  
  14. public TestAOPSchemaAdvice(){
  15. super("classpath:spring-aop-schema-advice.xml");
  16. }
  17.  
  18. @Test
  19. public void testBiz(){
  20. AspectBiz aBiz = super.getbean("AspectBiz");
  21. aBiz.biz();
  22. }
  23.  
  24. @Test
  25. public void testInit(){
  26. AspectBiz aBiz = super.getbean("AspectBiz");
  27. aBiz.init("moocService",3);
  28. }
  29.  
  30. @Test
  31. /**
  32. * 配合spring-aop-schema-advice.xml 使用,调的是接口类,但是实际使用的是实现类
  33. */
  34. public void testFit(){
  35. Fit fit = (Fit)super.getbean("AspectBiz");
  36. fit.filter();
  37. }
  38. }

配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/aop
  8. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
  9.  
  10. <bean id = "moocAspect" class = "com.imooc.aop.schema.advice.MoocAspect"></bean>
  11. <bean id = "AspectBiz" class = "com.imooc.aop.schema.advice.biz.AspectBiz"></bean>
  12.  
  13. <aop:config>
  14. <aop:aspect id="moocAspectAOP" ref="moocAspect">
  15. <!-- 声明切入点:从哪里开始执行 -->
  16. <!-- 执行com.imooc.aop.schema.advice.biz包下的所有Biz结尾的java类中的所有方法时 -->
  17. <!-- <aop:pointcut expression="execution(* com.imooc.aop.schema.advice.biz.*Biz.*(..))" id="moocPointCut"/> -->
  18. <!-- <aop:before method="before" pointcut-ref="moocPointCut"/> -->
  19. <!-- <aop:after-returning method="afterreturning" pointcut-ref="moocPointCut"/> -->
  20. <!-- <aop:after-throwing method="throwing" pointcut-ref="moocPointCut"/> -->
  21. <!-- <aop:after method="after" pointcut-ref="moocPointCut"/> -->
  22. <!-- <aop:around method="around" pointcut-ref="moocPointCut"/> -->
  23.  
  24. <!-- <aop:around method="aroundInit" pointcut="execution(* com.imooc.aop.schema.advice.biz.AspectBiz.init(String,int)) and args(bizName,times)"/> -->
  25.  
  26. <!-- 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"/>

 

  1. </aop:aspect>
  2. </aop:config>
  3.  
  4. </beans>

接口:

  1. package com.imooc.aop.schema.advice;
  2.  
  3. public interface Fit {
  4. void filter();
  5. }

实现类:

  1. package com.imooc.aop.schema.advice;
  2.  
  3. public class FitImpl implements Fit {
  4.  
  5. @Override
  6. public void filter() {
  7. System.out.println("FitImpl 执行中");
  8. }
  9.  
  10. }

打印日志:

  1. 四月 20, 2019 9:50:09 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
  2. 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6e1d4150: startup date [Sat Apr 20 09:50:09 CST 2019]; root of context hierarchy
  3. 四月 20, 2019 9:50:09 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  4. 信息: Loading XML bean definitions from class path resource [spring-aop-schema-advice.xml]
  5. FitImpl 执行中
  6. 四月 20, 2019 9:50:09 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
  7. 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@6e1d4150: startup date [Sat Apr 20 09:50:09 CST 2019]; root of context hierarchy

Spring课程 Spring入门篇 5-6 introductions应用的更多相关文章

  1. Spring Boot -01- 快速入门篇(图文教程)

    Spring Boot -01- 快速入门篇(图文教程) 今天开始不断整理 Spring Boot 2.0 版本学习笔记,大家可以在博客看到我的笔记,然后大家想看视频课程也可以到[慕课网]手机 app ...

  2. Spring实践系列-入门篇(一)

    本文主要介绍了在本地搭建并运行一个Spring应用,演示了Spring依赖注入的特性 1 环境搭建 1.1 Maven依赖 目前只用到依赖注入的功能,故以下三个包已满足使用. <properti ...

  3. Spring课程 Spring入门篇 6-2 ProxyFactoryBean及相关内容(上)

    1 解析 1.1 类的方式实现各种通知需要实现的接口 1.2 创建Spring aop代理的优点及方法 1.3 代理控制切入点和通知的顺序的代码实现(具体完全实现,见代码2.1) 1.4 代理方式选择 ...

  4. Spring课程 Spring入门篇 7-3 advice扩展

    课程链接: 1 解析 1.1 advice中aspect 切面传参 1.2 通知参数名称——argNames属性, 参数为 JoinPoint.ProceedingJoinPoint.JoinPoin ...

  5. Spring课程 Spring入门篇 7-2 Advice定义及实例

    1 解析 1.1 通知:after和afterreturning的区别 1.2 @RunWith 是什么? 2 代码演练 2.1 注解方式配置通知的两种方式 2.2 异常通知 2.3 非异常通知 1 ...

  6. Spring课程 Spring入门篇 6-3 ProxyFactoryBean及相关内容(下)

    1 解析 1.1 使用global advisors demo 1.2 jdk代理和cglib代理的选择 1.3 如何强制使用CGLIB实现AOP? 1.4 JDK动态代理和CGLIB字节码生成的区别 ...

  7. Spring课程 Spring入门篇 5-2 配置切面aspect

    本节主要讲了在xml中配置切面的demo 1 解析 1.1 配置切面xml 1.2 配置切面xml 1.3 问:什么是动态代理? 2 代码演练 2.1 配置切面xml 1 解析 1.1 配置切面xml ...

  8. Spring课程 Spring入门篇 4-9 Spring bean装配之对jsr支持的说明

    1 解析 1.1 疑问:2.2去掉@resource注解,为什么不能赋值?不是有set方法了吗? 1.2 @resource注解版本支持 1.3 没有显式指定@resource的那么,默认名称从何获得 ...

  9. Spring课程 Spring入门篇 4-8 Spring bean装配之基于java的容器注解说明--基于泛型的自动装配

    1 解析 1.1 什么是泛型? 1.2 泛型有什么作用? 1.3 泛型装配样式? 2 代码演练 2.1 泛型应用 1 解析 1.1 什么是泛型? Java泛型设计原则:只要在编译时期没有出现警告,那么 ...

随机推荐

  1. 三,PHP中错误日志display_errors与error_reporting配置

    1,display_errors display_errors 错误回显,一般常用语开发模式,但是很多应用在正式环境中也忘记了关闭此选项.错误回显可以暴露出非常多的敏感信息,为攻击者下一步攻击提供便利 ...

  2. Python——深拷贝和浅拷贝

    深拷贝.浅拷贝 1. 浅拷贝 浅拷贝是对于一个对象的顶层拷贝 import copy a = [[1, 2], 3] b = copy.copy(a) print(id(a)) print(id(b) ...

  3. Oracle数据库count的一些操作

    --统计数量 select count(*) from table; --统计某一列的数量(去空) select count(col) from table; --统计某一列的值大于或小于另一个值的数 ...

  4. QQ第三方登陆示例

    先上图 若想实现QQ登录,需要成为QQ互联的开发者,审核通过才可实现.注册方法可参考链接http://wiki.connect.qq.com/%E6%88%90%E4%B8%BA%E5%BC%80%E ...

  5. 关于使用Google Analyse导入库文件编译出错的解决办法.

    (最新解决办法):因为要通过cocoapod链接库,那么直接在Podfile上面加上 pod 'GoogleAnalytics-iOS-SDK', '~> 3.0.9',然后在pod insta ...

  6. 2.ajax+servlet实现注册时用户名验证

    效果: 精灵图(来源:从百度注册中download下来的): userVerify.jsp <%@ page language="java" contentType=&quo ...

  7. 使用百度地图API查地理坐标

    在网络编程中,我们会和API打交道.那么,什么是API?如何使用API呢?本文分享了一下我对API的理解以及百度地图API的使用. API是"Application Programming ...

  8. vue嵌套路由-query传递参数(三)

    在嵌套路由中我们经常会遇到父路由向子路由里面传递参数,传递参数有两种方法,通过 query 或者 params index.html <div id="app"> &l ...

  9. golang (3) 编译不同的平台文件

    Golang 支持在一个平台下生成另一个平台可执行程序的交叉编译功能. Mac下编译Linux, Windows平台的64位可执行程序: CGO_ENABLED=0 GOOS=linux GOARCH ...

  10. Salesforce LINKS

    Salesforce的二次开发平台的多租户架构 http://blog.talkingdata.net/?p=4807 Salesforce 简介 https://www.cnblogs.com/ch ...