首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

Spring AOP施用步骤

2012-07-15 
Spring AOP使用步骤在Spring中使用AOP编程步骤:以下使用的Spring为2.5.6版本1、在Spring配置文件(applicati

Spring AOP使用步骤

在Spring中使用AOP编程步骤:


以下使用的Spring为2.5.6版本


1、在Spring配置文件(applicationContext.xml)中配置Spring对AspectJ的支持;以下两种方式任意一种即可

???? <!-- <aop:aspectj-autoproxy/> -->
??? <bean name="code">public interface PersonManager {public void addPerson(Person person);}

?

public class SimplePersonManager implements PersonManager{public void addPerson(Person person) {Person.persons.add(person);                System.out.println("添加了一个用户");}}
?

?

4、定义一个切面类,该切面类中定义了一个切入点,连接点为SimplePersonManager类中的addPerson方法

?

import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;@Aspectpublic class SimpleAspect {@Before ("execution (* rote.spring.aop.service.impl.SimplePersonManager.addPerson(..))")public void addPersonAop(){System.out.println("-------------------addPerson-----------------------");};}

?

5、在Spring配置文件中声明切面类

??? <!-- 切面声明 -->
??? <bean id="simpleAspectj" name="code">ApplicationContext context = new ClassPathXmlApplicationContext("rote/spring/aop/applicationContext-aop.xml");PersonManager personManager = (PersonManager)context.getBean("personManager");personManager.addPerson(new Person());

?上述代码运行后,将输出如下结果,在调用addPerson方法之前执行了addPersonAop方法:

-------------------addPerson-----------------------
添加了一个用户

?

热点排行