本文共 2037 字,大约阅读时间需要 6 分钟。
Spring的AOP分为注解和配置两种方式实现. 网上注解的例子比较多.
package trytry.aop;
/** * @author 李晨 * @version 创建时间:Jul 28, 2009 3:01:01 PM */ public class Common {
public void show(){
System.out.println(
"------------------普通----------------");
}
}
package trytry.aop;
/** * @author 李晨 * @version 创建时间:Jul 28, 2009 3:03:08 PM */ public class Check {
public void show(){
System.out.println(
"------------------验证----------------");
}
}
下面是核心的Spring的配置文件applicationContext-aop.xml <? xml version ="1.0" encoding ="UTF-8" ?> < beans xmlns ="http://www.springframework.org/schema/beans" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop ="http://www.springframework.org/schema/aop" xmlns:tx ="http://www.springframework.org/schema/tx" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
> < bean id ="common" class ="trytry.aop.Common" /> < bean id ="check" class ="trytry.aop.Check" /> < aop:config > < aop:aspect id ="deal" ref ="check" > < aop:pointcut id ="target" expression ="execution(* trytry.aop.Common.show(..))" /> < aop:before method ="show" pointcut-ref ="target" /> </ aop:aspect > </ aop:config > </ beans > package trytry.aop;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/** * @author 李晨 * @version 创建时间:Jul 28, 2009 3:06:07 PM */ public class Client {
public static void main(String[] args) {
BeanFactory factory=
new ClassPathXmlApplicationContext(
"applicationContext-aop.xml");
Common c=(Common) factory.getBean(
"common");
c.show();
}
}
------------------验证----------------
------------------普通----------------
本文转自chainli 51CTO博客,原文链接:http://blog.51cto.com/lichen/184432,如需转载请自行联系原作者