目标对象的接口: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 */
4package com.dragon.Advice;
5
6import java.lang.reflect.Method;
7
8import org.springframework.aop.AfterReturningAdvice;
9
10/** *//**
11 * @author dragon
12 *
13 */
14public 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 */
4package com.dragon.Advice;
5
6import org.aopalliance.intercept.MethodInterceptor;
7import org.aopalliance.intercept.MethodInvocation;
8
9
10/** *//**
11 * @author dragon
12 *
13 */
14public 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>
现在开始写测试类,Test.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
*/
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext("/src/applicationContext.xml");
Student s = (Student) ctx.getBean("student");
s.addStudent("ss");
}
}
http://jff.iteye.com/blog/221458
分享到:
相关推荐
spring aop
spring aop 拦截 实例spring aop 拦截 实例spring aop 拦截 实例
spring 采用aop 的实例
使用SpringAOP完成日志记录功能的实例
spring AOP的实例代码,有着详细的讲解,
SpringAop实例,包括xml和配置两种方式.jar齐全
aop java
在Springboot上使用AOP的简单实例源码,可以结合本人的文章一起学习理解 https://blog.csdn.net/vincent_yuan89/article/details/85128009
Spring-AOP实例
Spring Aop的使用实例
spring3.2.8使用aop的简单实例
spring aop 拦截实例,下载下来直接就可以用了,很好很方便。
springaop详细解释以及代码实现demo,包含了所以aop通知类型
一个基于@AspectJ的spring2.0 AOP应用实例,很小很简单,没有任何额外信息,最适合AOP入门学习。使用log4j打印信息。把项目直接import进myeclipse就可以使用啦......
基于xml代码的SpringAOP实例详解,适合初步学习springAOP的开发人员了解该功能的使用。
aop入门。
里面有Spring-AOP的介绍(word文档),还有写了一些Spring的实例(MyEclipse项目),直接导入项目就可以运行了。
MVC3 Filter 验证 AOP 实例
Spring Aop实例(AOP 如此简单)@Aspect、@Around 注解方式配置
spring AOP实例