计算王:Spring的AOP例子
来源:百度文库 编辑:九乡新闻网 时间:2024/07/14 14:48:45
一个简单的Spring的AOP例子
经过这段日子的学习和使用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
目标类: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
前置通知: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
后置通知: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
环绕通知: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
配置文件applicationContext.xml
1
2
3
4
5
6
7
8
9
10
11
12
13 com.dragon.study.IStudent
14
15
16
21
22
23
24
25
26
27
28
29
30
31
现在开始写测试类,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 }
经过这段日子的学习和使用Spring,慢慢地体会到Spring的优妙之处,正在深入地吸收Spring的精华,呵呵。现在写的这个只是个简单AOP例子,包括前置通知,后置通知,环绕通知,和目标对象。写这个例子的主要目标只是想让想学AOP的能更快地入门,了解一下如何去配置AOP里面的东东。
目标对象的接口:IStudent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
目标类:StudentImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
前置通知:BeforeAdvice.java
1 /**
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
后置通知:AfterAdvice.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
环绕通知:CompareInterceptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
配置文件applicationContext.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
现在开始写测试类,Test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Spring的AOP例子
spring aop pointcut expression 例子说明
Spring AOP 常用的四种
Spring Hibernate在AOP方面的配置冲突解决之道
通俗的解释Spring中IOC, DI, AOP
Spring AOP 常用的四种实现方式 -
一个spring的简单例子2
Spring aop expression 星号(*)
Spring AOP技术配置语法
spring mvc注解例子
spring tx:advice 和 aop:config 配置事务
Spring中的两种AOP织入方式
35、活生生的例子
会计实务的例子
spring的ioc实
梁小婷:机会成本的小例子
spring jar包的相关性
Spring与Struts的区别
简单的生活例子,深刻的经济学原理
美丽的城市街头艺术的例子
17、这只是一个狡辩的例子
易数治疗多年顽症的例子
史上最全的特色菜谱(17866例子)
一步一步解读MT4的例子EA2