[toc] AOP是SPRing提供的關鍵特性之一。AOP即面向切面編程,是OOP編程的有效補充。使用AOP技術,可以將一些系統性相關的編程工作,獨立提取出來,獨立實現,然后通過切面切入進系統。從而避免了在業務邏輯的代碼中混入很多的系統相關的邏輯——比如權限管理,事物管理,日志記錄等等。這些系統性的編程工作都可以獨立編碼實現,然后通過AOP技術切入進系統即可。從而達到了 將不同的關注點分離出來 的效果。本文深入剖析Spring的AOP的原理。
1) aspect :切面,切入系統的一個切面。比如事務管理是一個切面,權限管理也是一個切面;
2) Join point :連接點,也就是可以進行橫向切入的位置;
3) Advice :通知,切面在某個連接點執行的操作(分為: Before advice , After returning advice , After throwing advice , After (finally) advice , Around advice );
4) Pointcut :切點,符合切點表達式的連接點,也就是真正被切入的地方;
被代理對象:
public class UserServiceImpl implements UserService { public void addUser(User user) { System.out.println("add user into database."); } public User getUser(int id) { User user = new User(); user.setId(id); System.out.println("getUser from database."); return user; }}代理中間類:
import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;public class ProxyUtil implements InvocationHandler { private Object target; // 被代理的對象 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("do sth before...."); Object result = method.invoke(target, args); System.out.println("do sth after...."); return result; } ProxyUtil(Object target){ this.target = target; } public Object getTarget() { return target; } public void setTarget(Object target) { this.target = target; }}測試:
import java.lang.reflect.Proxy;import net.aazj.pojo.User;public class ProxyTest { public static void main(String[] args){ Object proxyedObject = new UserServiceImpl(); // 被代理的對象 ProxyUtil proxyUtils = new ProxyUtil(proxyedObject); // 生成代理對象,對被代理對象的這些接口進行代理:UserServiceImpl.class.getInterfaces() UserService proxyObject = (UserService) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), UserServiceImpl.class.getInterfaces(), proxyUtils); proxyObject.getUser(1); proxyObject.addUser(new User()); }}執行結果:
do sth before....getUser from database.do sth after....do sth before....add user into database.do sth after....我們看到在 UserService接口中的方法 addUser 和 getUser方法的前面插入了我們自己的代碼。這就是JDK動態代理實現AOP的原理。我們看到該方式有一個要求, 被代理的對象必須實現接口,而且只有接口中的方法才能被代理 。輸出結果:
do sth before....getUser from database.do sth after....do sth before....add user into database.do sth after....我們看到達到了同樣的效果。它的原理是生成一個父類 enhancer.setSuperclass( this.target.getClass()) 的子類 enhancer.create() ,然后對父類的方法進行攔截enhancer.setCallback( this) . 對父類的方法進行覆蓋,所以父類方法不能是final的。如果被代理對象實現了接口,那么就使用JDK的動態代理技術,反之則使用CGLIB來實現AOP,所以 Spring默認是使用JDK的動態代理技術實現AOP的 。
JdkDynamicAopProxy的實現其實很簡單:final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializable { @Overridepublic Object getProxy(ClassLoader classLoader) { if (logger.isDebugEnabled()) { logger.debug("Creating JDK dynamic proxy: target source is " + this.advised.getTargetSource()); } Class<?>[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised); findDefinedEqualsAndHashCodeMethods(proxiedInterfaces); return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);}再看一個例子:
<bean id="aspectBean" class="net.aazj.aop.DataSourceInterceptor"/><aop:config> <aop:aspect id="dataSourceAspect" ref="aspectBean"> <aop:pointcut id="dataSourcePoint" expression="execution(public * net.aazj.service..*.getUser(..))" /> <aop:pointcut expression="" id=""/> <aop:before method="before" pointcut-ref="dataSourcePoint"/> <aop:after method=""/> <aop:around method=""/> </aop:aspect> <aop:aspect></aop:aspect></aop:config><aop:aspect> 配置一個切面;<aop:pointcut>配置一個切點,基于切點表達式;<aop:before>,<aop:after>,<aop:around>是定義不同類型的advise. aspectBean 是切面的處理bean:public class DataSourceInterceptor { public void before(JoinPoint jp) { DataSourceTypeManager.set(DataSources.SLAVE); }}然后掃描Service包:```python<context:component-scan base-package="net.aazj.service,net.aazj.aop" /><div class="se-preview-section-delimiter"></div>
最后在service上進行注解:
@Service("userService")@Transactionalpublic class UserServiceImpl implements UserService{ @Autowired private UserMapper userMapper; @Transactional (readOnly=true) public User getUser(int userId) { System.out.println("in UserServiceImpl getUser"); System.out.println(DataSourceTypeManager.get()); return userMapper.getUser(userId); } public void addUser(String username){ userMapper.addUser(username);// int i = 1/0; // 測試事物的回滾 } public void deleteUser(int id){ userMapper.deleteByPrimaryKey(id);// int i = 1/0; // 測試事物的回滾 } @Transactional (rollbackFor = BaseBusinessException.class) public void addAndDeleteUser(String username, int id) throws BaseBusinessException{ userMapper.addUser(username); this.m1(); userMapper.deleteByPrimaryKey(id); } private void m1() throws BaseBusinessException { throw new BaseBusinessException("xxx"); } public int insertUser(User user) { return this.userMapper.insert(user); }}<div class="se-preview-section-delimiter"></div>搞定。這種事務配置方式,不需要我們書寫pointcut表達式,而是我們在需要事務的類上進行注解。但是如果我們自己來寫切面的代碼時,還是要寫pointcut表達式。下面看一個例子(自己寫切面邏輯):首先去掃描 @Aspect 注解定義的 切面:<context:component-scan base-package="net.aazj.aop" /><div class="se-preview-section-delimiter"></div>切面代碼:
import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;@Aspect // for aop@Component // for auto scan@Order(0) // execute before @Transactionalpublic class DataSourceInterceptor { @Pointcut("execution(public * net.aazj.service..*.get*(..))") public void dataSourceSlave(){}; @Before("dataSourceSlave()") public void before(JoinPoint jp) { DataSourceTypeManager.set(DataSources.SLAVE); }}<div class="se-preview-section-delimiter"></div>我們使用到了 @Aspect 來定義一個切面;@Component是配合<context:component-scan/>,不然掃描不到;@Order定義了該切面切入的順序 ,因為在同一個切點,可能同時存在多個切面,那么在這多個切面之間就存在一個執行順序的問題。該例子是一個切換數據源的切面,那么他應該在 事務處理 切面之前執行,所以我們使用 @Order(0) 來確保先切換數據源,然后加入事務處理。@Order的參數越小,優先級越高,默認的優先級最低:/** * Annotation that defines ordering. The value is optional, and represents order value * as defined in the {@link Ordered} interface. Lower values have higher priority. * The default value is {@code Ordered.LOWEST_PRECEDENCE}, indicating * lowest priority (losing to any other specified order value). */@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})public @interface Order { /** * The order value. Default is {@link Ordered#LOWEST_PRECEDENCE}. * @see Ordered#getOrder() */ int value() default Ordered.LOWEST_PRECEDENCE;}<div class="se-preview-section-delimiter"></div>關于數據源的切換可以參加專門的博文:http://www.cnblogs.com/digdeep/p/4512368.html
方法:
@Before("execution(public * net.aazj.service..*.getUser(..)) && args(userId,..)") public void before3(int userId) { System.out.println("userId-----" + userId); }它會攔截 net.aazj.service 包下或者子包下的getUser方法,并且該方法的第一個參數必須是int型的, 那么使用切點表達式args(userId,..) 就可以使我們在切面中的處理方法before3中可以訪問這個參數。before2方法也讓我們知道也可以通過 JoinPoint 參數來獲得被攔截方法的參數數組。 JoinPoint 是每一個切面處理方法都具有的參數, @Around 類型的具有的參數類型為ProceedingJoinPoint。通過 JoinPoint或者 ProceedingJoinPoint 參數可以訪問到被攔截對象的一些信息(參見上面的 before2 方法)。新聞熱點
疑難解答