spring aop 之xml
1、类库

2.aop概念

一个切面可以有多个切点
3.在方法前后进行aop的测试代码
3.1aop.xml
<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.xsd"> <bean id="audience" class="com.lzp.aop.Audience" />
<bean class="com.lzp.aop.Guitar" id="guitar">
<constructor-arg name="name" value="吉他"></constructor-arg>
</bean>
<bean id="eddie" class="com.lzp.aop.Instrumentalist">
<property name="instrument" ref="guitar" />
</bean>
<bean id="testPerformer" class="com.lzp.aop.TestPerformer"></bean> <bean id="logger" class="com.lzp.aop.WriterLogger" /> <bean id="loan" class="com.lzp.aop.TestLoan" />
<bean id="person" class="com.lzp.aop.Person">
<constructor-arg name="name" value="小名"></constructor-arg>
</bean> <aop:config>
<!--参数执行时间点 -->
<aop:aspect ref="audience">
<aop:pointcut expression="execution(* com.lzp.aop.Performer.perform(..))"
id="performerPointCut" />
<aop:before pointcut-ref="performerPointCut" method="takeSeats" /> <aop:before pointcut-ref="performerPointCut" method="turnOffCellPhones" /> <aop:after-returning pointcut-ref="performerPointCut"
method="applaud" /> <aop:after-throwing pointcut-ref="performerPointCut"
method="demandRefund" /> </aop:aspect>
<!-- 捕获带参数的方法执行 -->
<aop:aspect ref="logger">
<aop:pointcut
expression="execution(* com.lzp.aop.Loan.transfer(com.lzp.aop.Person)) and args(person)"
id="performerPointCut2" />
<aop:before method="Log" pointcut-ref="performerPointCut2"
arg-names="person" />
</aop:aspect>
</aop:config>
<!--<end id="audience_aspect" /> --> </beans>
3.2 类和接口
//Instrument
public abstract class Instrument { public Instrument(String name) {
this.name = name;
} public String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public abstract void play();
}
//Performer
public interface Performer {
void perform() throws PerformanceException; } //Instrumentalist
public class Instrumentalist implements Performer {
public void perform() throws PerformanceException {
instrument.play();
} private Instrument instrument; public void setInstrument(Instrument instrument) {
this.instrument = instrument;
} //Guitar
public class Guitar extends Instrument {
public Guitar(String name) {
super(name);
} public void play() {
System.out.println(name + "Strum strum strum");
}
} public Instrument getInstrument() {
return instrument;
} }
3.3mainTest
ApplicationContext context = new ClassPathXmlApplicationContext(
"aop.xml"); /* Performer performer = (Performer) context.getBean("eddie"); performer.perform();*/
4.获取方法传参的aop测试
4.1xml同上
4.2类和接口代码
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public interface Loan {
void transfer(Person person);
}
public class TestLoan implements Loan {
@Override
public void transfer(Person person) {
}
}
public interface Logger {
void Log(Person Person);
Person getPerson();
}
public class WriterLogger implements Logger {
private Person person;
@Override
public void Log(Person person) {
// TODO Auto-generated method stub
System.out.println("拦截带参数测试:person 的name为"+person.getName());
this.person=person;
}
@Override
public Person getPerson() {
// TODO Auto-generated method stub
return this.person;
}
}
上面实例主要记录了获取person类型参数的然后进行记录日志。
mainTest
ApplicationContext context = new ClassPathXmlApplicationContext(
"aop.xml"); /* Performer performer = (Performer) context.getBean("eddie"); performer.perform();*/ Loan loan = (Loan) context.getBean("loan");
Person person = (Person) context.getBean("person");
loan.transfer(person);
spring aop 之xml的更多相关文章
- spring aop 使用xml方式的简单总结
spring aop的 xml的配置方式的简单实现: 1.编写自己的切面类:配置各个通知类型 /** * */ package com.lilin.maven.service.aop; import ...
- Spring AOP-xml配置
在spring AOP(一)中介绍了AOP的基本概念和几个术语,现在学习一下在XML中如何配置AOP. 在XML中AOP的配置元素有以下几种: AOP配置元素 描述 <aop:config> ...
- Spring AOP 在XML中声明切面
转载地址:http://www.jianshu.com/p/43a0bc21805f 在XML中将一个Java类配置成一个切面: AOP元素 用途 <aop:advisor> 定义AOP通 ...
- Spring AOP之xml 配置实现
首先这个配置模式估计现在已经不用了,因为我在我们公司的项目里面并没有看到这么配置AOP相关的东西.不过,这个就和学习spring的控制反转(IOC)和依赖注入(DI)一样,刚刚开始的时候,都是从简单的 ...
- Spring AOP(三)--XML方式实现
本文介绍通过XML方式实现Spring AOP,在上一篇中已经介绍了通过注解+java配置的方式,这篇文章主要是看XML中怎么配置,直接上代码了: 一.创建一个连接点 1⃣️定义接口 注意⚠️:可以定 ...
- Spring AOP基于xml配置实例
SpringAOP里的几个术语,什么切面,切点之类的,官方的说明太抽象.为了更好地理解记忆,这里几下我自己的通俗的理解. 切面:就是日记类,什么前置通知后置通知(这些都是所谓的Advice)的具体方法 ...
- spring-第十八篇之spring AOP基于XML配置文件的管理方式
1.在XML配置文件中配置切面.切入点.增强处理.spring-1.5之前只能使用XML Schema方式配置切面.切入点.增强处理. spring配置文件中,所有的切面.切入点.增强处理都必须定义在 ...
- Spring系列(四):Spring AOP详解和实现方式(xml配置和注解配置)
参考文章:http://www.cnblogs.com/hongwz/p/5764917.html 一.什么是AOP AOP(Aspect Oriented Programming),即面向切面编程, ...
- Spring初学之xml实现AOP前置通知、后置通知、返回通知、异常通知等
实现两个整数的加减乘除,在每个方法执行前后打印日志. ArithmeticCalculator.java: package spring.aop.impl.xml; public interface ...
随机推荐
- 前端解读面向切面编程(AOP)
前言 面向对象(OOP)作为经典的设计范式,对于我们来说可谓无人不知,还记得我们入行起始时那句经典的总结吗-万事万物皆对象. 是的,基于OOP思想封装.继承.多态的特点,我们会自然而然的遵循模块化.组 ...
- Python3高级基础(2)
1 Python3模拟数据结构 1.1 栈 栈是计算机中经典的数据结构,简单的说,栈就是限制在一端进行插入删除操作的线性表.栈有两种最重要的操作,即pop(从栈顶弹出一个元素)和push(将一个元素进 ...
- 潭州课堂25班:Ph201805201 爬虫基础 第四课 Requests (课堂笔记)
优雅到骨子里的Requests 1528811134432 简介 上一篇文章介绍了Python的网络请求库urllib和urllib3的使用方法,那么,作为同样是网络请求库的Request ...
- 利用dockerfile定制镜像
利用dockerfile定制镜像 镜像的定制就是定制每一层所添加的配置.文件.如果可以吧每一层修改.安装.构建.操作的命令都写入到一个脚本,用脚本来构建.定制镜像,这个脚本就是dockerfile. ...
- js实现截取或查找字符串中的子字符串
获取 答案: var string0="sss.sscdyfasdfdgfg";//sscdy获取 ,); 答案是采用substr方法. 定义和用法:substr方法用于返回一个从 ...
- 喵哈哈村的魔法考试 Round #11 (Div.2) 题解
喵哈哈村的星星与月亮(一) 打表发现答案就等于a*b%mod 注意a*b可能爆longlong #include<bits/stdc++.h> using namespace std; c ...
- Delphi识别读取验证码
unit OCR; interface uses Windows, SysUtils, Graphics, Classes, PNGImage, GIFImage, JPEG, Math, Asphy ...
- android:ListView bbs Demo
我们制 作的 message_left.9.png 可以作为收到消息的背景图,那么毫无疑问你还需要再制作一张 message_right.9.png 作为发出消息的背景图. 图片都提供好了之后就可以开 ...
- Linux:FHS标准
FHS(英文:Filesystem Hierarchy Standard 中文:文件系统层次结构标准),多数Linux版本采用这种文件组织形式,FHS定义了系统中每个区域的用途.所需要的最小构成的文件 ...
- linux,日志查找技巧
1.查询日志中含有某个关键字的信息 cat app.log |grep 'error' 2.查询日志尾部最后10行的日志 tail -n 10 app.log 3.查询10行之后的所有日志 tail ...