-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava-exception.html
More file actions
615 lines (503 loc) · 68.4 KB
/
java-exception.html
File metadata and controls
615 lines (503 loc) · 68.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Java 异常知识点思考与总结 | happyJared - 博客</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="keywords" content="happyJared - 博客。保持勤學習的心,做好寫代碼這事。公众号:超级码里奥" />
<meta name="description" content="Java 中的异常可以是方法执行过程中引发的,也可以是通过 throw 语句手动抛出的。一旦程序运行过程中发生了异常,JRE 就会试图寻找异常处理程序来处理异常,用具体的异常对象来包装该异常。 Throwable 类是 Java 异常类的顶层父类,一个对象只有是 Throwable 类的(直接或者间接)实例,它才是一个异常对象,才可以被抛出(throw)或者捕获(catch),才能被异常处理机制">
<meta name="keywords" content="java exception,java error">
<meta property="og:type" content="article">
<meta property="og:title" content="Java 异常知识点思考与总结">
<meta property="og:url" content="https://blog.mariojd.cn/java-exception.html">
<meta property="og:site_name" content="happyJared - 博客">
<meta property="og:description" content="Java 中的异常可以是方法执行过程中引发的,也可以是通过 throw 语句手动抛出的。一旦程序运行过程中发生了异常,JRE 就会试图寻找异常处理程序来处理异常,用具体的异常对象来包装该异常。 Throwable 类是 Java 异常类的顶层父类,一个对象只有是 Throwable 类的(直接或者间接)实例,它才是一个异常对象,才可以被抛出(throw)或者捕获(catch),才能被异常处理机制">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://image.mariojd.cn/happyjared/2018125/accba531a365e6ae39614ebfa3273900.png">
<meta property="og:image" content="http://image.mariojd.cn/happyjared/2018125/Java-Checked-Vs-Unchecked-Exception-Crunchify.png">
<meta property="og:image" content="http://image.mariojd.cn/happyjared/2018125/858860-20170911121451422-233079767.png">
<meta property="og:image" content="http://image.mariojd.cn/happyjared/2018125/20181205151242.png">
<meta property="og:updated_time" content="2019-06-02T11:41:35.965Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java 异常知识点思考与总结">
<meta name="twitter:description" content="Java 中的异常可以是方法执行过程中引发的,也可以是通过 throw 语句手动抛出的。一旦程序运行过程中发生了异常,JRE 就会试图寻找异常处理程序来处理异常,用具体的异常对象来包装该异常。 Throwable 类是 Java 异常类的顶层父类,一个对象只有是 Throwable 类的(直接或者间接)实例,它才是一个异常对象,才可以被抛出(throw)或者捕获(catch),才能被异常处理机制">
<meta name="twitter:image" content="http://image.mariojd.cn/happyjared/2018125/accba531a365e6ae39614ebfa3273900.png">
<link rel="icon" href="/favicon.ico">
<link href="//cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="/css/style.css">
<script src="/js/pace.min.js"></script>
<!-- Google Analytics -->
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-127138870-1', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "//hm.baidu.com/hm.js?7d6076888c62ff336f5b580c57fd4141";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
<div style="display: none;">
<script src="//s22.cnzz.com/z_stat.php?id=1274993267&web_id=1274993267" language="JavaScript"></script>
</div>
</head>
<body>
<div id="container">
<header id="header">
<!--<div id="banner"></div>-->
<div id="header-outer">
<div id="header-menu" class="header-menu-pos animated">
<div class="header-menu-container">
<a href="/" class="left">
<span class="site-title"></span>
</a>
<nav id="header-menu-nav" class="right">
<a href="/">
<i class="fa fa-h-square"></i>
<span>主页</span>
</a>
<a href="/life">
<i class="fa fa-battery-three-quarters"></i>
<span>岁月</span>
</a>
<a target="_blank" href="https://mp.mariojd.cn">
<i class="fa fa-dot-circle-o"></i>
<span>洞见</span>
</a>
<a href="/archives">
<i class="fa fa-window-restore"></i>
<span>归档</span>
</a>
<a target="_blank" href="http://jianshu.mariojd.cn">
<i class="fa fa-superpowers"></i>
<span>简统</span>
</a>
<a href="/about">
<i class="fa fa-user"></i>
<span>关于</span>
</a>
</nav>
<a class="mobile-header-menu-button">
<i class="fa fa-bars"></i>
</a>
</div>
</div>
<div id="header-row">
<div id="logo">
<a href="/">
<img src="/images/logo.png" alt="logo">
</a>
</div>
<div class="header-info">
<!--<div id="header-title">-->
<!---->
<!--</div>-->
<div id="header-description">
<div id="hitokoto" style="height:24px;line-height:24px;">
保持勤學習的心,做好寫代碼這事
</div>
</div>
</div>
<nav class="header-nav">
<div class="social">
<a title="github" target="_blank" href="//github.com/happyjared">
<i class="fa fa-github fa-2x"></i></a>
<a title="简书" target="_blank" href="//www.jianshu.com/u/000a530f461c">
<i class="fa fa-book fa-2x"></i></a>
<a title="企鹅号" target="_blank" href="//wpa.qq.com/msgrd?v=3&uin=1394223902&site=qq&menu=yes">
<i class="fa fa-qq fa-2x"></i></a>
<a title="公众号" target="_blank" href="//image.mariojd.cn/qrcode_for_supermmario_1280.jpg">
<i class="fa fa-wechat fa-2x"></i></a>
</div>
</nav>
</div>
</div>
</header>
<div class="outer">
<section id="main" class="body-wrap"><article id="post-Java 异常知识点思考与总结" class="article article-type-post" itemscope itemprop="blogPost">
<div class="article-inner">
<header class="article-header">
<h1 class="post-title" itemprop="name">
Java 异常知识点思考与总结
</h1>
<div class="post-title-bar">
<ul>
<li>
<i class="fa fa-book"></i>
<a href="/categories/Java/">Java</a>
</li>
<li>
<i class="fa fa-calendar"></i> 2018-12-05
</li>
<li>
<i class="fa fa-eye"></i>
<span id="busuanzi_value_page_pv"></span>
</li>
</ul>
</div>
</header>
<div class="article-entry post-content" itemprop="articleBody">
<!--
self_category:
self_tags:
osChina_sys_category:
csdn_article_category:
csdn_blog_category:
-->
<p>Java 中的异常可以是方法执行过程中引发的,也可以是通过 throw 语句手动抛出的。一旦程序运行过程中发生了异常,JRE 就会试图寻找异常处理程序来处理异常,用具体的异常对象来包装该异常。</p>
<p>Throwable 类是 Java 异常类的顶层父类,一个对象只有是 Throwable 类的(直接或者间接)实例,它才是一个异常对象,才可以被抛出(throw)或者捕获(catch),才能被异常处理机制识别和处理。除了 JDK 中内建的常用异常类,还允许我们自定义异常。</p>
<h3 id="异常划分"><a href="#异常划分" class="headerlink" title="异常划分"></a>异常划分</h3><p><img src="http://image.mariojd.cn/happyjared/2018125/accba531a365e6ae39614ebfa3273900.png" alt="Java异常结构图"></p>
<p>可以看到,Throwable 派生出 Error 和 Exception ,这体现了 Java 平台设计者针对不同异常情况的合理分类。其中,Exception 是指应用正常运行中,可以被预料的意外情况,程序捕获后可以进行相应的处理。Error 是指在正常情况下,不大可能出现的情况,而绝大多数的 Error 都会导致程序进入非正常的、不可恢复的状态,Error 类错误通常不可以被捕获,如 OutOfMemoryError、NoClassDefFoundError。</p>
<p><img src="http://image.mariojd.cn/happyjared/2018125/Java-Checked-Vs-Unchecked-Exception-Crunchify.png" alt="Checked && Unchecked Exception"></p>
<p>对于程序来说,异常又可以划分为应检查(checked)异常和不检查(unchecked)异常,应检查异常要求必须在代码里进行显式捕获和处理,javac 会在编译期间就进行检查。</p>
<ul>
<li>不检查异常(unchecked exception):</li>
</ul>
<p>不检查异常就是所谓的运行时异常,通常是可以通过编码来避免的一些逻辑错误,包括 Error 和 RuntimeException 以及他们的子类。javac在编译时,不会提示和发现此类异常,即不要求通过代码显示处理这些异常。对于这些异常,我们应该修正代码,而不是通过异常处理器来解决,如除0错误:<code>ArithmeticException</code>,强制类型转换错误:<code>ClassCastException</code>,数组越界异常:<code>ArrayIndexOutOfBoundsException</code>,使用了空对象出现的 <code>NullPointerException</code> 等。</p>
<ul>
<li>应检查异常(checked exception):</li>
</ul>
<p>除了 Error 和 RuntimeException 的其它异常。javac 强制要求处理的异常,可以用 try-catch-finally 或 try-with-resources 语句捕获并处理,也可以使用 throws 往上抛出,否则编译不会通过。应检查异常通常是由程序的运行环境所导致的,而这些在程序运行过程中是无法提前预知的,于是代码中就应该为这样的异常提前准备,如SQLException , IOException和ClassNotFoundException 等。</p>
<p><img src="http://image.mariojd.cn/happyjared/2018125/858860-20170911121451422-233079767.png" alt="异常栈"></p>
<p>函数通常是层级调用的,进而形成调用栈,而异常则是执行某个函数时所引发的。因此,只要在方法调用的过程中发生了异常,那么他的所有 caller 都会被异常影响,当这些被影响的函数以异常信息输出时,就形成的了异常追踪栈(如上图所示)。所以,异常最先发生的地方,也叫做异常抛出点。</p>
<h3 id="try…catch…finally-语句块"><a href="#try…catch…finally-语句块" class="headerlink" title="try…catch…finally 语句块"></a>try…catch…finally 语句块</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">try</span> (ServletOutputStream outputStream = response.getOutputStream()) { <span class="comment">// Try-with-resources</span></span><br><span class="line"> <span class="comment">// 1. try块中放可能发生异常的代码</span></span><br><span class="line"> <span class="comment">// 1.1 如果执行完try且不发生异常,则接着去执行finally块和finally后面的代码(如果有的话)</span></span><br><span class="line"> <span class="comment">// 1.2 如果发生了异常,则会先尝试去匹配catch块,最后再执行finally块(如果有的话)</span></span><br><span class="line">} <span class="keyword">catch</span> (ClassCastException | IndexOutOfBoundsException e) { <span class="comment">// Multiple catch</span></span><br><span class="line"> <span class="comment">// 1. 每一个catch块用于捕获并处理一个特定的异常,或者这个异常类型的子类。Java7提供的multiple catch新特性,可以将多个异常声明在一个catch中</span></span><br><span class="line"> <span class="comment">// 2. catch后面的括号定义了异常类型和异常参数。如果异常与之匹配且是最先匹配到的,则虚拟机将使用这个catch块来处理异常</span></span><br><span class="line"> <span class="comment">// 3. 在catch块中可以使用这个块的异常参数来获取异常的相关信息。异常参数是这个catch块中的局部变量,其它块不能访问</span></span><br><span class="line"> <span class="comment">// 4. 如果当前try块中发生的异常在后续的所有catch中都没捕获到,则先去执行finally,然后到这个函数的外部caller中去匹配异常处理器</span></span><br><span class="line"> <span class="comment">// 5. 如果try中没有发生异常,则所有的catch块将被忽略</span></span><br><span class="line">} <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> <span class="comment">// 1. 异常匹配是按照catch块的顺序从上往下寻找的,只有第一个匹配的catch会得到执行。匹配时,不仅运行精确匹配,也支持父类匹配</span></span><br><span class="line"> <span class="comment">// 2. 如果同一个try块下的多个catch异常类型有父子关系,应该将子类异常放在前面,父类异常放在后面,确保每个catch块都有其存在的意义</span></span><br><span class="line"> <span class="comment">// 3. 异常处理就是将执行控制流从异常发生的地方转移到能够处理这种异常的地方。当一个函数的某条语句发生异常时,这条语句的后面的语句就不会再执行了,它失去了焦点</span></span><br><span class="line">} <span class="keyword">finally</span> { </span><br><span class="line"> <span class="comment">// 1. finally块是可选的</span></span><br><span class="line"> <span class="comment">// 2. 无论异常是否发生,异常是否匹配被处理,finally都最终会执行</span></span><br><span class="line"> <span class="comment">// 3. 一个try至少要有一个catch块,否则至少要有1个finally块。但是finally不是用来处理异常的,finally不会捕获异常</span></span><br><span class="line"> <span class="comment">// 4. finally主要做一些资源的清理工作,比如流的关闭,数据库连接的关闭等;Java7及以后的版本中,更是推荐使用try-with-resources这种新特性来简化这些操作 </span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="throws-异常声明"><a href="#throws-异常声明" class="headerlink" title="throws 异常声明"></a>throws 异常声明</h3><p>throws 是另一种处理异常的方式,它不同于 try…catch…finally,throws仅仅是将函数中可能出现的异常向调用者声明,其本身并不进行处理。</p>
<p>采取这种异常处理的原因大多是:方法编写者本身不知道如何处理这样的异常,或者说让调用方来处理会更好,从而让调用方来为可能发生的异常负责。</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">example</span><span class="params">()</span> <span class="keyword">throws</span> IOException </span>{ </span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="finally-块"><a href="#finally-块" class="headerlink" title="finally 块"></a>finally 块</h3><p>try块中的代码执行完后,finally块是一定执行的。但也有一种比较特殊的情况,就是在这之前执行了<code>System.exit()</code>。</p>
<p>finally块通常用来做资源的释放、关闭文件等操作。良好的编程习惯是:在try块中打开资源,在finally块中清理并释放这些资源,Java7之后更是推荐直接使用try-with-resources。</p>
<p>下面简单总结一下:</p>
<ol>
<li><p>finally块没有处理异常的能力,处理异常的只能是catch块;</p>
</li>
<li><p>在同一try…catch…finally块中 ,如果try中抛出异常,且有匹配的catch块,则先执行catch块,再执行finally块。如果没有catch块匹配,则先执行finally,然后去外面的调用者中寻找合适的catch块;</p>
</li>
<li><p>在同一try…catch…finally块中 ,try发生异常,且匹配的catch块中处理异常时也抛出异常,后面的finally还是会先执行,最后才去外围调用者中寻找合适的catch块。</p>
</li>
</ol>
<p>补充几点开发建议:</p>
<ol>
<li>不要在fianlly中使用return</li>
<li>不要在finally中向外抛出异常</li>
<li>不要在finally中做除了释放资源的其它的事情</li>
<li>用try-with-resources避免finally</li>
</ol>
<h3 id="自定义异常"><a href="#自定义异常" class="headerlink" title="自定义异常"></a>自定义异常</h3><p>扩展自Exception类的自定义异常,属于应检查异常(checked exception)。如果要自定义非检查异常(unchecked exception),继承RuntimeException即可。</p>
<p>通常情况下,自定义的异常应该总是包含如下的构造器,具体可以参考jdk中自带的异常是如何定义的:</p>
<ul>
<li>一个无参构造函数</li>
<li>一个带有 String 参数的构造函数,并传递给父类的构造函数。</li>
<li>一个带有 String 参数和 Throwable 参数,并都传递给父类构造函数</li>
<li>一个带有 Throwable 参数的构造函数,并传递给父类的构造函数。</li>
</ul>
<p><img src="http://image.mariojd.cn/happyjared/2018125/20181205151242.png" alt="IOException"></p>
<h3 id="异常案例"><a href="#异常案例" class="headerlink" title="异常案例"></a>异常案例</h3><p>异常处理可谓也是一门艺术活。下面列举了一些错误的、常见的异常处理方式,你可以通过阅读代码来提前思考,判断这些异常处理中,具体有哪些不当之处:</p>
<h4 id="示例一"><a href="#示例一" class="headerlink" title="示例一"></a>示例一</h4><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">try</span> {</span><br><span class="line"> Thread.sleep(<span class="number">1000L</span>);</span><br><span class="line">} <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> <span class="comment">// do nothing...</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li><p>尽量不要捕获类似 Exception 这样的通用异常,而是应该捕获特定异常,在这里 Thread.sleep() 抛出的 InterruptedException</p>
</li>
<li><p>不要生吞(swallow)异常,这是异常处理中特别要注意的事情,因为很可能会导致非常难以诊断的诡异情况。如果我们没有把异常抛出来,或者也没有输出到日志(Logger)之类,程序可能在后续代码以不可控的方式结束。没人能够轻易判断究竟是哪里抛出了异常,以及是什么原因产生了异常</p>
</li>
</ul>
<h4 id="示例二"><a href="#示例二" class="headerlink" title="示例二"></a>示例二</h4><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">try</span> {</span><br><span class="line"> <span class="comment">// …</span></span><br><span class="line">} <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>开发或测试环境中,上面这段代码是没有问题的,但在产品代码中,是绝对不允许这样处理的。来看看printStackTrace()的文档,开头就是“Prints this throwable and its backtrace to the <strong>standard error stream</strong>”。问题就在这里,在稍微复杂一点的应用中,标准错误流(STERR)并不是个合适的输出选项,因为你很难判断异常到底输出到哪里了。</p>
<h4 id="示例三"><a href="#示例三" class="headerlink" title="示例三"></a>示例三</h4><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">(String fileName)</span> </span>{</span><br><span class="line"> <span class="comment">// 提前校验参数是否合法</span></span><br><span class="line"> Assert.isTrue(!<span class="string">""</span>.equals(fileName) && Objects.nonNull(fileName), <span class="string">"file name is not empty"</span>);</span><br><span class="line"> File file = <span class="keyword">new</span> File(fileName);</span><br><span class="line"> <span class="comment">// ...</span></span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>遵循 <strong>Throw early, catch later</strong> 原则。如果 fileName 是 null 或者 空字符串,那么后面程序获取文件时肯定会抛出异常。提前校验并且抛出异常,可以更加清晰地反映问题。</p>
<h3 id="示例四"><a href="#示例四" class="headerlink" title="示例四"></a>示例四</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserService</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Resource</span></span><br><span class="line"> <span class="keyword">private</span> UserMapper userMapper;</span><br><span class="line"> </span><br><span class="line"> <span class="meta">@Transactional</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">insert</span><span class="params">(User user)</span> </span>{</span><br><span class="line"> <span class="comment">// 插入用户信息</span></span><br><span class="line"> userMapper.insertUser(user);</span><br><span class="line"> <span class="comment">// 手动抛出异常</span></span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> SQLException(<span class="string">"数据库异常"</span>);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>上述这段代码中,异常并没有被捕获到,所以事务并不会回滚。 Spring Boot 默认的事务规则是遇到运行异常(RuntimeException)和程序错误(Error)才会回滚。而 SQLException 是非运行异常,继承自 Exception。解决上述问题,需要在 @Transactional 注解中使用 rollbackFor 属性来指定异常:<code>@Transactional(rollbackFor = Exception.class)</code></p>
<h3 id="示例五"><a href="#示例五" class="headerlink" title="示例五"></a>示例五</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserService</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Resource</span></span><br><span class="line"> <span class="keyword">private</span> UserMapper userMapper;</span><br><span class="line"> </span><br><span class="line"> <span class="meta">@Transactional</span>(rollbackFor = Exception.class)</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">insert</span><span class="params">(User user)</span> </span>{</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="comment">// 插入用户信息</span></span><br><span class="line"> userMapper.insertUser(user);</span><br><span class="line"> <span class="comment">// 手动抛出异常</span></span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> SQLException(<span class="string">"数据库异常"</span>);</span><br><span class="line"> } <span class="keyword">catch</span> (Exception e) {</span><br><span class="line"> <span class="comment">// 异常处理逻辑</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>同样的,事务并没有因为抛出异常而回滚,这是因为 try…catch 把异常生吞了,这个细节往往比上面那个坑更加难发现。</p>
<h3 id="示例六"><a href="#示例六" class="headerlink" title="示例六"></a>示例六</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserService</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Resource</span></span><br><span class="line"> <span class="keyword">private</span> UserMapper userMapper;</span><br><span class="line"> </span><br><span class="line"> <span class="meta">@Transactional</span>(rollbackFor = Exception.class)</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">synchronized</span> <span class="keyword">void</span> <span class="title">insert</span><span class="params">(User user)</span> </span>{</span><br><span class="line"> <span class="comment">// 插入用户信息</span></span><br><span class="line"> userMapper.insertUser(user);</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p>上述代码中,synchronized 并不会生效,原因是因为事务的范围比锁的范围大。加锁的那部分代码执行完之后,锁释放掉了但事务还没结束,此时假设另外一个线程进来了,事务没结束的话,插入动作就会产生脏数据。解决办法有两种,第一,去掉事务(不推荐);第二,在调用该方法的地方加锁,保证锁的范围比事务的范围大即可。</p>
<h3 id="性能分析"><a href="#性能分析" class="headerlink" title="性能分析"></a>性能分析</h3><p>从性能的角度来审视一下 Java 的异常处理机制,这里有两个相对昂贵的地方:</p>
<ul>
<li><p>try-catch 代码段会产生额外的性能开销,换个角度说,它往往会影响 JVM 对代码进行优化,因此建议仅捕获有必要的代码段,尽量不要用一个大的 try 块包住整段的代码;与此同时,利用异常控制代码流程,也不是一个好主意,这远比通常意义上的条件语句(if/else、switch)要低效</p>
</li>
<li><p>每实例化一个 Exception,都会对当前的栈进行快照,这是一个相对比较重的操作。如果发生的非常频繁,这个性能开销就不能被忽略了</p>
</li>
</ul>
<h3 id="推荐阅读"><a href="#推荐阅读" class="headerlink" title="推荐阅读"></a>推荐阅读</h3><blockquote>
<p><a href="https://monkeysayhi.github.io/2017/10/04/Java%E5%B8%B8%E7%94%A8%E5%BC%82%E5%B8%B8%E6%95%B4%E7%90%86/" target="_blank" rel="noopener">Java 常用异常整理</a><br><a href="http://www.cnblogs.com/lulipro/p/7504267.html" target="_blank" rel="noopener">Java 中的异常和处理详解</a><br><a href="http://lrwinx.github.io/2016/04/28/%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E8%AE%BE%E8%AE%A1java%E5%BC%82%E5%B8%B8/" target="_blank" rel="noopener">如何优雅的设计 Java 异常</a><br><a href="http://www.rowkey.me/blog/2017/09/17/java-exception/" target="_blank" rel="noopener">Java 中 9 个处理 Exception 的最佳实践</a><br><a href="http://www.importnew.com/5616.html" target="_blank" rel="noopener">改进异常处理的6条建议</a><br><a href="https://blog.csdn.net/csdnsevenn/article/details/84645568" target="_blank" rel="noopener">程序员如何处理被 “吃” 掉的异常?</a><br><a href="http://www.cnblogs.com/xinzhao/p/4902295.html" target="_blank" rel="noopener">Spring MVC 异常处理详解</a><br><a href="http://www.sohu.com/a/237842130_684445" target="_blank" rel="noopener">谈谈异常</a> </p>
<p>欢迎关注我的个人公众号:超级码里奥<br>如果这对您有帮助,欢迎点赞和分享,转载请注明出处 </p>
</blockquote>
<div class="post-copyright">
<div class="content">
<p>最后更新: 2019年06月02日 19:41</p>
<p>原始链接: <a class="post-url" href="/java-exception.html" title="Java 异常知识点思考与总结">https://blog.mariojd.cn/java-exception.html</a></p>
<footer>
<a href="https://blog.mariojd.cn">
<img src="/images/logo.png" alt="happyJared">
happyJared
</a>
</footer>
</div>
</div>
<div class="page-reward">
<a id="rewardBtn" href="javascript:;">赏</a>
</div>
<div id="reward" class="post-modal reward-lay">
<a class="close" href="javascript:;" id="reward-close">×</a>
<span class="reward-title">
<i class="icon icon-quote-left"></i>
支持创作
<i class="icon icon-quote-right"></i>
</span>
<div class="reward-content">
<div class="reward-code">
<img id="rewardCode" src="/images/wechat_code.jpg" alt="打赏二维码">
</div>
<div class="reward-select">
<label class="reward-select-item checked" data-id="wechat" data-wechat="/images/wechat_code.jpg">
<img class="reward-select-item-wechat" src="/images/wechat.png" alt="微信">
</label>
<label class="reward-select-item" data-id="alipay" data-alipay="/images/alipay_code.jpg">
<img class="reward-select-item-alipay" src="/images/alipay.png" alt="支付宝">
</label>
</div>
</div>
</div>
</div>
<footer class="article-footer">
<div class="post-share">
<a href="javascript:;" id="share-sub" class="post-share-fab">
<i class="fa fa-share-alt"></i>
</a>
<div class="post-share-list" id="share-list">
<ul class="share-icons">
<li>
<a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://blog.mariojd.cn/java-exception.html&title=《Java 异常知识点思考与总结》 — happyJared - 博客&pic=https://image.mariojd.cn/happyjared/2018123/java-exception.jpg" data-title="微博">
<i class="fa fa-weibo"></i>
</a>
</li>
<li>
<a class="weixin share-sns" id="wxFab" href="javascript:;" data-title="微信">
<i class="fa fa-weixin"></i>
</a>
</li>
<li>
<a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://blog.mariojd.cn/java-exception.html&title=《Java 异常知识点思考与总结》 — happyJared - 博客&source=Java developer, like Python and Javascript too,为人乐观、积极、向上、勤奋、和善、好相处,讲卫生,爱学习,爱..." data-title="QQ">
<i class="fa fa-qq"></i>
</a>
</li>
<li>
<a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://blog.mariojd.cn/java-exception.html" data-title="Facebook">
<i class="fa fa-facebook"></i>
</a>
</li>
<li>
<a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Java 异常知识点思考与总结》 — happyJared - 博客&url=https://blog.mariojd.cn/java-exception.html&via=https://blog.mariojd.cn" data-title="Twitter">
<i class="fa fa-twitter"></i>
</a>
</li>
<li>
<a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://blog.mariojd.cn/java-exception.html" data-title="Google+">
<i class="fa fa-google-plus"></i>
</a>
</li>
</ul>
</div>
</div>
<div class="post-modal wx-share" id="wxShare">
<a class="close" href="javascript:;" id="wxShare-close">×</a>
<p>扫一扫,分享到微信</p>
<img src="//api.qrserver.com/v1/create-qr-code/?data=https://blog.mariojd.cn/java-exception.html" alt="微信分享二维码">
</div>
<div class="mask"></div>
<ul class="article-footer-menu">
<li class="article-footer-tags">
<i class="fa fa-tags"></i>
<a href="/tags/javaexception/" class="color5">java exception</a>
<a href="/tags/javaerror/" class="color1">java error</a>
</li>
</ul>
</footer>
</div>
</article>
<aside class="post-toc-pos post-toc-top" id="post-toc">
<nav class="post-toc-wrap">
<ol class="post-toc"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#异常划分"><span class="post-toc-text">异常划分</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#try…catch…finally-语句块"><span class="post-toc-text">try…catch…finally 语句块</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#throws-异常声明"><span class="post-toc-text">throws 异常声明</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#finally-块"><span class="post-toc-text">finally 块</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#自定义异常"><span class="post-toc-text">自定义异常</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#异常案例"><span class="post-toc-text">异常案例</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#示例一"><span class="post-toc-text">示例一</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#示例二"><span class="post-toc-text">示例二</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#示例三"><span class="post-toc-text">示例三</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#示例四"><span class="post-toc-text">示例四</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#示例五"><span class="post-toc-text">示例五</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#示例六"><span class="post-toc-text">示例六</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#性能分析"><span class="post-toc-text">性能分析</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#推荐阅读"><span class="post-toc-text">推荐阅读</span></a></li></ol>
</nav>
</aside>
<nav id="article-nav">
<a href="/tips-for-chrome-developer-tools.html" id="article-nav-newer" class="article-nav-link-wrap">
<span class="article-nav-title">
<i class="fa fa-hand-o-left" aria-hidden="true"></i>
Chrome 开发者工具的小技巧
</span>
</a>
<a href="/time-annotation-in-spring-data-jpa.html" id="article-nav-older" class="article-nav-link-wrap">
<span class="article-nav-title">Spring Data JPA 的时间注解:@CreatedDate 和 @LastModifiedDate</span>
<i class="fa fa-hand-o-right" aria-hidden="true"></i>
</a>
</nav>
<div id="SOHUCS" sid="Java 异常知识点思考与总结" ></div>
<script type="text/javascript">
(function(){
var appid = 'cytsi4W0L';
var conf = 'prod_302a63175740fd2ee2587f9345477b4c';
var width = window.innerWidth || document.documentElement.clientWidth;
if (width < 960) {
window.document.write('<script id="changyan_mobile_js" charset="utf-8" type="text/javascript" src="https://changyan.sohu.com/upload/mobile/wap-js/changyan_mobile.js?client_id=' + appid + '&conf=' + conf + '"><\/script>'); } else { var loadJs=function(d,a){var c=document.getElementsByTagName("head")[0]||document.head||document.documentElement;var b=document.createElement("script");b.setAttribute("type","text/javascript");b.setAttribute("charset","UTF-8");b.setAttribute("src",d);if(typeof a==="function"){if(window.attachEvent){b.onreadystatechange=function(){var e=b.readyState;if(e==="loaded"||e==="complete"){b.onreadystatechange=null;a()}}}else{b.onload=a}}c.appendChild(b)};loadJs("https://changyan.sohu.com/upload/changyan.js",function(){window.changyan.api.config({appid:appid,conf:conf})}); } })(); </script>
</section>
</div>
<footer id="footer">
<div class="outer">
<div id="footer-info" class="inner">
<p>
<span id="busuanzi_container_site_uv" style='display:none'>
访客数:<span id="busuanzi_value_site_uv"></span>
</span>
<span id="busuanzi_container_site_pv" style='display:none'>
访问量:<span id="busuanzi_value_site_pv"></span>
</span>
<span style='margin-left:5px'>
粤ICP备17033077号-1
</span>
</p>
<p>
<!-- Powered by <a href="http://hexo.io/" target="_blank">Hexo</a> -->
<!-- Theme <a href="//github.com/wongminho/hexo-theme-miho" target="_blank">MiHo</a> -->
Copyright © 2018 - 2020 happyJared
<!-- Hosted by <a href="https://pages.coding.me" style="font-weight: bold">Coding Pages</a> -->
<script type="text/javascript" src="//js.users.51.la/19683707.js"></script>
<!-- <script type="text/javascript">var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1274993267'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s19.cnzz.com/z_stat.php%3Fid%3D1274993267%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));</script> -->
<br>
</p>
</div>
</div>
</footer>
<script>
(function(){
var bp = document.createElement('script');
var curProtocol = window.location.protocol.split(':')[0];
if (curProtocol === 'https') {
bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
}
else {
bp.src = 'http://push.zhanzhang.baidu.com/push.js';
}
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(bp, s);
})();
</script>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<script src="https://v1.hitokoto.cn/?encode=js&select=%23hitokoto" defer></script>
<script src="//cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script>
var mihoConfig = {
root: "https://blog.mariojd.cn",
animate: false,
isHome: false,
share: true,
reward: 1
}
</script>
<div class="sidebar">
<div id="sidebar-search" title="Search">
<i class="fa fa-search"></i>
</div>
<div id="sidebar-category" title="Categories">
<i class="fa fa-book"></i>
</div>
<div id="sidebar-tag" title="Tags">
<i class="fa fa-tags"></i>
</div>
<div id="sidebar-top">
<span class="sidebar-top-icon"><i class="fa fa-angle-up"></i></span>
</div>
</div>
<div class="sidebar-menu-box" id="sidebar-menu-box">
<div class="sidebar-menu-box-container">
<div id="sidebar-menu-box-categories">
<a class="category-link" href="/categories/Chocolatey/">Chocolatey</a><a class="category-link" href="/categories/Docker/">Docker</a><a class="category-link" href="/categories/Docker/Elasticsearch/">Elasticsearch</a><a class="category-link" href="/categories/Docker/MySQL/">MySQL</a><a class="category-link" href="/categories/Docker/PostgreSQL/">PostgreSQL</a><a class="category-link" href="/categories/Docker/Redis/">Redis</a><a class="category-link" href="/categories/IDEA/">IDEA</a><a class="category-link" href="/categories/Java/">Java</a><a class="category-link" href="/categories/Java/Lombok/">Lombok</a><a class="category-link" href="/categories/Java/Spring-Boot/">Spring Boot</a><a class="category-link" href="/categories/Java/Spring-Boot/Swagger/">Swagger</a><a class="category-link" href="/categories/Java/热部署/">热部署</a><a class="category-link" href="/categories/Jmeter/">Jmeter</a><a class="category-link" href="/categories/Linux/">Linux</a><a class="category-link" href="/categories/Micro-Service/">Micro Service</a><a class="category-link" href="/categories/Python/">Python</a><a class="category-link" href="/categories/Python/Selenium/">Selenium</a><a class="category-link" href="/categories/Python/Selenium/Windows-Batch/">Windows Batch</a><a class="category-link" href="/categories/Redis/">Redis</a><a class="category-link" href="/categories/Spring/">Spring</a><a class="category-link" href="/categories/Web/">Web</a><a class="category-link" href="/categories/hexo/">hexo</a><a class="category-link" href="/categories/python/">python</a><a class="category-link" href="/categories/websocket/">websocket</a><a class="category-link" href="/categories/中间件/">中间件</a><a class="category-link" href="/categories/前端/">前端</a><a class="category-link" href="/categories/学习资源/">学习资源</a><a class="category-link" href="/categories/开源/">开源</a><a class="category-link" href="/categories/开源/YApi/">YApi</a><a class="category-link" href="/categories/开源/YApi/Swagger/">Swagger</a><a class="category-link" href="/categories/开源和中间件/">开源和中间件</a><a class="category-link" href="/categories/开源项目/">开源项目</a><a class="category-link" href="/categories/抓包工具/">抓包工具</a><a class="category-link" href="/categories/数据库中间件/">数据库中间件</a><a class="category-link" href="/categories/数据库中间件/Mycat/">Mycat</a><a class="category-link" href="/categories/编辑器/">编辑器</a><a class="category-link" href="/categories/编辑器/个人感悟/">个人感悟</a><a class="category-link" href="/categories/编辑器/快捷键/">快捷键</a><a class="category-link" href="/categories/设计模式/">设计模式</a><a class="category-link" href="/categories/随笔/">随笔</a>
</div>
<div id="sidebar-menu-box-tags">
<a href="/tags/AcpectJ/" style="font-size: 10px;">AcpectJ</a> <a href="/tags/Aop/" style="font-size: 10px;">Aop</a> <a href="/tags/Chocolatey/" style="font-size: 10px;">Chocolatey</a> <a href="/tags/Config-Center/" style="font-size: 10.91px;">Config Center</a> <a href="/tags/Ctrip-Apollo/" style="font-size: 10.91px;">Ctrip Apollo</a> <a href="/tags/Eclipse/" style="font-size: 10.91px;">Eclipse</a> <a href="/tags/Faker/" style="font-size: 10.91px;">Faker</a> <a href="/tags/Fiddler/" style="font-size: 10px;">Fiddler</a> <a href="/tags/GitHub-Pages/" style="font-size: 10.91px;">GitHub Pages</a> <a href="/tags/Hexo/" style="font-size: 10px;">Hexo</a> <a href="/tags/IDEA/" style="font-size: 20px;">IDEA</a> <a href="/tags/JSR-303/" style="font-size: 10px;">JSR 303</a> <a href="/tags/Java/" style="font-size: 16.36px;">Java</a> <a href="/tags/Java类库/" style="font-size: 10px;">Java类库</a> <a href="/tags/JetBrains/" style="font-size: 18.18px;">JetBrains</a> <a href="/tags/Jmeter/" style="font-size: 11.82px;">Jmeter</a> <a href="/tags/Lombok/" style="font-size: 10px;">Lombok</a> <a href="/tags/Micro-Service/" style="font-size: 10.91px;">Micro Service</a> <a href="/tags/Mycat/" style="font-size: 12.73px;">Mycat</a> <a href="/tags/ORM/" style="font-size: 10.91px;">ORM</a> <a href="/tags/Peewee/" style="font-size: 10px;">Peewee</a> <a href="/tags/Python/" style="font-size: 11.82px;">Python</a> <a href="/tags/RestTemplate/" style="font-size: 10px;">RestTemplate</a> <a href="/tags/SQLAlchemy/" style="font-size: 10px;">SQLAlchemy</a> <a href="/tags/Spring/" style="font-size: 10.91px;">Spring</a> <a href="/tags/Spring-Boot/" style="font-size: 13.64px;">Spring Boot</a> <a href="/tags/Spring-Data-Elasticsearch/" style="font-size: 10px;">Spring Data Elasticsearch</a> <a href="/tags/Spring-in-action/" style="font-size: 10px;">Spring in action</a> <a href="/tags/Windows/" style="font-size: 10px;">Windows</a> <a href="/tags/ajax/" style="font-size: 10px;">ajax</a> <a href="/tags/appium/" style="font-size: 10px;">appium</a> <a href="/tags/base-command/" style="font-size: 10px;">base command</a> <a href="/tags/bash/" style="font-size: 10px;">bash</a> <a href="/tags/bat/" style="font-size: 10px;">bat</a> <a href="/tags/bat脚本/" style="font-size: 10px;">bat脚本</a> <a href="/tags/beautifulsoup4/" style="font-size: 10px;">beautifulsoup4</a> <a href="/tags/boss/" style="font-size: 10px;">boss</a> <a href="/tags/builder/" style="font-size: 10px;">builder</a> <a href="/tags/chrome/" style="font-size: 10.91px;">chrome</a> <a href="/tags/chrome-plugin/" style="font-size: 10px;">chrome plugin</a> <a href="/tags/coding-net/" style="font-size: 10px;">coding.net</a> <a href="/tags/common-commands/" style="font-size: 10.91px;">common commands</a> <a href="/tags/csdn/" style="font-size: 10px;">csdn</a> <a href="/tags/design-patterns/" style="font-size: 14.55px;">design patterns</a> <a href="/tags/directory-management/" style="font-size: 10px;">directory management</a> <a href="/tags/directory-permission/" style="font-size: 10px;">directory permission</a> <a href="/tags/disk-management/" style="font-size: 10px;">disk management</a> <a href="/tags/docker/" style="font-size: 15.45px;">docker</a> <a href="/tags/docker-compose/" style="font-size: 10px;">docker compose</a> <a href="/tags/druid/" style="font-size: 10px;">druid</a> <a href="/tags/elasticsearch/" style="font-size: 10.91px;">elasticsearch</a> <a href="/tags/elasticsearch-cluster/" style="font-size: 10px;">elasticsearch cluster</a> <a href="/tags/ffmpeg/" style="font-size: 10px;">ffmpeg</a> <a href="/tags/fiddler/" style="font-size: 10px;">fiddler</a> <a href="/tags/fiddler-capture/" style="font-size: 10px;">fiddler capture</a> <a href="/tags/file-management/" style="font-size: 10px;">file management</a> <a href="/tags/file-permission/" style="font-size: 10px;">file permission</a> <a href="/tags/head-plugin/" style="font-size: 10px;">head plugin</a> <a href="/tags/hexo/" style="font-size: 13.64px;">hexo</a> <a href="/tags/hexo-themes/" style="font-size: 10px;">hexo themes</a> <a href="/tags/hexo-translate-title/" style="font-size: 10px;">hexo-translate-title</a> <a href="/tags/hot-key/" style="font-size: 10px;">hot key</a> <a href="/tags/http/" style="font-size: 10px;">http</a> <a href="/tags/http-proxy/" style="font-size: 10px;">http proxy</a> <a href="/tags/http-s/" style="font-size: 10px;">http(s)</a> <a href="/tags/https/" style="font-size: 10px;">https</a> <a href="/tags/https抓包/" style="font-size: 10px;">https抓包</a> <a href="/tags/ik-analysis/" style="font-size: 10px;">ik analysis</a> <a href="/tags/imooc/" style="font-size: 10px;">imooc</a> <a href="/tags/itchat/" style="font-size: 10px;">itchat</a> <a href="/tags/jasypt-spring-boot/" style="font-size: 10px;">jasypt spring boot</a> <a href="/tags/java/" style="font-size: 10.91px;">java</a> <a href="/tags/java-error/" style="font-size: 10px;">java error</a> <a href="/tags/java-exception/" style="font-size: 10px;">java exception</a> <a href="/tags/java8-stream/" style="font-size: 10px;">java8 stream</a> <a href="/tags/javacv/" style="font-size: 10px;">javacv</a> <a href="/tags/jetcache/" style="font-size: 10px;">jetcache</a> <a href="/tags/jsonp/" style="font-size: 10px;">jsonp</a> <a href="/tags/jvm/" style="font-size: 10px;">jvm</a> <a href="/tags/lagou/" style="font-size: 10px;">lagou</a> <a href="/tags/linux-backup/" style="font-size: 10px;">linux backup</a> <a href="/tags/linux-compress/" style="font-size: 10px;">linux compress</a> <a href="/tags/linux-packeage/" style="font-size: 10px;">linux packeage</a> <a href="/tags/linux-thing/" style="font-size: 13.64px;">linux thing</a> <a href="/tags/mind-mapping/" style="font-size: 11.82px;">mind mapping</a> <a href="/tags/mitmproxy/" style="font-size: 10px;">mitmproxy</a> <a href="/tags/mongo-express/" style="font-size: 10px;">mongo-express</a> <a href="/tags/mongodb/" style="font-size: 10px;">mongodb</a> <a href="/tags/multiple-datasource/" style="font-size: 10.91px;">multiple datasource</a> <a href="/tags/mybatis/" style="font-size: 10px;">mybatis</a> <a href="/tags/mysql/" style="font-size: 11.82px;">mysql</a> <a href="/tags/nginx/" style="font-size: 10.91px;">nginx</a> <a href="/tags/oschina/" style="font-size: 10px;">oschina</a> <a href="/tags/postgresql/" style="font-size: 11.82px;">postgresql</a> <a href="/tags/prototype/" style="font-size: 10px;">prototype</a> <a href="/tags/py2exe/" style="font-size: 10px;">py2exe</a> <a href="/tags/pyinstaller/" style="font-size: 10px;">pyinstaller</a> <a href="/tags/python/" style="font-size: 17.27px;">python</a> <a href="/tags/python-core/" style="font-size: 10px;">python core</a> <a href="/tags/python-data-science/" style="font-size: 10px;">python data science</a> <a href="/tags/python-spider/" style="font-size: 10px;">python spider</a> <a href="/tags/python-standard-library/" style="font-size: 10px;">python standard library</a> <a href="/tags/redis/" style="font-size: 12.73px;">redis</a> <a href="/tags/redis-sentinel/" style="font-size: 10px;">redis-sentinel</a> <a href="/tags/requests/" style="font-size: 10px;">requests</a> <a href="/tags/restful-api/" style="font-size: 10px;">restful api</a> <a href="/tags/scrapy/" style="font-size: 12.73px;">scrapy</a> <a href="/tags/selenium/" style="font-size: 13.64px;">selenium</a> <a href="/tags/shortcuts-key/" style="font-size: 10px;">shortcuts key</a> <a href="/tags/simple-factory/" style="font-size: 11.82px;">simple factory</a> <a href="/tags/singleton/" style="font-size: 10px;">singleton</a> <a href="/tags/spider/" style="font-size: 10.91px;">spider</a> <a href="/tags/spider-crawl/" style="font-size: 10px;">spider crawl</a> <a href="/tags/spring-boot/" style="font-size: 17.27px;">spring boot</a> <a href="/tags/spring-boot-actuator/" style="font-size: 10px;">spring boot actuator</a> <a href="/tags/spring-boot-admin/" style="font-size: 10px;">spring boot admin</a> <a href="/tags/spring-boot2/" style="font-size: 10px;">spring boot2</a> <a href="/tags/spring-cache/" style="font-size: 10px;">spring cache</a> <a href="/tags/spring-data-jpa/" style="font-size: 12.73px;">spring data jpa</a> <a href="/tags/spring-data-rest/" style="font-size: 10px;">spring data rest</a> <a href="/tags/swagger/" style="font-size: 10.91px;">swagger</a> <a href="/tags/tomcat/" style="font-size: 10px;">tomcat</a> <a href="/tags/vi/" style="font-size: 10px;">vi</a> <a href="/tags/vim/" style="font-size: 10px;">vim</a> <a href="/tags/vimium/" style="font-size: 10px;">vimium</a> <a href="/tags/websocket/" style="font-size: 10px;">websocket</a> <a href="/tags/weixin/" style="font-size: 10px;">weixin</a> <a href="/tags/ws-s/" style="font-size: 10px;">ws(s)</a> <a href="/tags/wss/" style="font-size: 10px;">wss</a> <a href="/tags/yapi/" style="font-size: 10px;">yapi</a> <a href="/tags/zealer/" style="font-size: 10px;">zealer</a> <a href="/tags/个人博客/" style="font-size: 10.91px;">个人博客</a> <a href="/tags/个人感悟/" style="font-size: 10.91px;">个人感悟</a> <a href="/tags/主从复制/" style="font-size: 10.91px;">主从复制</a> <a href="/tags/主从搭建/" style="font-size: 11.82px;">主从搭建</a> <a href="/tags/乐观锁/" style="font-size: 10px;">乐观锁</a> <a href="/tags/代理调试/" style="font-size: 10px;">代理调试</a> <a href="/tags/入门篇/" style="font-size: 10px;">入门篇</a> <a href="/tags/单例/" style="font-size: 10px;">单例</a> <a href="/tags/博客/" style="font-size: 10px;">博客</a> <a href="/tags/压测工具/" style="font-size: 11.82px;">压测工具</a> <a href="/tags/原型/" style="font-size: 10px;">原型</a> <a href="/tags/字符串机制/" style="font-size: 10px;">字符串机制</a> <a href="/tags/学习资源/" style="font-size: 10.91px;">学习资源</a> <a href="/tags/实践篇/" style="font-size: 10.91px;">实践篇</a> <a href="/tags/工厂方法/" style="font-size: 10px;">工厂方法</a> <a href="/tags/建造者/" style="font-size: 10px;">建造者</a> <a href="/tags/开源/" style="font-size: 10px;">开源</a> <a href="/tags/开源博客/" style="font-size: 10.91px;">开源博客</a> <a href="/tags/开源项目/" style="font-size: 10.91px;">开源项目</a> <a href="/tags/微信授权/" style="font-size: 10px;">微信授权</a> <a href="/tags/悲观锁/" style="font-size: 10px;">悲观锁</a> <a href="/tags/抽象工厂/" style="font-size: 10px;">抽象工厂</a> <a href="/tags/数据库中间件/" style="font-size: 12.73px;">数据库中间件</a> <a href="/tags/热部署/" style="font-size: 10px;">热部署</a> <a href="/tags/简书/" style="font-size: 10px;">简书</a> <a href="/tags/简单工厂/" style="font-size: 10px;">简单工厂</a> <a href="/tags/编辑器/" style="font-size: 19.09px;">编辑器</a> <a href="/tags/读书笔记/" style="font-size: 10px;">读书笔记</a> <a href="/tags/读写分离/" style="font-size: 10.91px;">读写分离</a> <a href="/tags/跑步/" style="font-size: 10px;">跑步</a> <a href="/tags/配置篇/" style="font-size: 10px;">配置篇</a> <a href="/tags/钉钉运动/" style="font-size: 10px;">钉钉运动</a> <a href="/tags/阿里技术/" style="font-size: 10px;">阿里技术</a>
</div>
</div>
<a href="javascript:;" class="sidebar-menu-box-close">×</a>
</div>
<div class="mobile-header-menu-nav" id="mobile-header-menu-nav">
<div class="mobile-header-menu-container">
<span class="title">Menus</span>
<ul class="mobile-header-menu-navbar">
<li>
<a href="/">
<i class="fa fa-h-square"></i><span>主页</span>
</a>
</li>
<li>
<a href="/life">
<i class="fa fa-battery-three-quarters"></i><span>岁月</span>
</a>
</li>
<li>
<a target="_blank" href="https://mp.mariojd.cn">
<i class="fa fa-dot-circle-o"></i><span>洞见</span>
</a>
</li>
<li>
<a href="/archives">
<i class="fa fa-window-restore"></i><span>归档</span>
</a>
</li>
<li>
<a target="_blank" href="http://jianshu.mariojd.cn">
<i class="fa fa-superpowers"></i><span>简统</span>
</a>
</li>
<li>
<a href="/about">
<i class="fa fa-user"></i><span>关于</span>
</a>
</li>
</ul>
</div>
<div class="mobile-header-tag-container">
<span class="title">Tags</span>
<div id="mobile-header-container-tags">
<a href="/tags/AcpectJ/" style="font-size: 10px;">AcpectJ</a> <a href="/tags/Aop/" style="font-size: 10px;">Aop</a> <a href="/tags/Chocolatey/" style="font-size: 10px;">Chocolatey</a> <a href="/tags/Config-Center/" style="font-size: 10.91px;">Config Center</a> <a href="/tags/Ctrip-Apollo/" style="font-size: 10.91px;">Ctrip Apollo</a> <a href="/tags/Eclipse/" style="font-size: 10.91px;">Eclipse</a> <a href="/tags/Faker/" style="font-size: 10.91px;">Faker</a> <a href="/tags/Fiddler/" style="font-size: 10px;">Fiddler</a> <a href="/tags/GitHub-Pages/" style="font-size: 10.91px;">GitHub Pages</a> <a href="/tags/Hexo/" style="font-size: 10px;">Hexo</a> <a href="/tags/IDEA/" style="font-size: 20px;">IDEA</a> <a href="/tags/JSR-303/" style="font-size: 10px;">JSR 303</a> <a href="/tags/Java/" style="font-size: 16.36px;">Java</a> <a href="/tags/Java类库/" style="font-size: 10px;">Java类库</a> <a href="/tags/JetBrains/" style="font-size: 18.18px;">JetBrains</a> <a href="/tags/Jmeter/" style="font-size: 11.82px;">Jmeter</a> <a href="/tags/Lombok/" style="font-size: 10px;">Lombok</a> <a href="/tags/Micro-Service/" style="font-size: 10.91px;">Micro Service</a> <a href="/tags/Mycat/" style="font-size: 12.73px;">Mycat</a> <a href="/tags/ORM/" style="font-size: 10.91px;">ORM</a> <a href="/tags/Peewee/" style="font-size: 10px;">Peewee</a> <a href="/tags/Python/" style="font-size: 11.82px;">Python</a> <a href="/tags/RestTemplate/" style="font-size: 10px;">RestTemplate</a> <a href="/tags/SQLAlchemy/" style="font-size: 10px;">SQLAlchemy</a> <a href="/tags/Spring/" style="font-size: 10.91px;">Spring</a> <a href="/tags/Spring-Boot/" style="font-size: 13.64px;">Spring Boot</a> <a href="/tags/Spring-Data-Elasticsearch/" style="font-size: 10px;">Spring Data Elasticsearch</a> <a href="/tags/Spring-in-action/" style="font-size: 10px;">Spring in action</a> <a href="/tags/Windows/" style="font-size: 10px;">Windows</a> <a href="/tags/ajax/" style="font-size: 10px;">ajax</a> <a href="/tags/appium/" style="font-size: 10px;">appium</a> <a href="/tags/base-command/" style="font-size: 10px;">base command</a> <a href="/tags/bash/" style="font-size: 10px;">bash</a> <a href="/tags/bat/" style="font-size: 10px;">bat</a> <a href="/tags/bat脚本/" style="font-size: 10px;">bat脚本</a> <a href="/tags/beautifulsoup4/" style="font-size: 10px;">beautifulsoup4</a> <a href="/tags/boss/" style="font-size: 10px;">boss</a> <a href="/tags/builder/" style="font-size: 10px;">builder</a> <a href="/tags/chrome/" style="font-size: 10.91px;">chrome</a> <a href="/tags/chrome-plugin/" style="font-size: 10px;">chrome plugin</a> <a href="/tags/coding-net/" style="font-size: 10px;">coding.net</a> <a href="/tags/common-commands/" style="font-size: 10.91px;">common commands</a> <a href="/tags/csdn/" style="font-size: 10px;">csdn</a> <a href="/tags/design-patterns/" style="font-size: 14.55px;">design patterns</a> <a href="/tags/directory-management/" style="font-size: 10px;">directory management</a> <a href="/tags/directory-permission/" style="font-size: 10px;">directory permission</a> <a href="/tags/disk-management/" style="font-size: 10px;">disk management</a> <a href="/tags/docker/" style="font-size: 15.45px;">docker</a> <a href="/tags/docker-compose/" style="font-size: 10px;">docker compose</a> <a href="/tags/druid/" style="font-size: 10px;">druid</a> <a href="/tags/elasticsearch/" style="font-size: 10.91px;">elasticsearch</a> <a href="/tags/elasticsearch-cluster/" style="font-size: 10px;">elasticsearch cluster</a> <a href="/tags/ffmpeg/" style="font-size: 10px;">ffmpeg</a> <a href="/tags/fiddler/" style="font-size: 10px;">fiddler</a> <a href="/tags/fiddler-capture/" style="font-size: 10px;">fiddler capture</a> <a href="/tags/file-management/" style="font-size: 10px;">file management</a> <a href="/tags/file-permission/" style="font-size: 10px;">file permission</a> <a href="/tags/head-plugin/" style="font-size: 10px;">head plugin</a> <a href="/tags/hexo/" style="font-size: 13.64px;">hexo</a> <a href="/tags/hexo-themes/" style="font-size: 10px;">hexo themes</a> <a href="/tags/hexo-translate-title/" style="font-size: 10px;">hexo-translate-title</a> <a href="/tags/hot-key/" style="font-size: 10px;">hot key</a> <a href="/tags/http/" style="font-size: 10px;">http</a> <a href="/tags/http-proxy/" style="font-size: 10px;">http proxy</a> <a href="/tags/http-s/" style="font-size: 10px;">http(s)</a> <a href="/tags/https/" style="font-size: 10px;">https</a> <a href="/tags/https抓包/" style="font-size: 10px;">https抓包</a> <a href="/tags/ik-analysis/" style="font-size: 10px;">ik analysis</a> <a href="/tags/imooc/" style="font-size: 10px;">imooc</a> <a href="/tags/itchat/" style="font-size: 10px;">itchat</a> <a href="/tags/jasypt-spring-boot/" style="font-size: 10px;">jasypt spring boot</a> <a href="/tags/java/" style="font-size: 10.91px;">java</a> <a href="/tags/java-error/" style="font-size: 10px;">java error</a> <a href="/tags/java-exception/" style="font-size: 10px;">java exception</a> <a href="/tags/java8-stream/" style="font-size: 10px;">java8 stream</a> <a href="/tags/javacv/" style="font-size: 10px;">javacv</a> <a href="/tags/jetcache/" style="font-size: 10px;">jetcache</a> <a href="/tags/jsonp/" style="font-size: 10px;">jsonp</a> <a href="/tags/jvm/" style="font-size: 10px;">jvm</a> <a href="/tags/lagou/" style="font-size: 10px;">lagou</a> <a href="/tags/linux-backup/" style="font-size: 10px;">linux backup</a> <a href="/tags/linux-compress/" style="font-size: 10px;">linux compress</a> <a href="/tags/linux-packeage/" style="font-size: 10px;">linux packeage</a> <a href="/tags/linux-thing/" style="font-size: 13.64px;">linux thing</a> <a href="/tags/mind-mapping/" style="font-size: 11.82px;">mind mapping</a> <a href="/tags/mitmproxy/" style="font-size: 10px;">mitmproxy</a> <a href="/tags/mongo-express/" style="font-size: 10px;">mongo-express</a> <a href="/tags/mongodb/" style="font-size: 10px;">mongodb</a> <a href="/tags/multiple-datasource/" style="font-size: 10.91px;">multiple datasource</a> <a href="/tags/mybatis/" style="font-size: 10px;">mybatis</a> <a href="/tags/mysql/" style="font-size: 11.82px;">mysql</a> <a href="/tags/nginx/" style="font-size: 10.91px;">nginx</a> <a href="/tags/oschina/" style="font-size: 10px;">oschina</a> <a href="/tags/postgresql/" style="font-size: 11.82px;">postgresql</a> <a href="/tags/prototype/" style="font-size: 10px;">prototype</a> <a href="/tags/py2exe/" style="font-size: 10px;">py2exe</a> <a href="/tags/pyinstaller/" style="font-size: 10px;">pyinstaller</a> <a href="/tags/python/" style="font-size: 17.27px;">python</a> <a href="/tags/python-core/" style="font-size: 10px;">python core</a> <a href="/tags/python-data-science/" style="font-size: 10px;">python data science</a> <a href="/tags/python-spider/" style="font-size: 10px;">python spider</a> <a href="/tags/python-standard-library/" style="font-size: 10px;">python standard library</a> <a href="/tags/redis/" style="font-size: 12.73px;">redis</a> <a href="/tags/redis-sentinel/" style="font-size: 10px;">redis-sentinel</a> <a href="/tags/requests/" style="font-size: 10px;">requests</a> <a href="/tags/restful-api/" style="font-size: 10px;">restful api</a> <a href="/tags/scrapy/" style="font-size: 12.73px;">scrapy</a> <a href="/tags/selenium/" style="font-size: 13.64px;">selenium</a> <a href="/tags/shortcuts-key/" style="font-size: 10px;">shortcuts key</a> <a href="/tags/simple-factory/" style="font-size: 11.82px;">simple factory</a> <a href="/tags/singleton/" style="font-size: 10px;">singleton</a> <a href="/tags/spider/" style="font-size: 10.91px;">spider</a> <a href="/tags/spider-crawl/" style="font-size: 10px;">spider crawl</a> <a href="/tags/spring-boot/" style="font-size: 17.27px;">spring boot</a> <a href="/tags/spring-boot-actuator/" style="font-size: 10px;">spring boot actuator</a> <a href="/tags/spring-boot-admin/" style="font-size: 10px;">spring boot admin</a> <a href="/tags/spring-boot2/" style="font-size: 10px;">spring boot2</a> <a href="/tags/spring-cache/" style="font-size: 10px;">spring cache</a> <a href="/tags/spring-data-jpa/" style="font-size: 12.73px;">spring data jpa</a> <a href="/tags/spring-data-rest/" style="font-size: 10px;">spring data rest</a> <a href="/tags/swagger/" style="font-size: 10.91px;">swagger</a> <a href="/tags/tomcat/" style="font-size: 10px;">tomcat</a> <a href="/tags/vi/" style="font-size: 10px;">vi</a> <a href="/tags/vim/" style="font-size: 10px;">vim</a> <a href="/tags/vimium/" style="font-size: 10px;">vimium</a> <a href="/tags/websocket/" style="font-size: 10px;">websocket</a> <a href="/tags/weixin/" style="font-size: 10px;">weixin</a> <a href="/tags/ws-s/" style="font-size: 10px;">ws(s)</a> <a href="/tags/wss/" style="font-size: 10px;">wss</a> <a href="/tags/yapi/" style="font-size: 10px;">yapi</a> <a href="/tags/zealer/" style="font-size: 10px;">zealer</a> <a href="/tags/个人博客/" style="font-size: 10.91px;">个人博客</a> <a href="/tags/个人感悟/" style="font-size: 10.91px;">个人感悟</a> <a href="/tags/主从复制/" style="font-size: 10.91px;">主从复制</a> <a href="/tags/主从搭建/" style="font-size: 11.82px;">主从搭建</a> <a href="/tags/乐观锁/" style="font-size: 10px;">乐观锁</a> <a href="/tags/代理调试/" style="font-size: 10px;">代理调试</a> <a href="/tags/入门篇/" style="font-size: 10px;">入门篇</a> <a href="/tags/单例/" style="font-size: 10px;">单例</a> <a href="/tags/博客/" style="font-size: 10px;">博客</a> <a href="/tags/压测工具/" style="font-size: 11.82px;">压测工具</a> <a href="/tags/原型/" style="font-size: 10px;">原型</a> <a href="/tags/字符串机制/" style="font-size: 10px;">字符串机制</a> <a href="/tags/学习资源/" style="font-size: 10.91px;">学习资源</a> <a href="/tags/实践篇/" style="font-size: 10.91px;">实践篇</a> <a href="/tags/工厂方法/" style="font-size: 10px;">工厂方法</a> <a href="/tags/建造者/" style="font-size: 10px;">建造者</a> <a href="/tags/开源/" style="font-size: 10px;">开源</a> <a href="/tags/开源博客/" style="font-size: 10.91px;">开源博客</a> <a href="/tags/开源项目/" style="font-size: 10.91px;">开源项目</a> <a href="/tags/微信授权/" style="font-size: 10px;">微信授权</a> <a href="/tags/悲观锁/" style="font-size: 10px;">悲观锁</a> <a href="/tags/抽象工厂/" style="font-size: 10px;">抽象工厂</a> <a href="/tags/数据库中间件/" style="font-size: 12.73px;">数据库中间件</a> <a href="/tags/热部署/" style="font-size: 10px;">热部署</a> <a href="/tags/简书/" style="font-size: 10px;">简书</a> <a href="/tags/简单工厂/" style="font-size: 10px;">简单工厂</a> <a href="/tags/编辑器/" style="font-size: 19.09px;">编辑器</a> <a href="/tags/读书笔记/" style="font-size: 10px;">读书笔记</a> <a href="/tags/读写分离/" style="font-size: 10.91px;">读写分离</a> <a href="/tags/跑步/" style="font-size: 10px;">跑步</a> <a href="/tags/配置篇/" style="font-size: 10px;">配置篇</a> <a href="/tags/钉钉运动/" style="font-size: 10px;">钉钉运动</a> <a href="/tags/阿里技术/" style="font-size: 10px;">阿里技术</a>
</div>
</div>
</div>
<div class="search-wrap">
<span class="search-close">×</span>
<a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
<i class="icon icon-lg icon-chevron-left"></i>
</a>
<input class="search-field" placeholder="Search..." id="keywords">
<a id="search-submit" href="javascript:;">
<i class="fa fa-search"></i>
</a>
<div class="search-container" id="search-container">
<ul class="search-result" id="search-result">
</ul>
</div>
</div>
<div id="search-tpl">
<li class="search-result-item">
<a href="{url}" class="search-item-li">
<span class="search-item-li-title" title="{title}">{title}</span>
</a>
</li>
</div>
<script src="/js/search.js"></script>
<script src="/js/main.js"></script>
<script src="//cdn.bootcss.com/particles.js/2.0.0/particles.min.js"></script>
<div id="particles"></div>
<script src="/js/particles.js"></script>
<script src="/js/pop-img.js"></script>
<script>
$(".article-entry p img").popImg();
</script>
</div>
</body>
</html>