一个简单的Spring的AOP例子
目标对象的接口:IStudent.java
/** 2
* 3
*/ 4
package com.dragon.study;5
6
/** 7
* @author dragon8
*9
*/ 10
public interface IStudent {11
12
public void addStudent(String name);13
} 14

目标类:StudentImpl.java
/** 2
* 3
*/ 4
package com.dragon.study.Impl;5
6
import com.dragon.study.IStudent;7
8
/** 9
* @author dragon10
*11
*/ 12
public class StudentImpl implements IStudent {13
14
public void addStudent(String name) {15
System.out.println( " 欢迎 " + name + " 你加入Spring家庭! " );16
} 17
} 18

前置通知:BeforeAdvice.java
/** 2
* 3
*/ 4
package com.dragon.Advice;5
6
import java.lang.reflect.Method;7
8
import org.springframework.aop.MethodBeforeAdvice;9
10
/** 11
* @author dragon12
*13
*/ 14
public class BeforeAdvice implements MethodBeforeAdvice {15
16
public void before(Method method,Object[] args, Object target)17
throws Throwable {18
19
System.out.println( " 这是BeforeAdvice类的before方法. " );20
21
} 22
} 23

后置通知:AfterAdvice.java
/**2
* 3
*/4
package com.dragon.Advice;5

6
import java.lang.reflect.Method;7

8
import org.springframework.aop.AfterReturningAdvice;9

10
/**11
* @author dragon12
*13
*/14
public class AfterAdvice implements AfterReturningAdvice{15
16
public void afterReturning(Object returnValue ,Method method,17
Object[] args,Object target) throws Throwable{18
System.out.println("这是AfterAdvice类的afterReturning方法.");19
}20
21

22
}23

环绕通知:CompareInterceptor.java
/**2
* 3
*/4
package com.dragon.Advice;5

6
import org.aopalliance.intercept.MethodInterceptor;7
import org.aopalliance.intercept.MethodInvocation;8

9

10
/**11
* @author dragon12
*13
*/14
public class CompareInterceptor implements MethodInterceptor{15

16
public Object invoke(MethodInvocation invocation) throws Throwable{17
Object result = null;18
String stu_name = invocation.getArguments()[0].toString();19
if ( stu_name.equals("dragon")){20
//如果学生是dragon时,执行目标方法,21
result= invocation.proceed();22
23
} else{24
System.out.println("此学生是"+stu_name+"而不是dragon,不批准其加入.");25
}26
27
return result;28
}29
}30

配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>2
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">3

4
<beans>5

6
<bean id="beforeAdvice" class="com.dragon.Advice.BeforeAdvice"></bean>7
<bean id="afterAdvice" class="com.dragon.Advice.AfterAdvice"></bean>8
<bean id="compareInterceptor" class="com.dragon.Advice.CompareInterceptor"></bean>9
<bean id="studenttarget" class="com.dragon.study.Impl.StudentImpl"></bean>10

11
<bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean">12
<property name="proxyInterfaces">13
<value>com.dragon.study.IStudent</value>14
</property>15
<property name="interceptorNames">16
<list>17
<value>beforeAdvice</value>18
<value>afterAdvice</value>19
<value>compareInterceptor</value> 20
</list>21
</property>22
<property name="target">23
<ref bean="studenttarget"/>24
</property>25

26
</bean>27

28

29

30

31
</beans>现在开始写测试类,Test.java
/**2
* 3
*/4
package com;5

6
import org.springframework.context.ApplicationContext;7
import org.springframework.context.support.FileSystemXmlApplicationContext;8

9
import com.dragon.study.IStudent;10

11
/**12
* @author dragon13
*14
*/15
public class Test {16

17
/**18
* @param args19
*/20
public static void main(String[] args) {21
// TODO Auto-generated method stub22
ApplicationContext ctx = 23
new FileSystemXmlApplicationContext("/com/dragon/applicationContext.xml");24
25
IStudent person = (IStudent)ctx.getBean("student");26
person.addStudent("dragon");27
28
// person.addStudent("javadragon");29
}30

31
}32

一个简单的Spring的AOP例子的更多相关文章
- 一个简单的Spring测试的例子
在做测试的时候我们用到Junit Case,当我们的项目中使用了Sring的时候,我们应该怎么使用spring容器去管理我的测试用例呢?现在我们用一个简单的例子来展示这个过程. 1 首先我们新建一个普 ...
- 【Spring】简单的Spring MVC入门例子
前言 测试特性需要搭建一个简单的Spring MVC的例子,遂记录之,只是例子,只为入门者之示例. 版本说明 声明POM文件,指定需引入的JAR. <properties> <spr ...
- 在eclipse中配置一个简单的spring入门项目
spring是一个很优秀的基于Java的轻量级开源框架,为了解决企业级应用的复杂性而创建的,spring不仅可用于服务器端开发,从简单性.可测试性和松耦合性的角度,任何java应用程序都可以利用这个思 ...
- 创建一个简单的Spring应用
环境已经安装完成,接下来创建一个简单的Spring应用. 创建Spring应用步骤: 创建一个maven项目 添加spring库依赖 创建Bean类 添加Bean的xml装配文件 创建主类 运行应用程 ...
- 构建一个简单的Spring Boot项目
11 构建一个简单的Spring Boot项目 这个章节描述如何通过Spring Boot构建一个"Hello Word"web应用,侧重介绍Spring Boot的一些重要功能. ...
- 一个简单的spring boot程序
搭建一个spring boot项目十分的方便,网上也有许多,可以参考 https://www.cnblogs.com/ityouknow/p/5662753.html 进行项目的搭建.在此我就不详细介 ...
- 一个简单的Spring AOP例子
转载自: http://www.blogjava.net/javadragon/archive/2006/12/03/85115.html 经过这段日子的学习和使用Spring,慢慢地体会到Sprin ...
- JAVA WEB快速入门之通过一个简单的Spring项目了解Spring的核心(AOP、IOC)
接上篇<JAVA WEB快速入门之从编写一个JSP WEB网站了解JSP WEB网站的基本结构.调试.部署>,通过一个简单的JSP WEB网站了解了JAVA WEB相关的知识,比如:Ser ...
- CSS布局中一个简单的应用BFC的例子
什么是BFC BFC(Block Formatting Context),简单讲,它是提供了一个独立布局的环境,每个BFC都遵守同一套布局规则.例如,在同一个BFC内,盒子会一个挨着一个的排,相邻盒子 ...
随机推荐
- Zabbix——设置模板
前提条件: Zabbix版本为4.0 可以正常使用Server 1. 添加H3C网络交换机的CPU和内存模板,并配置自动发现. 注意,需要添加完成后,重新进行搜索后再此进入才有下列选项. 添加CPU的 ...
- Linux 只显示目录或者文件方法
ls 参数 -a 表示显示所有文件,包含隐藏文件-d 表示显示目录自身的属性,而不是目录中的内容-F 选项会在显示目录条目时,在目录后加一个/ 只显示目录 方法一: find . -type d -m ...
- Java : Spring基础 AOP
简单的JDK动态代理例子(JDK动态代理是用了接口实现的方式)(ICar是接口, GoogleCar是被代理对象, MyCC是处理方法的类): public class TestCar { publi ...
- 关于解决 https 网站无法加载 http 脚本
前几天刚配置好https网站 然后今天浏览发现自己网站的地图插件不见了 然后看了一下报错显示 然后百度搜索一番找到了解决办法 <meta http-equiv="Content-Sec ...
- PHPExcel 导入Excel数据 (导出下一篇我们继续讲解)
一:使用composer下载 phpoffice/phpexcel 或者直接下载安装包 composer require phpoffice/phpexcel 二 1:导入数据 原理:读取文件,获取文 ...
- Vue directive自定义指令+canvas实现H5图片压缩上传-Base64格式
前言 最近优化项目-手机拍照图片太大,回显速度比较慢,使用了vue的自定义指令实现H5压缩上传base64格式的图片 canvas自定义指令 Vue.directive("canvas&qu ...
- 微信小程序通过api接口将json数据展现到小程序上
实现知乎客户端的一个重要知识前提就是,要知道怎么通过知乎新闻的接口,来把数据展示到微信小程序端上. 那么我们这一就先学习一下,如何将接口获取到的数据展示到微信小程序上. 1.用到的知识点 <1& ...
- Python学习笔记六:集合
集合 Set,去重,关系测试:交.并.差等:无序 list_1=set(list_1), type(list_1) list_2=set([xxxxx]) 交集:list_1.intersectin( ...
- 转载:C语言指针使用的注意事项
相信大家对指针的用法已经很熟了,这里也不多说些定义性的东西了,只说一下指针使用中的注意事项吧. 一.在定义指针的时候注意连续声明多个指针时容易犯的错误,例如int * a,b;这种声明是声明了一个指向 ...
- 立个Flag (20180617-20181231)
入行7年,今年年初正式接触Java,前面6年一直在做C++相关的工作,去年年中跳槽,语言从C++转向了C#,半年之后又转向了Java. 虽说语言有相似性,但每种语言都有自己独有的知识体系,想要游刃有余 ...