Spring AOP面向切面编程的实现
1、涉及到的几个概念
切面类、被切对象、切入点、切入时间、切入内容;(自己命的名,好理解点)
2、看配置文件
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="audience" class="com.spring.test.aop.Audience"/> <bean id="sax" class="com.spring.test.setter.Saxophone"/>
<bean id="kenny" class="com.spring.test.setter.Instrumentalist">
<property name="song" value="Jingle Bells" />
<property name="age" value="25" />
<property name="instrument" ref="sax"/>
</bean> <aop:config proxy-target-class="true">
<aop:aspect ref="audience">
<aop:pointcut id="performance" expression="execution(* com.spring.test.setter.Instrumentalist.*(..))"/>
<!--<aop:pointcut id="performance1" expression="execution(* com.spring.test.action1.Performer.perform(..))"/>--> <aop:before pointcut-ref="performance" method="takeSeats"/>
<aop:before pointcut-ref="performance" method="turnOffCellPhones"/>
<aop:after-returning pointcut-ref="performance" method="applaud"/>
<aop:after-throwing pointcut-ref="performance" method="demandRefund"/> </aop:aspect>
</aop:config>
</beans>
AOP配置文件
3、需要引入的jar包

4、代码
package com.spring.test.action1;
public interface Performer {
void perform() ;
}
package com.spring.test.setter; import com.spring.test.action1.Performer;
/**
* 被切面类,(一般是业务类)
* @author Administrator
*
*/
public class Instrumentalist implements Performer{
private String song;
private int age;
private Instrument instrument;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSong() {
return song;
}
public void setSong(String song) {
this.song = song;
}
public Instrument getInstrument() {
return instrument;
}
public void setInstrument(Instrument instrument) {
this.instrument = instrument;
}
public Instrumentalist(){}
public Instrumentalist(String song,int age,Instrument instrument){
this.song = song;
this.age = age;
this.instrument = instrument;
}
public void perform() {
System.out.println("Instrumentalist age:"+age);
System.out.print("Playing "+song+":");
instrument.play();
}
public void say(){
System.out.println("this is saying !!!!!");
}
}
package com.spring.test.setter;
public interface Instrument {
public void play();
}
package com.spring.test.setter;
public class Saxophone implements Instrument {
public Saxophone(){}
public void play() {
System.out.println("TOOT TOOT TOOT");
}
}
package com.spring.test.aop;
/**
* 切面类
* @author Administrator
*
*/
public class Audience {
public void takeSeats(){
System.out.println("The audience is taking their seats.");
}
public void turnOffCellPhones(){
System.out.println("The audience is turning off their cellphones");
}
public void applaud(){
System.out.println("CLAP CLAP CLAP");
}
public void demandRefund(){
System.out.println("Boo! We want money back");
}
}
package com.spring.test.setter; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); Instrumentalist performer = (Instrumentalist)ctx.getBean("kenny");
performer.perform();
performer.say(); }
}
5、关于切点表达式 详见:
http://www.cnblogs.com/yanjunwu/p/3996570.html
切面编程优点:有利于系统维护、升级、优化改造;便于将业务逻辑分离;
参考:www.cnblogs.com/xing901022/
Spring AOP面向切面编程的实现的更多相关文章
- 详细解读 Spring AOP 面向切面编程(二)
本文是<详细解读 Spring AOP 面向切面编程(一)>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实 ...
- 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~
简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...
- spring AOP面向切面编程学习笔记
一.面向切面编程简介: 在调用某些类的方法时,要在方法执行前或后进行预处理或后处理:预处理或后处理的操作被封装在另一个类中.如图中,UserService类在执行addUser()或updateUse ...
- 【Spring系列】Spring AOP面向切面编程
前言 接上一篇文章,在上午中使用了切面做防重复控制,本文着重介绍切面AOP. 在开发中,有一些功能行为是通用的,比如.日志管理.安全和事务,它们有一个共同点就是分布于应用中的多处,这种功能被称为横切关 ...
- 从源码入手,一文带你读懂Spring AOP面向切面编程
之前<零基础带你看Spring源码--IOC控制反转>详细讲了Spring容器的初始化和加载的原理,后面<你真的完全了解Java动态代理吗?看这篇就够了>介绍了下JDK的动态代 ...
- Spring AOP面向切面编程详解
前言 AOP即面向切面编程,是一种编程思想,OOP的延续.在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等等.在阅读本文前希望您已经对Spring有一定的了解 注:在能对代码进行添 ...
- Spring AOP 面向切面编程相关注解
Aspect Oriented Programming 面向切面编程 在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业. ...
- Spring AOP 面向切面编程入门
什么是AOP AOP(Aspect Oriented Programming),即面向切面编程.众所周知,OOP(面向对象编程)通过的是继承.封装和多态等概念来建立一种对象层次结构,用于模拟公共行为的 ...
- 详细解读 Spring AOP 面向切面编程(一)
又是一个周末, 今天我要和大家分享的是 AOP(Aspect-Oriented Programming)这个东西,名字与 OOP 仅差一个字母,其实它是对 OOP 编程方式的一种补充,并非是取而代之. ...
- Spring Aop面向切面编程&&自动注入
1.面向切面编程 在程序原有纵向执行流程中,针对某一个或某一些方法添加通知,形成横切面的过程叫做面向切面编程 2.常用概念 原有功能:切点,pointcut 前置通知:在切点之前执行的功能,befor ...
随机推荐
- Windows后渗透
My 命令行下收集主机信息 使用wmic识别安装到系统中的补丁情况: wmic qfe get description,installedOn 识别正在运行的服务: sc query type= se ...
- Maven搭建springMVC+spring+hibernate环境
这次不再使用struts2做控制器,采用spring自己的springMVC框架实现. 首先,改写pom.xml文件,不需要struts2的相关jar了. pom.xml <project xm ...
- css实现三列布局,左右固定值,中间自适应。
这里主要用到的是position:absolute;及margin属性;代码很简单,一看就明白. <!DOCTYPE html> <html lang="zh_CN&quo ...
- hdu_5286_wyh2000 and sequence(分块)
题目链接:hdu_5286_wyh2000 and sequence 题意: 给一段长度为N的序列,每次询问l-r(l和r和上一次询问的答案有关)内 不同的数的 出现次数的次方 的和.强制在线 题解: ...
- 三、WCF的宿主
注:本文为学习摘抄,原文地址:http://www.cnblogs.com/iamlilinfeng/archive/2012/10/01/2706353.html 一.WCF服务应用程序与WCF服务 ...
- IOS 类似网易新闻客户端内容滚动菜单跟随居中组件
需求分析: 1.类似网易新闻客户端页面滚动组件.菜单栏对应菜单项一直居中 2.点击菜单栏可以切换到对应的page 3.滑动页面可以自动切换相应的菜单.并且对应的菜单栏居中显示 4.初始化时可以自定义菜 ...
- HDU 4520 小Q系列故事――最佳裁判(STL)
小Q系列故事——最佳裁判 Problem Description 过去的2012年对小Q来说是很悲催的一年,失恋了12次,每次都要郁闷1个来月. 好在小Q是个体育迷,在最痛苦的时候,他常常用观看各种体 ...
- Excel教程(11) - 查找和引用函数
ADDRESS 用途:以文字形式返回对工作簿中某一单元格的引用. 语法: sheADDRESS(row_num,column_num,abs_num,a1,et_text) 参数:Row_num ...
- Openjudge-计算概论(A)-完美立方
描述: a的立方 = b的立方 + c的立方 + d的立方为完美立方等式.例如12的立方 = 6的立方 + 8的立方 + 10的立方 .编写一个程序,对任给的正整数N (N≤100),寻找所有的四元组 ...
- Find and run the whalesay image
Find and run the whalesay image People all over the world create Docker images. You can find these i ...