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 ...
随机推荐
- Java开发人员必须掌握的Linux命令(二)
子曰:"工欲善其事,必先利其器." 学习应该是快乐的,在这个乐园中我努力让自己能用简洁易懂(搞笑有趣)的表达来讲解让知识或者技术,让学习之旅充满乐趣,这就是写博文的初心. 本篇的旅 ...
- ReportNG报表显示中文乱码和TestNG显示中文乱码实力解决办法
最近在进军测试自动化框架学习阶段,但无意间总是会伴随小问题的困扰,比如中文乱码,而导致显示总是不舒服,个人觉得,就一定要解决,似乎有点点强迫症.所以遇到ReportNG报表显示中文乱码和TestNG显 ...
- linux下构建MysqlCluster集群,NDB搜索引擎
搭建管理节点 Ndb搜索引擎对于服务器的内存要求比较高,因为所有数据节点的数据,以及索引,事务等等都需要加载进内存中. 下载 mysql-cluster-gpl-7.6.8-linux-glibc2. ...
- Alpha冲刺随笔—:第一天
课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(十天冲刺) 团队名称:葫芦娃队 作业目标:在十天冲刺里对每天的任务进行总结. 随笔汇总:https://www.cnblogs ...
- SqlDataAdapter更新数据
插入.更新和删除的排序 在许多情况下,以何种顺序向数据源发送通过 DataSet 作出的更改是相当重要的. 例如,如果已更新现有行的主键值并且添加了具有新主键值的新行, 则务必要在处理插入之前处理更新 ...
- Microsoft Corporation 去掉 windows 修改 启动加载 版权
windows 修改 开机界面 boot启动界面 windows 修改 启动加载 版权 windows 系统如何修改开机画面的版权文字“Microsoft Corporation ... ◎Micro ...
- [模板][P3796]AC自动机(加强版)
Description: 输出有哪些模式串在文本串中出现次数最多,这个次数是多少 Hint: 多组数据,$ len_{文本串}<=10^6,\sum len_{模式串} <= 70*150 ...
- git 创建SSH key
可以自己搭建一台运行Git的服务器,不过现阶段,为了学Git先搭个服务器绝对是小题大作.好在这个世界上有个叫GitHub的神奇的网站,从名字就可以看出,这个网站就是提供Git仓库托管服务的,所以,只要 ...
- php中__call() 和 __callStatic方法的使用
__call 与__callStatic 魔法方法是php5.3后新增的,二者的应用场景: 1.当要调用的方法不存在或权限不足时,会自动调用__call 方法. 2.当调用的静态方法不存在或权限不足时 ...
- ADSL理解
ADSL技术提供的上行和下行带宽不对称,因此称为非对称数字用户线路. ADSL技术采用频分复用技术把普通的电话线分成了电话.上行和下行三个相对独立的信道,从而避免了相互之间的干扰.用户可以边打电话边上 ...