Spring 面向切面编程(AOP)

来源:互联网 发布:杨振宁 李政道 知乎 编辑:程序博客网 时间:2024/06/18 06:04
向切面编程(AOP是Aspect Oriented Program的首字母缩写) ,我们知道,面向对象的特点是继承、多态和封装。
而封装就要求将功能分散到不同的对象中去,这在软件设计中往往称为职责分配。实际上也就是说,让不同的类设计不同的方法。
这样代码就分散到一个个的类中去了。这样做的好处是降低了代码的复杂程度,使类可重用。

但是人们也发现,在分散代码的同时,也增加了代码的重复性。什么意思呢?
比如说,我们在两个类中,可能都需要在每个方法中做日志。按面向对象的设计方法,我们就必须在两个类的方法中都加入日志的内容。
也许他们是完全相同的,但就是因为面向对象的设计让类与类之间无法联系,而不能将这些重复的代码统一起来。
也许有人会说,那好办啊,我们可以将这段代码写在一个独立的类独立的方法里,然后再在这两个类中调用。
但是,这样一来,这两个类跟我们上面提到的独立的类就有耦合了,它的改变会影响这两个类。
那么,有没有什么办法,能让我们在需要的时候,随意地加入代码呢?
这种在运行时,动态地将代码切入到类的指定方法、指定位置上的编程思想就是面向切面的编程。
一般而言,我们管切入到指定类指定方法的代码片段称为切面,而切入到哪些类、哪些方法则叫切入点。
有了AOP,我们就可以把几个类共有的代码,抽取到一个切片中,等到需要时再切入对象中去,从而改变其原有的行为。
这样看来,AOP其实只是OOP的补充而已。OOP从横向上区分出一个个的类来,而AOP则从纵向上向对象中加入特定的代码。
有了AOP,OOP变得立体了。如果加上时间维度,AOP使OOP由原来的二维变为三维了,由平面变成立体了。从技术上来说,AOP基本上是通过代理机制实现的。
AOP在编程历史上可以说是里程碑式的,对OOP编程是一种十分有益的补充。


下面来看一个简单的列子:

DAO接口

package com.jadeon.dao;public interface BasicDao {public void basicLogin();}
实现DAO接口类

package com.jadeon.dao.impl;import com.jadeon.dao.BasicDao;public class BasicDaoImpl implements BasicDao{@Overridepublic void basicLogin() {System.out.println("Enter login method...");}}
Service接口
package com.jadeon.service;public interface BasicService {public void basicLogin();}
实现Service接口类

package com.jadeon.service.impl;import com.jadeon.dao.BasicDao;import com.jadeon.service.BasicService;public class BasicServiceImpl implements BasicService {private BasicDao dao;@Overridepublic void basicLogin() {dao.basicLogin();}public BasicDao getDao() {return dao;}public void setDao(BasicDao dao) {this.dao = dao;}}
AOP切入类:

package com.jadeon.util;import org.aspectj.lang.ProceedingJoinPoint;public class LogHelper {public void startLog(){System.out.println("startLog...");}public void stopLog(){System.out.println("stopLog...");}public void around(ProceedingJoinPoint pjp) throws Throwable{System.out.println("startLog...");pjp.proceed();System.out.println("stopLog...");}}

applicationContext.xml:配置文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd           http://www.springframework.org/schema/aop           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">        <bean id="basicDao" class="com.jadeon.dao.impl.BasicDaoImpl"/>        <bean id="basicService" class="com.jadeon.service.impl.BasicServiceImpl">    <property name="dao" ref="basicDao"/>    </bean>    <!--     使用动态代理完成log记录 --><!-- 1. 注入logHelper 到ioc容器中  -->    <bean id="logHelper" class="com.jadeon.util.LogHelper" />    <!--     配置aop切面 -->    <aop:config><!-- 配置一个log的切面 --><aop:aspect id="logAspect" ref="logHelper"><!-- 切入点:操作点 --><aop:pointcut expression="execution(* com.jadeon.service.impl.BasicServiceImpl.basicLogin(..))" id="logPointcut"/><!-- 通知:前置通知 --><!--  <aop:before method="startLog" pointcut-ref="logPointcut"/> --><!-- 后置通知 --><!-- <aop:after method="stopLog" pointcut-ref="logPointcut"/>  --><!-- 环绕通知 --><aop:around method="around" pointcut-ref="logPointcut"/>    </aop:aspect>    </aop:config></beans>

测试类

package com.jadeon.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.jadeon.service.BasicService;public class Test {public static void main(String[] args) {@SuppressWarnings("resource")ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");BasicService service = (BasicService) context.getBean("basicService");service.basicLogin();}}