成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

深入了解Spring中的AOP

開發 開發工具
談起AOP,大家都知道是面向切面編程,但你真的了解Spring中的AOP嗎?Spring AOP、JDK動態代理、CGLIB、AspectJ之間又有什么關聯和區別?

 談起AOP,大家都知道是面向切面編程,但你真的了解Spring中的AOP嗎?Spring AOP、JDK動態代理、CGLIB、AspectJ之間又有什么關聯和區別?

[[284151]]

在Spring中AOP包含兩個概念,一是Spring官方基于JDK動態代理和CGLIB實現的Spring AOP;二是集成面向切面編程神器AspectJ。Spring AOP和AspectJ不是競爭關系,基于代理的框架的Spring AOP和成熟框架AspectJ都是有價值的,它們是互補的。

Spring無縫地將Spring AOP、IoC與AspectJ集成在一起,從而達到AOP的所有能力。Spring AOP默認將標準JDK動態代理用于AOP代理,可以代理任何接口。但如果沒有面向接口編程,只有業務類,則使用CGLIB。當然也可以全部強制使用CGLIB,只要設置proxy-target-class="true"。

AOP中的術語

通知(Advice)

Spring切面可以應用5種類型的通知:

前置通知(Before):在目標方法被調用之前調用通知功能;

后置通知(After):在目標方法完成之后調用通知,此時不會關心方法的輸出是什么;

返回通知(After-returning):在目標方法成功執行之后調用通知;

異常通知(After-throwing):在目標方法拋出異常后調用通知;

環繞通知(Around):通知包裹了被通知的方法,在被通知的方法調用之前和調用之后執行自定義的行為。

連接點(Join point)

切點(Poincut)

切面(Aspect)

引入(Introduction)

織入(Weaving)

這些術語的解釋,其他博文中很多,這里就不再贅述。

用2個例子來說明Spring AOP和AspectJ的用法

現在有這樣一個場景,頁面傳入參數當前頁page和每頁展示多少條數據rows,我們需要寫個攔截器將page、limit參數轉換成MySQL的分頁語句offset、rows。

先看Spring AOP實現

1、實現MethodInterceptor,攔截方法 

  1. public class MethodParamInterceptor implements MethodInterceptor { 
  2.  
  3. @Override 
  4.  
  5. @SuppressWarnings("unchecked"
  6.  
  7. public Object invoke(MethodInvocation invocation) throws Throwable { 
  8.  
  9. Object[] params = invocation.getArguments(); 
  10.  
  11. if (ArrayUtils.isEmpty(params)) { 
  12.  
  13. return invocation.proceed(); 
  14.  
  15.  
  16. for (Object param : params) { 
  17.  
  18. //如果參數類型是Map 
  19.  
  20. if (param instanceof Map) { 
  21.  
  22. Map paramMap = (Map) param; 
  23.  
  24. processPage(paramMap); 
  25.  
  26. break; 
  27.  
  28.  
  29.  
  30. return invocation.proceed(); 
  31.  
  32.  
  33. /** 
  34.  
  35.  
  36. * @param paramMap 
  37.  
  38. */ 
  39.  
  40. private void processPage(Map paramMap) { 
  41.  
  42. if (!paramMap.containsKey("page") && !paramMap.containsKey("limit")) { 
  43.  
  44. return
  45.  
  46.  
  47. int page = 1; 
  48.  
  49. int rows = 10; 
  50.  
  51. for (Map.Entry entry : paramMap.entrySet()) { 
  52.  
  53. String key = entry.getKey(); 
  54.  
  55. String value = entry.getValue().toString(); 
  56.  
  57. if ("page".equals(key)) { 
  58.  
  59. page = NumberUtils.toInt(value, page); 
  60.  
  61. else if ("limit".equals(key)) { 
  62.  
  63. rows = NumberUtils.toInt(value, rows); 
  64.  
  65. }else { 
  66.  
  67. //TODO 
  68.  
  69.  
  70.  
  71. int offset = (page - 1) * rows
  72.  
  73. paramMap.put("offset", offset); 
  74.  
  75. paramMap.put("rows"rows); 
  76.  
  77.  

2、定義后置處理器,將方法攔截件加入到advisor中。我們通過注解@Controller攔截所有的Controller,@RestController繼承于Controller,所以統一攔截了。 

  1. public class RequestParamPostProcessor extends AbstractBeanFactoryAwareAdvisingPostProcessor 
  2.  
  3. implements InitializingBean { 
  4.  
  5. private Class validatedAnnotationType = Controller.class; 
  6.  
  7. @Override 
  8.  
  9. public void afterPropertiesSet() throws Exception { 
  10.  
  11. Pointcut pointcut = new AnnotationMatchingPointcut(this.validatedAnnotationType, true); 
  12.  
  13. this.advisor = new DefaultPointcutAdvisor(pointcut, new MethodParamInterceptor()); 
  14.  
  15.  

 

3、萬事俱備只欠東風,Processor也寫好了,只需要讓Processor生效。

 

  1. @Configuration 
  2.  
  3. public class MethodInterceptorConfig { 
  4.  
  5. @Bean 
  6.  
  7. public RequestParamPostProcessor converter() { 
  8.  
  9. return new RequestParamPostProcessor(); 
  10.  
  11.  
  12. }` 

這里有個坑需要注意一下,如果在配置類中注入業務Bean。 

  1. @Configuration 
  2.  
  3. public class MethodInterceptorConfig { 
  4.  
  5. @Autowired 
  6.  
  7. private UserService userService; 
  8.  
  9. @Bean 
  10.  
  11. public RequestParamPostProcessor converter() { 
  12.  
  13. return new RequestParamPostProcessor(); 
  14.  
  15.  

啟動時,會出現:

  1. 2019-11-08 14:55:50.954 INFO 51396 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'sqlSessionFactory' of type [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 
  2.  
  3. 2019-11-08 14:55:50.960 INFO 51396 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'sqlSessionTemplate' of type [org.mybatis.spring.SqlSessionTemplate] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 
  4.  
  5. 2019-11-08 14:55:51.109 INFO 51396 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'rememberMapper' of type [com.sun.proxy.$Proxy84] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 
  6.  
  7. 2019-11-08 14:55:53.406 INFO 51396 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 

很多切面失效,如事務切面。這是因為注入了自定義的Bean,自定義的Bean優先級最低,由最低優先級的BeanPostProcessor來加載并完成初始化的。但為了加載其中的RequestParamPostProcessor,導致不得不優先裝載低優先級Bean,此時事務處理器的AOP等都還沒完成加載,注解事務初始化都失敗了。但Spring就提示了一個INFO級別的提示,然后剩下的Bean由最低優先級的BeanPostProcessor正常處理。

AspectJ方式實現切面 

  1. @Component@Aspect@Slf4jpublic class MethodParamInterceptor { 
  2.  
  3.     @Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)"
  4.     public void paramAspect() { 
  5.  
  6.     } 
  7.  
  8.  
  9.     @Before("paramAspect()"
  10.     public void beforeDataSource(JoinPoint joinPoint) { 
  11.         Arrays.stream(joinPoint.getArgs()).forEach(paramObject -> { 
  12.             if (paramObject instanceof Map) { 
  13.                 Map parameter = (Map) paramObject; 
  14.                 processPage(parameter); 
  15.             } 
  16.         }); 
  17.  
  18.     } 
  19.  
  20.     private void processPage(Map<String, Object> paramMap) { 
  21.         if (null == paramMap) { 
  22.             return
  23.         } 
  24.         if (!paramMap.containsKey("page") && !paramMap.containsKey("limit")) { 
  25.             return
  26.         } 
  27.         int page = 1; 
  28.         int rows = 10; 
  29.         for (Map.Entry<String, Object> entry : paramMap.entrySet()) { 
  30.             String key = entry.getKey(); 
  31.             String value = entry.getValue().toString(); 
  32.             if ("page".equals(key)) { 
  33.                 page = NumberUtils.toInt(value, page); 
  34.             } else if ("limit".equals(key)) { 
  35.                 rows = NumberUtils.toInt(value, rows); 
  36.             } 
  37.         } 
  38.         int offset = (page - 1) * rows
  39.         paramMap.put("offset", offset); 
  40.         paramMap.put("rows"rows); 
  41.     } 
  42.  
  43.  
  44.     @After("paramAspect()"
  45.     public void afterDataSource(JoinPoint joinPoint) { 
  46.  
  47.     } 

從上面兩個例子可以對比出SpringAOP和AspectJ的兩種不同用法,但達到的能力是一樣的。

Sping AOP在組織、抽象代碼場景中更加適合,AspectJ用于單純的切面來實現某項功能更加簡潔。

【本文是51CTO專欄機構“舟譜數據”的原創文章,微信公眾號“舟譜數據( id: zhoupudata)”】 

戳這里,看該作者更多好文

 

責任編輯:華軒 來源: 51CTO
相關推薦

2017-01-20 08:30:19

JavaScriptfor循環

2024-04-12 07:51:05

SpringBean初始化

2010-06-23 20:31:54

2010-07-13 09:36:25

2010-11-19 16:22:14

Oracle事務

2009-08-25 16:27:10

Mscomm控件

2022-08-26 13:48:40

EPUBLinux

2020-09-21 09:53:04

FlexCSS開發

2020-07-20 06:35:55

BashLinux

2024-03-07 16:12:46

Java字符串線程

2019-08-02 08:59:21

Token認證服務器

2018-02-24 13:21:02

2018-09-04 16:20:46

MySQ索引數據結構

2013-04-10 11:16:19

iPad的MouseE

2016-10-20 08:46:17

2021-09-03 08:27:47

FortinetSASE平臺安全

2011-07-18 15:08:34

2022-06-03 10:09:32

威脅檢測軟件

2010-11-15 11:40:44

Oracle表空間

2018-06-22 13:05:02

前端JavaScript引擎
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 狠狠干天天干 | 香蕉国产在线视频 | 亚洲精品一区二区在线观看 | 日韩久久久久 | 毛片站 | 午夜天堂| 欧美在线a | 天天操天天干天天爽 | 欧美色综合一区二区三区 | 亚洲一二三在线观看 | 一区二区三区四区av | 国产一区二区三区在线看 | 九色91视频 | 91av视频| 波多野结衣二区 | 日韩精品一区中文字幕 | 古装三级在线播放 | 福利网站在线观看 | 99热成人在线 | 在线国产视频观看 | 国产精品久久久久久久久久 | 国家aaa的一级看片 h片在线看 | 欧美日韩国产一区二区三区 | 韩日在线视频 | 欧美9999 | 日韩不卡一区二区三区 | 伊人精品一区二区三区 | 亚洲天堂中文字幕 | 亚洲精品视频在线观看视频 | 嫩草91在线| 成人在线激情 | 国产综合久久 | 国产伊人久久久 | 亚洲一二三区免费 | 91精品国产欧美一区二区成人 | 国产精品一级在线观看 | 欧美日韩三级 | 欧美黄色精品 | 在线国产一区二区 | 成人免费看黄网站在线观看 | 欧美日韩免费在线 |