经过这段日子的学习和使用Spring,慢慢地体会到Spring的优妙之处,正在深入地吸收Spring的精华,呵
呵。现在写的这个只是个简单AOP例子,包括前置通知,后置通知,环绕通知,和目标对象。写这个例子的主要目标只是想让想学AOP的能更快地入门,了解一
下如何去配置AOP里面的东东。
目标对象的接口:IStudent.java
/**
*
*/
package com.dragon.study;
/**
* @author dragon
*
*/
public interface IStudent {
public void addStudent(String name);
}
目标类:StudentImpl.java
/** */ /**
*
*/
package com.dragon.study.Impl;
import com.dragon.study.IStudent;
/** */ /**
* @author dragon
*
*/
public class StudentImpl implements IStudent {
public void addStudent(String name) {
System.out.println( " 欢迎 " + name + " 你加入Spring家庭! " );
}
}
前置通知:BeforeAdvice.java
/**
*
*/
package com.dragon.Advice;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
/**
* @author dragon
*
* /
public class BeforeAdvice implements MethodBeforeAdvice {
public void before(Method method,Object[] args, Object target)
throws Throwable {
System.out.println( " 这是BeforeAdvice类的before方法. " );
}
后置通知:AfterAdvice.java
/**
*
*/
package com.dragon.Advice;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
/**
* @author dragon
*
*/
public class AfterAdvice implements AfterReturningAdvice{
public void afterReturning(Object returnValue ,Method method,
Object[] args,Object target) throws Throwable{
System.out.println("这是AfterAdvice类的afterReturning方法.");
}
}
环绕通知:CompareInterceptor.java
/**
*
*/
package com.dragon.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
* @author dragon
*
*/
public class CompareInterceptor implements MethodInterceptor{
public Object invoke(MethodInvocation invocation) throws Throwable{
Object result = null;
String stu_name = invocation.getArguments()[0].toString();
if ( stu_name.equals("dragon")){
//如果学生是dragon时,执行目标方法,
result= invocation.proceed();
} else{
System.out.println("此学生是"+stu_name+"而不是dragon,不批准其加入.");
}
return result;
}
}
配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="beforeAdvice" class="com.dragon.Advice.BeforeAdvice"></bean>
<bean id="afterAdvice" class="com.dragon.Advice.AfterAdvice"></bean>
<bean id="compareInterceptor" class="com.dragon.Advice.CompareInterceptor"></bean>
<bean id="studenttarget" class="com.dragon.study.Impl.StudentImpl"></bean>
<bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.dragon.study.IStudent</value>
</property>
<property name="interceptorNames">
<list>
<value>beforeAdvice</value>
<value>afterAdvice</value>
<value>compareInterceptor</value>
</list>
</property>
<property name="target">
<ref bean="studenttarget"/>
</property>
</bean>
</beans>
现在开始写测试类,Test.java
/**
*
*/
package com;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.dragon.study.IStudent;
/**
* @author dragon
*
*/
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx =
new FileSystemXmlApplicationContext("/com/dragon/applicationContext.xml");
IStudent person = (IStudent)ctx.getBean("student");
person.addStudent("dragon");
// person.addStudent("javadragon");
}
}
}
出处: http://www.blogjava.net/javadragon/archive/2006/12/03/85115.html
分享到:
相关推荐
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 MVC中AOP的例子,分为配置和实现两部分。 ### 配置 1. 在`spring-common.xml`配置文件中,我们需要引入AOP相关的命名空间,并声明一个`<aop:aspectj-autoproxy>`元素。这会告诉Spring容器...
全面讲解spring Aop 功能,例子丰富。
Spring AOP,全称Aspect-Oriented Programming(面向切面编程),是Spring框架的重要组成部分,用于实现横切关注点的模块化。它允许开发者定义“切面”,将那些与业务逻辑无关,却为多个对象共有的行为(如日志、...
springAOP小例子 装配器 切入点 通知等
Spring AOP 日志管理 实例LoggingThrowsAdvice.java
实现了springAOP 适合新手进行学习
Spring3.1AOP简单例子,需要的jar包分开上传了
Spring IOC AOP MVC 简单例子
利用SPring AOP配置切面的一个例子
spring aop 自定义切面示例 aspectj 需要相应的架包支持