课程链接:

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

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. [Objective-C语言教程]基本语法(4)

    前面已经看到了Objective-C程序的基本结构,因此很容易理解Objective-C编程语言的其他基本构建块. Objective-C令牌 Objective-C程序由各种令牌组成,令牌可以是关键 ...

  2. XPath语法使用的一些心得

    XPath语法还是很强大的,支持一些函数和操作符,方便操作,但是因为版本的原因可能有的函数只能在XPath2(2007)里使用,而不能在XPath1(1999)中使用,比如函数 ends-with(s ...

  3. 2016级算法第二次上机-B.Bamboo的OS实验

    Bamboo的OS实验 分析 首先理解题意,要完成不同数量的不同命令,但是完成相同的命令之间必须有n个间隔,为使得时间最短,自然优先用其他命令来填充这n分钟的时间,由于数量少的命令可以用来填充空隙,所 ...

  4. NASA的10条代码编写原则

    NASA的10条代码编写原则 作者: Gerard J. Holzmann 来源: InfoQ 原文链接 英文原文:NASA's 10 Coding Rules for Writing Safety ...

  5. 剑指offer——面试题18.1:删除链表中重复的节点

    // 面试题18(二):删除链表中重复的结点 // 题目:在一个排序的链表中,如何删除重复的结点?例如,在图3.4(a)中重复 // 结点被删除之后,链表如图3.4(b)所示. #include &l ...

  6. zookeeper 数据节点的增删改查

    1.连接服务端 [root@localhost bin]# ./zkCli.sh -server 127.0.0.1:2181 Connecting to 127.0.0.1:2181 2018-05 ...

  7. node.js修改全局安装文件路径

    在进行 node.js 的开发过程中,我们需要下载大量的依赖模块,为了不让 c 盘的东西太过于散乱,可以通过修改node的配置参数,来修改node依赖的下载路径. 步骤: ①创建两个文件夹:node_ ...

  8. 文献综述十六:基于UML的中小型超市管理系统分析与设计

    一.基本信息 标题:基于UML的中小型超市管理系统分析与设计 时间:2016 出版源:Journal of Xiangnan University 文件分类:uml技术系统的研究 二.研究背景 开发一 ...

  9. (转)expect命令脚本语言介绍及生产实践

    原文:http://www.fblinux.com/?p=526 Expect介绍 expect是一个用来实现自动交互功能的软件套件,是用来实现自动和交互式任务程序进行通信,无需人的手工干预.比如SS ...

  10. js需要清楚的内存模型

    原型 原型重写 原型继承 组合方式实现继承 函数作用域链