切入点(Pointcut)切入点是连接点的集合,通知将在满足一个切入点表达式的所有连接点上运行。举例:在拦截器中,有一系列判断性的内容
1if(method.equals("savePerson")||method.equals("updatePerson") ||method.equals("deletePerson")){ ... }满足了上面三个方法才能开启事务,这些判断条件就为切入点
引入(Introduction)引入的意思是在一个类中加入新的属性或者方法。目标对象(Target Object)被一个或多个切面所通知的对象成为目标对象。AOP代理(AOP Proxy)AOP代理是AOP框架所生成的对象,该对象是目标对象的代理对象。代理对象能够在目标对象的基础上,在相应的连接点上调用通知。织入(Weaving)把切面连接到其他的应用程序之上,创建一个被通知的对象的过程,被称为织入。事务处理类Transaction
123456789public class Transaction { public void beginTransaction(){ System.out.println("begin transaction"); } public void commit(){ System.out.println("commit"); }}接下来最重要的就是AOP的配置文件,applicationContext.xml。通过配置AOP可以实现创建代理对象:代理对象的方法=目标方法+通知。将原本不相关的目标方法和通知结合起来。
123456789101112131415<bean id="personDao" class="cn.zju.spring.PersonDaoImpl"></bean><bean id="transaction" class="cn.zju.spring.Transaction"></bean> <aop:config><!-- 切入点表达式 确定目标类 id 标示--> <aop:pointcut expression="execution(* cn.zju.spring.PersonDaoImpl.*(..))" id="perform"/> <aop:aspect ref="transaction"> <!-- ref指向切面类 --> <aop:before method="beginTransaction" pointcut-ref="perform"/> <!--前置通知 --> <aop:after-returning method="commit" pointcut-ref="perform"/> <!--后置通知 --> </aop:aspect></aop:config>客户端代码
12345public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); PersonDao personDao = (PersonDao)context.getBean("personDao"); personDao.savePerson();}