- 浏览: 36694 次
- 性别:
- 来自: 大连
最新评论
-
liwei113114:
servlet后面的BARCODE_TYPE参数传错了,参数名 ...
(转)用java生成条形码,barcode4j得应用 -
Smart_chw:
<div class="quote_title ...
(转)Struts2调用流程 -
lishijia:
看起来确实费劲。。。
(转)Struts2调用流程 -
JArcher:
建议图文结合。。。
(转)Struts2调用流程 -
free_zk:
实在是没有办法看了
(转)Struts2调用流程
经过这段日子的学习和使用Spring,慢慢地体会到Spring的优妙之处,正在深入地吸收Spring的精华,
呵呵。现在写的这个只是个简单AOP例子,包括前置通知,后置通知,环绕通知,和目标对象。写这个例子的主要目标只是想让想学AOP的能更快地入门,了解
一下如何去配置AOP里面的东东。
目标对象的接口:IStudent.java
1
/**
2 *
3 */
4 package com.dragon.study;
5
6 /**
7 * @author dragon
8 *
9 */
10 public interface IStudent {
11
12 public void addStudent(String name);
13 }
14
2 *
3 */
4 package com.dragon.study;
5
6 /**
7 * @author dragon
8 *
9 */
10 public interface IStudent {
11
12 public void addStudent(String name);
13 }
14
目标类:StudentImpl.java
1
/**
2 *
3 */
4 package com.dragon.study.Impl;
5
6 import com.dragon.study.IStudent;
7
8 /**
9 * @author dragon
10 *
11 */
12 public class StudentImpl implements IStudent {
13
14 public void addStudent(String name) {
15 System.out.println( " 欢迎 " + name + " 你加入Spring家庭! " );
16 }
17 }
18
2 *
3 */
4 package com.dragon.study.Impl;
5
6 import com.dragon.study.IStudent;
7
8 /**
9 * @author dragon
10 *
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
1
/**
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 dragon
12 *
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
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 dragon
12 *
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
1
/**
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 dragon
12 *
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
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 dragon
12 *
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
1
/**
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 dragon
12 *
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
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 dragon
12 *
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
1
<?
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 >
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
1
/**
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 dragon
13 *
14 */
15 public class Test {
16
17 /**
18 * @param args
19 */
20 public static void main(String[] args) {
21 // TODO Auto-generated method stub
22 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
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 dragon
13 *
14 */
15 public class Test {
16
17 /**
18 * @param args
19 */
20 public static void main(String[] args) {
21 // TODO Auto-generated method stub
22 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
发表评论
-
POI读取EXCEL进行sheet复制
2011-08-10 12:10 2688/****************************** ... -
POI设置EXCEL单元格格式为文本、小数、百分比、货币、日期、科学计数法和中文大写
2011-04-28 15:55 1263再读本篇文章之前,请先看我的前一篇文章,前一篇文章中有重点 ... -
(转)ArrayList实现
2011-02-19 08:34 755ArrayList是List接口的一个可变长数组实现。实现 ... -
(转)iReport与JasperReport简介
2011-01-31 08:45 1270一、iReport与JasperReport简介 ... -
(转)用java生成条形码,barcode4j得应用
2011-01-26 09:54 2754最近一个oa系统用到了 ... -
(转)条形码barcode4j的使用
2011-01-26 09:51 900 -
如何成功制作可执行的Jar包
2010-12-19 12:23 747今天试了一下制作可执行的Jar文件的方法。 具体如下: 首 ... -
bat运行jar文件
2010-12-19 09:07 1552set JAVA_HOME=D:\Users\Java\jdk ... -
(转)JAR文件包及jar命令详解
2010-12-19 07:39 784常常在网上看到 ... -
(转)在java程序中如何知道数据库表的主键
2010-12-11 07:59 1378有同事问到在程序中怎样知道数据库表中那 ... -
(转)浅谈JDBC事务和JTA (XA)事务
2010-11-10 09:46 1079事务简介 一般情 ... -
Java, 面试题, Spring
2010-11-05 14:36 1265Java, 面试题, Spring 113. 什么是aop,a ... -
JSP 抛出一些URI异常的解决方法
2010-05-31 10:24 1266本文来自CSDN博客:http://blog.csdn.net ... -
(转)ECSide入门(资料整理)
2010-03-24 14:14 794ECSide是有一个基于jsp tag的开源列表组件.简 ... -
(转)Struts2调用流程
2010-01-07 14:11 14241. 当Servlet容器接收到一个请求后,将请求交给你在we ... -
Struts1和Struts2的区别和对比
2009-08-07 20:46 6681.Action 类: • Struts1要求Action类 ...
相关推荐
NULL 博文链接:https://306963591.iteye.com/blog/1129837
spring的aop简单例子spring的aop简单例子spring的aop简单例子spring的aop简单例子
spring aop例子,这是spring aop例子哦
Spring AOP 几个不同使用方法的完整例子,使用Junit4c测试, 在我的博客上有不同配置组合的说明,可以参考
spring aop简单例子,入门学习的好资料
spring aop 经典例子(原创),无论新手还是老手,可以快速对Spring AOP有个总体的认识。基于JDK1.6,Spring2.0.5,Eclipse IDE。
最近做的Spring 2.5 AOP 例子,保留下以后复习说不定用得上. 1.使用类扫描功能这个有点意思.使用时方便了许多. 2.XML配置方式
eclipse的SpringAOP和动态代理的小例子
spring aop 通过注解实现的项目,这只是一个简单的例子。运行环境也简单。
Spring.net AOP 动态代理例子
全面讲解spring Aop 功能,例子丰富。
找了很久,也没在网上找到spring mvc框架下的aop例子~研究了好久终于写出来了~拿出来和大家分享一下~
springAOP小例子 装配器 切入点 通知等
Spring AOP 日志管理 实例LoggingThrowsAdvice.java
实现了springAOP 适合新手进行学习
Spring3.1AOP简单例子,需要的jar包分开上传了
Spring IOC AOP MVC 简单例子
利用SPring AOP配置切面的一个例子
spring aop 自定义切面示例 aspectj 需要相应的架包支持
两个spring aop 的小例子