-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1312 lines (1100 loc) · 48.8 KB
/
index.html
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta name="keywords" content="Gridea静态个人博客">
<meta name="description" content="wilde的博客">
<meta name="theme-color" content="#000">
<title>wilde的博客</title>
<link rel="shortcut icon" href="/favicon.ico?v=1617541603285">
<link rel="stylesheet" href="/media/css/gemini.css">
<link rel="stylesheet" href="/media/fonts/font-awesome.css">
<link
href="//fonts.googleapis.com/css?family=Monda:300,300italic,400,400italic,700,700italic|Roboto Slab:300,300italic,400,400italic,700,700italic|Rosario:300,300italic,400,400italic,700,700italic|PT Mono:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext"
rel="stylesheet" type="text/css">
<link href="/media/hljs/styles/atom-one-dark-reasonable.css"
rel="stylesheet">
<link rel="stylesheet" href="/styles/main.css">
<script src="/media/hljs/highlight.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/velocity.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/velocity.ui.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css"
integrity="sha384-zB1R0rpPzHqg7Kpt0Aljp8JPLqbXI3bhnPWROx27a9N0Ll6ZP/+DiW/UqRcLbRjq" crossorigin="anonymous">
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js"
integrity="sha384-y23I5Q6l+B6vatafAwxRu/0oK/79VlbSz7Q9aiSZUvyWYIYsd+qj+o24G5ZU2zJz" crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js"
integrity="sha384-kWPLUVMOks5AQFrykwIup5lo0m3iMkkHrD0uJ4H5cjeGihAutqP0yW0J6dpFiVkI" crossorigin="anonymous"
onload="renderMathInElement(document.body);"></script>
</head>
<body>
<div class="head-top-line"></div>
<div class="header-box">
<div class="gemini">
<header class="header ">
<div class="blog-header box-shadow-wrapper bg-color " id="header">
<div class="nav-toggle" id="nav_toggle">
<div class="toggle-box">
<div class="line line-top"></div>
<div class="line line-center"></div>
<div class="line line-bottom"></div>
</div>
</div>
<div class="site-meta">
<div class="site-title">
<a href="/" class="brand">
<span>wilde的博客</span>
</a>
</div>
<p class="subtitle">仰望星空,脚踏实地</p>
</div>
<nav class="site-nav" id="site_nav">
<ul id="nav_ul">
<li class="nav-item nav-item-active">
<a href="/" target="_self">
<i class="fa fa-home"></i> 首页
</a>
</li>
<li class="nav-item ">
<a href="/archives/" target="_self">
<i class="fa fa-archive"></i> 归档
</a>
</li>
<li class="nav-item ">
<a href="/tags/" target="_self">
<i class="fa fa-tags"></i> 标签
</a>
</li>
<li class="nav-item ">
<a href="/post/about/" target="_self">
<i class="fa fa-user"></i> 关于
</a>
</li>
<li class="nav-item ">
<a href="/friends/" target="_self">
<i class="fa fa-address-book"></i> 友情链接
</a>
</li>
<li id="fa_search" class="nav-item">
<a href="javascript:void(0);">
<i class="fa fa-search"></i> <span class="language" data-lan="search">搜索</span>
</a>
</li>
</ul>
</nav>
</div>
</header>
</div>
<script type="text/javascript">
let showNav = true;
let navToggle = document.querySelector('#nav_toggle'),
siteNav = document.querySelector('#site_nav');
function navClick() {
let sideBar = document.querySelector('.sidebar');
let navUl = document.querySelector('#nav_ul');
navToggle.classList.toggle('nav-toggle-active');
siteNav.classList.toggle('nav-menu-active');
if (siteNav.classList.contains('nav-menu-active')) {
siteNav.style = "height: " + (navUl.children.length * 42) +"px !important";
} else {
siteNav.style = "";
}
}
navToggle.addEventListener('click',navClick);
</script>
</div>
<div class="main-continer">
<div
class="section-layout gemini ">
<div class="section-layout-wrapper">
<div id="sidebarMeta" class="sidebar">
<div class="sidebar-wrapper box-shadow-wrapper bg-color">
<div class="sidebar-item">
<img class="site-author-image right-motion" src="/images/avatar.png"/>
<p class="site-author-name">wilde</p>
<div class="site-description right-motion">
<p>一个热爱技术的student</p>
</div>
</div>
<div class="sidebar-item side-item-stat right-motion">
<div class="sidebar-item-box">
<a href="/archives/">
<span class="site-item-stat-count">1</span>
<span class="site-item-stat-name language" data-lan="article">文章</span>
</a>
</div>
<div class="sidebar-item-box">
<a href="">
<span class="site-item-stat-count">1</span>
<span class="site-item-stat-name language" data-lan="category">分类</span>
</a>
</div>
<div class="sidebar-item-box">
<a href="/tags/">
<span class="site-item-stat-count">1</span>
<span class="site-item-stat-name language" data-lan="tag">标签</span>
</a>
</div>
</div>
<div class="sidebar-item sidebar-item-social">
<div class="social-item">
<a href="https://github.com/wilde3">
<i class="fa fa-github-alt" title="github"></i>
</a>
</div>
</div>
<div style="width: 100%; position: relative;">
<canvas id="canvasDiyBlock" style="width:100%;">当前浏览器不支持canvas,请更换浏览器后再试</canvas>
<script src="/media/js/magic/clock.js"></script>
</div>
</div>
</div>
<script>
let sidebarMeta = document.querySelector('#sidebarMeta');
let scheme = 'gemini';
let sidebarWrapper = document.querySelector('.sidebar-wrapper');
if (sidebarMeta && (scheme === 'pisces' || scheme === 'gemini')) {
document.addEventListener('scroll', function(e) {
if (document.scrollingElement.scrollTop > parseInt(sidebarMeta.style.marginTop) + 10) {
sidebarWrapper.classList.add('home-sidebar-fixed')
} else {
sidebarWrapper.classList.remove('home-sidebar-fixed')
}
});
}
</script>
<div class="section-box gemini">
<section class="section posts-expand slide-down-in">
<article class="post-list-box post box-shadow-wrapper">
<div class="article-wrapper bg-color">
<section class="post-header">
<h1 class="post-title">
<a class="post-title-link" href="https://wilde3.github.io/post/Spring/">
Spring01基础知识
</a>
</h1>
<div class="post-meta">
<span class="meta-item pc-show">
<i class="fa fa-calendar-o"></i>
<span class="language" data-lan="publish">发布于</span>
<span class="publish-time" data-t="2021-04-04 12:37:48">2021-04-04 12:37:48</span>
<span class="post-meta-divider pc-show">|</span>
</span>
<span class="meta-item">
<i class="fa fa-folder-o"></i>
<span class="pc-show language" data-lan="category-in">分类于</span>
<a href="https://wilde3.github.io/tag/JisRHjH2F/">
<span>Spring</span>
</a>
</span>
<span class="post-meta-divider">|</span>
<span class="meta-item">
<i class="fa fa-clock-o"></i>
<span>15<span class="language" data-lan="minute">分钟</span></span>
</span>
<span class="meta-item">
<span class="post-meta-divider">|</span>
<i class="fa fa-file-word-o"></i>
<span>3184<span class="pc-show language" data-lan="words">字数</span></span>
</span>
</div>
</section>
<div class="post-body">
<!-- 没有手动摘要切开启了自动摘要,则根据配置筛除摘要内容 -->
<p><h2 id="1-spring框架的引言">1. Spring框架的引言</h2>
<p><code>spring</code>(春天),生于在2002年,由<code>Rod Johnson</code>创作。Spring框架是一个集众多设计模式于一身的<code>开源的</code>、<code>轻量级</code>的<code>项目管理</code>框架。致力于JAVAEE轻量级解决方案。相对于原来学过的框架而言,spring框架和之前学习的struts2 、 mybatis 框架有了本质的区别,不是替换原来的某个框架,而是对其进行<code>整合管理</code>。</p>
<p><code>轻量级解决方案</code>:提供一个以简单的、统一的、高效的方式构造整个应用,并且可以将单层框架以最佳的组合揉和在一起建立一个连贯的体系。</p>
<!-- 自动摘要出来的内容有<code则补上结尾,防止格式错误 -->
</code></pre>
<p>
<div class="post-button text-center">
<a class="btn language" data-lan="read-more" href="https://wilde3.github.io/post/Spring/" rel="contents">
阅读全文 »
</a>
</div>
</div>
</div>
</article>
<div class="page bg-color">
<ul class="pagination-ul">
<li class="pagination-li pagination-active">
<a href="/page/../">
1
</a>
</li>
</ul>
</div>
</section>
</div>
</div>
</div>
<div class="footer-box">
<footer class="footer">
<div class="copyright">
Powered by <a href="https://github.com/getgridea/gridea" target="_blank">Gridea</a> | © 2019-2020 Theme By <a
href="https://github.com/hsxyhao/gridea-theme-next" target="_blank">HsxyHao</a>
</div>
<div class="poweredby">
</div>
</footer>
<div class="gemini back-to-top" id="back_to_top">
<i class="fa fa-arrow-up"></i>
<span class="scrollpercent">
<span id="back_to_top_text">0</span>%
</span>
</div>
<div class="bg-img">
<img src="/media/images/1.jpg" />
</div>
</div>
<script>
let sideBarOpen = 'sidebar-open';
let body = document.body;
let back2Top = document.querySelector('#back_to_top'),
back2TopText = document.querySelector('#back_to_top_text'),
drawerBox = document.querySelector('#drawer_box'),
rightSideBar = document.querySelector('.sidebar'),
viewport = document.querySelector('body');
function scrollAnimation(currentY, targetY) {
let needScrollTop = targetY - currentY
let _currentY = currentY
setTimeout(() => {
const dist = Math.ceil(needScrollTop / 10)
_currentY += dist
window.scrollTo(_currentY, currentY)
if (needScrollTop > 10 || needScrollTop < -10) {
scrollAnimation(_currentY, targetY)
} else {
window.scrollTo(_currentY, targetY)
}
}, 1)
}
back2Top.addEventListener("click", function (e) {
scrollAnimation(document.scrollingElement.scrollTop, 0);
e.stopPropagation();
return false;
});
window.addEventListener('scroll', function (e) {
let percent = document.scrollingElement.scrollTop / (document.scrollingElement.scrollHeight - document.scrollingElement.clientHeight) * 100;
if (percent > 1 && !back2Top.classList.contains('back-top-active')) {
back2Top.classList.add('back-top-active');
}
if (percent == 0) {
back2Top.classList.remove('back-top-active');
}
if (back2TopText) {
back2TopText.textContent = Math.floor(percent);
}
});
let hasCacu = false;
window.onresize = function () {
calcuHeight();
}
function calcuHeight() {
// 动态调整站点概览高度
if (!hasCacu && back2Top.classList.contains('pisces') || back2Top.classList.contains('gemini')) {
let sideBar = document.querySelector('.sidebar');
let navUl = document.querySelector('#site_nav');
sideBar.style = 'margin-top:' + (navUl.offsetHeight + navUl.offsetTop + 15) + 'px;';
hasCacu = true;
}
}
calcuHeight();
let open = false, MOTION_TIME = 300, RIGHT_MOVE_DIS = '320px';
if (drawerBox) {
let rightMotions = document.querySelectorAll('.right-motion');
let right = drawerBox.classList.contains('right');
let transitionDir = right ? "transition.slideRightIn" : "transition.slideLeftIn";
let openProp, closeProp;
if (right) {
openProp = {
paddingRight: RIGHT_MOVE_DIS
};
closeProp = {
paddingRight: '0px'
};
} else {
openProp = {
paddingLeft: RIGHT_MOVE_DIS
};
closeProp = {
paddingLeft: '0px'
};
}
drawerBox.onclick = function () {
open = !open;
window.Velocity(rightSideBar, 'stop');
window.Velocity(viewport, 'stop');
window.Velocity(rightMotions, 'stop');
if (open) {
window.Velocity(rightSideBar, {
width: RIGHT_MOVE_DIS
}, {
duration: MOTION_TIME,
begin: function () {
window.Velocity(rightMotions, transitionDir, {});
}
})
window.Velocity(viewport, openProp, {
duration: MOTION_TIME
});
} else {
window.Velocity(rightSideBar, {
width: '0px'
}, {
duration: MOTION_TIME,
begin: function () {
window.Velocity(rightMotions, {
opacity: 0
});
}
})
window.Velocity(viewport, closeProp, {
duration: MOTION_TIME
});
}
for (let i = 0; i < drawerBox.children.length; i++) {
drawerBox.children[i].classList.toggle('muse-line');
}
drawerBox.classList.toggle(sideBarOpen);
}
}
// 链接跳转
let newWindow = 'true'
if (newWindow === 'true') {
let links = document.querySelectorAll('.post-body a')
links.forEach(item => {
if (!item.classList.contains('btn')) {
item.setAttribute("target", "_blank");
}
})
}
let faSearch = document.querySelector('#fa_search');
faSearch.addEventListener('click', function () {
document.querySelector('#search_mask').style = ''
})
// 代码高亮
hljs.initHighlightingOnLoad();
// 离开当前页title变化
var leaveTitle = "";
if (leaveTitle) {
document.addEventListener('visibilitychange', function () {
if (document.visibilityState == 'hidden') {
normal_title = document.title;
document.title = leaveTitle;
} else {
document.title = normal_title;
}
});
}
</script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fancyapps/[email protected]/dist/jquery.fancybox.min.css" />
<script src="https://cdn.jsdelivr.net/gh/fancyapps/[email protected]/dist/jquery.fancybox.min.js"></script>
<script>
let images = document.querySelectorAll('.section img');
console.log(images);
images.forEach(image => {
var parent = image.parentElement;
var next = image.nextElementSibling;
parent.removeChild(image);
var aelem = document.createElement('a');
aelem.href = image.src;
aelem.dataset['fancybox'] = 'images';
aelem.dataset['rel'] = 'fancybox-button';
aelem.classList.add('fancybox');
aelem.appendChild(image);
parent.insertBefore(aelem, next);
})
</script>
</div>
</body>
<div class="search-mask" id="search_mask" style="display: none;">
<div class="search-box">
<div class="search-title">
<i class="fa fa-search"></i>
<div class="input-box">
<input id="search" type="text" class="language" data-lan="search" placeholder="搜索">
</div>
<i id="close" class="fa fa-times-circle"></i>
</div>
<div class="stat-box">
<span id="stat_count">0</span><span class="language" data-lan="stat">条相关条目,使用了</span><span id="stat_times">0</span><span class="language" data-lan="stat-time">毫秒</span>
<hr>
</div>
<div class="result" id="result">
<div class="item">
<a class="result-title" style="opacity: 0;" href="https://wilde3.github.io/post/Spring/"" data-c="
<h2 id="1-spring框架的引言">1. Spring框架的引言</h2>
<p><code>spring</code>(春天),生于在2002年,由<code>Rod Johnson</code>创作。Spring框架是一个集众多设计模式于一身的<code>开源的</code>、<code>轻量级</code>的<code>项目管理</code>框架。致力于JAVAEE轻量级解决方案。相对于原来学过的框架而言,spring框架和之前学习的struts2 、 mybatis 框架有了本质的区别,不是替换原来的某个框架,而是对其进行<code>整合管理</code>。</p>
<p><code>轻量级解决方案</code>:提供一个以简单的、统一的、高效的方式构造整个应用,并且可以将单层框架以最佳的组合揉和在一起建立一个连贯的体系。</p>
<hr>
<h2 id="2spring框架的核心作用">2.Spring框架的核心作用</h2>
<p>Spring 框架用来<code>管理</code><strong>[创建|使用|销毁]<strong>项目中的组件,由于spring 框架可以帮我们生产项目中组件对象,因此也习惯称spring是一个</strong>工厂|容器</strong>。</p>
<blockquote>
<p><strong><code>组件</code>:</strong> 项目中的service,dao,action,都是项目中的组件</p>
<p><strong><code>注意</code>:</strong> spring框架通常<strong>不管理对实体类对象创建</strong></p>
</blockquote>
<hr>
<h2 id="3第一个环境搭建">3.第一个环境搭建</h2>
<figure data-type="image" tabindex="1"><img src="https://cdn.jsdelivr.net/gh/wilde3/Image-Hosting@master/blog-gridea/Spring/1%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84.png" alt="" loading="lazy"></figure>
<p>步骤:<br>
新建一个模块,maven,选择webapp,取名为spring——day1。</p>
<blockquote>
<p><code>a. 引入依赖</code></p>
</blockquote>
<p>在xml文件中引入依赖</p>
<pre><code class="language-xml"> &lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-core&lt;/artifactId&gt;
&lt;version&gt;4.3.2.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-beans&lt;/artifactId&gt;
&lt;version&gt;4.3.2.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-web&lt;/artifactId&gt;
&lt;version&gt;4.3.2.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-expression&lt;/artifactId&gt;
&lt;version&gt;4.3.2.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-aop&lt;/artifactId&gt;
&lt;version&gt;4.3.2.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-context&lt;/artifactId&gt;
&lt;version&gt;4.3.2.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-context-support&lt;/artifactId&gt;
&lt;version&gt;4.3.2.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-aspects&lt;/artifactId&gt;
&lt;version&gt;4.3.2.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
&lt;version&gt;4.3.2.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
</code></pre>
<blockquote>
<p><code>b. 引入配置文件</code></p>
</blockquote>
<pre><code class="language-markdown"># 配置文件名称: 任意名称
# 配置文件位置: 项目中根下任意位置
# 配置文件的内容:
```java
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd &quot;&gt;
&lt;/beans&gt;
</code></pre>
<blockquote>
<p><code>c.创建组件</code></p>
</blockquote>
<p>新建directory——java,java目录下新建接口UserDAO.</p>
<pre><code class="language-java">public interface UserDAO {
void save(String name);
}
</code></pre>
<p>新建一个实现类UserDAOImpl实现接口</p>
<pre><code class="language-java">public class UserDAOImpl implements UserDAO {
public void save(String name) {
System.out.println(&quot;name = &quot; + name);
}
}
</code></pre>
<blockquote>
<p><code>d.工厂管理</code><br>
在resources目录下也建立一个directory——init,在init目录下新建一个spring配置文件,名为spring.xml</p>
</blockquote>
<pre><code class="language-xml">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd&quot;&gt;
&lt;!--通过spring管理组件
bean:用来管理组件对象的创建
class:用来指定管理组件对象的全限定名 包.类
id:用来指定spring框架创建的当前组件对象在spring中唯一标识
推荐id写成接口的首字母小写 如:userDAO
--&gt;
&lt;!--通过bean标签管理组件对象--&gt;
&lt;bean class=&quot;init.UserDAOImpl&quot; id=&quot;aa&quot;&gt;&lt;/bean&gt;
&lt;/beans&gt;
</code></pre>
<blockquote>
<p><code>e.启动工厂</code>测试<br>
在main-java-init目录下建立TestSpring的java文件用于测试。</p>
</blockquote>
<pre><code class="language-java"> public class TestSpring {
public static void main(String[] args) {
//启动工厂
ApplicationContext context = new ClassPathXmlApplicationContext(&quot;init/spring.xml&quot;);
//获取工程中创建好的对象 参数:获取工厂中指定对应的唯一标识
UserDAO userDAO = (UserDAO) context.getBean(&quot;aa&quot;);
userDAO.save(&quot;xiaochen&quot;);
}
}
</code></pre>
<p>若不采用spring,而采用传统方式new对象,则建立的测试为TestUserDAO:</p>
<pre><code class="language-java">public class TestUserDAO{
public static void main(String[] args){
//创建组件对象
UserDAOImpl userDAO = new UserDAOImpl();
//使用组件对象
userDAO.save(&quot;小陈&quot;);
//销毁jvm
}
}
</code></pre>
<hr>
<h2 id="4spring框架中的核心思想">4.Spring框架中的核心思想</h2>
<h4 id="41-ioc控制反转">4.1 IOC[控制反转]</h4>
<ul>
<li>
<p><code>IOC**(inversion of controll)控制反转</code></p>
<pre><code class="language-markdown"># 1.定义
将对象的创建由原来(new)的方式转移到配置文件中,交给spring工厂来创建对象
将原来手动通过new关键字创建对象的权利交给spring,由spring工厂创建对象过程,当然spring不仅要创建对象还要在创建对象的同时通过DI的方式为组件与组件调用关系
</code></pre>
</li>
<li>
<p><code>DI(dependcy Injection)依赖注入</code></p>
<pre><code class="language-markdown"># 1.定义
Spring不仅要创建对象,还要建立类与类之间的关系,因此在控制反转的基础上又提出了依赖注入的概念。为组件中成员变量完成赋值过程,这个过程就叫做依赖注入。
# 2.语法
两个步骤:
1.组件对象中需要哪个组件|谁 将谁声明为成员变量并提供公开的SET方法
2.在spring的配置文件中对应的组件标签内使用property标签去完成属性的赋值操作
</code></pre>
</li>
</ul>
<p>DI基本语法:</p>
<p>DAO组件:</p>
<pre><code class="language-java"> public class DeptDAOImpl implements DeptDAO{
@Override
public void save(String name){
System.out.println(&quot;DAO name:&quot; + name);
}
}
</code></pre>
<p>Service组件:</p>
<pre><code class="language-java">public class DeptServiceImpl implements DeptService{
//需要DAO组件对象 依赖DAO组件
private DeptDAO deptDAO;
//公开的SET方法
public void setDeptDAO(DeptDAO deptDAO){
this.deptDAO = deptDAO;
}@Override
public void save(String name){
System.out.println(&quot;deptService name:&quot; + name);
deptDAO.save(name);
}
}
</code></pre>
<p>xml配置文件赋值:</p>
<!--管理DAO组件-->
<p><bean class="di.DeptDAOImpl" id="aa"></bean></p>
<!--管理service组件-->
<p><bean class="di.DeptServiceImpl" id="deptService"></bean></p>
<pre><code>&lt;!--依赖的注入
property:用来给组件中的属性进行赋值操作
name:用来指定给组件中那个属性名进行赋值
ref:用来指定赋值对象在工厂中唯一标识 bean的id
--&gt;
&lt;property name=&quot;deptDAO&quot; ref=&quot;aa&quot;/&gt;
</code></pre>
</bean>
<p>启动工程测试:<br>
ApplicationContext context = new ClassPathXmlApplicationContext(&quot;di/spring.xml&quot;);<br>
//获取service组件<br>
DeptService deptService = (DeptService) context.getBean(&quot;deptService&quot;);<br>
deptService.save(&quot;百知教育&quot;)</p>
<h4 id="42-aop面向切面编程">4.2 AOP[面向切面编程]</h4>
<p><strong>AOP</strong>( Aspect Oriental Programing ) <strong>面向切面的编程</strong></p>
<hr>
<h2 id="5-set方式注入">5. SET方式注入</h2>
<p>spring中注入方式</p>
<p>a.SET注入 使用成员变量SET方式为属性完成赋值的过程 重点</p>
<p>b.构造注入 使用构造方法形式进行属性的赋值 了解</p>
<p>c.自动注入 就是通过在配置文件中完成类中属性自动赋值 了解</p>
<h3 id="51-八种基本类型string类型-日期类型的注入">5.1 八种基本类型+String类型 +日期类型的注入</h3>
<blockquote>
<p><code>基本类型注入</code></p>
</blockquote>
<pre><code class="language-xml">&lt;property name=&quot;name&quot; value=&quot;zhagnsan&quot;/&gt;
&lt;property name=&quot;age&quot; value=&quot;21&quot;/&gt;
&lt;property name=&quot;id&quot; value=&quot;100063&quot;/&gt;
&lt;property name=&quot;bir&quot; value=&quot;2012/12/12&quot;/&gt;
&lt;!--在spring技术栈中日期格式默认yyyy/MM/dd HH:mm:ss--&gt;
&lt;property name=&quot;price&quot; value=&quot;23.23&quot;/&gt;
</code></pre>
<blockquote>
<p><code>数组类型注入</code></p>
</blockquote>
<pre><code class="language-xml">&lt;!--注入数组类型数据--&gt;
&lt;property name=&quot;qqs&quot;&gt;
&lt;array&gt;
&lt;value&gt;xxx&lt;/value&gt;
&lt;value&gt;qqq&lt;/value&gt;
&lt;value&gt;vvvv&lt;/value&gt;
&lt;/array&gt;
&lt;/property&gt;
</code></pre>
<blockquote>
<p><code>注入引用类型和集合类型</code></p>
</blockquote>
<pre><code class="language-xml">&lt;!--注入引用类型和对象--&gt;
&lt;property name=&quot;userDAO&quot; ref=&quot;userDAO&quot;/&gt;
&lt;!--注入list集合 list--&gt;
&lt;property name=&quot;lists&quot;&gt;
&lt;list&gt;
&lt;value&gt;aaa&lt;/value&gt;
&lt;value&gt;bbb&lt;/value&gt;
&lt;value&gt;ccc&lt;/value&gt;
&lt;/list&gt;
&lt;/property&gt;
&lt;!--注入map entry--&gt;
&lt;property name=&quot;maps&quot;&gt;
&lt;map&gt;
&lt;entry key=&quot;aa&quot; value=&quot;xiaohei&quot;/&gt;
&lt;entry key=&quot;bb&quot; value=&quot;xiaoming&quot;/&gt;
&lt;entry key=&quot;cc&quot; value=&quot;xiaosan&quot;/&gt;
&lt;/map&gt;
&lt;/property&gt;
&lt;!--注入properties 集合使用props标签--&gt;
&lt;property name=&quot;props&quot;&gt;
&lt;props&gt;
&lt;prop key=&quot;url&quot;&gt;jdbc:mysql://localhost:3306/test&lt;/prop&gt;
&lt;prop key=&quot;driver&quot;&gt;com.mysql.jdbc.Driver&lt;/prop&gt;
&lt;prop key=&quot;username&quot;&gt;hr&lt;/prop&gt;
&lt;prop key=&quot;password&quot;&gt;hr&lt;/prop&gt;
&lt;/props&gt;
&lt;/property&gt;
</code></pre>
<blockquote>
<p><code>**注意**: 引用类型使用ref属性注入,基本类型使用value属性注入</code></p>
</blockquote>
<hr>
<h2 id="6构造注入">6.构造注入</h2>
<p>1.定义:使用类中构造方法为类中成员变量赋值的过程</p>
<p>2.语法:<br>
1.需要哪个组件属性将谁声明为成员变量,并提供公开的构造方法<br>
2.在配置文件中对应的组件标签内部使用<constructor-arg>标签进行注入</p>
<pre><code class="language-xml">&lt;!--管理DAO组件
1. SET方式注入 注入时使用property标签
2. 构造方法注入 注入时使用&lt;constructor-arg&gt;标签
--&gt;
&lt;bean class=&quot;cdi.EmpDAOImpl&quot; id=&quot;empDAO&quot;&gt;
&lt;!-- 使用构造注入 --&gt;
&lt;constructor-arg index=&quot;0&quot; name=&quot;id&quot; value=&quot;1&quot;/&gt;
&lt;constructor-arg index=&quot;1&quot; name=&quot;name&quot; value=&quot;xiaohei&quot;/&gt;
&lt;constructor-arg index=&quot;2&quot; name=&quot;age&quot; value=&quot;12&quot;/&gt;
&lt;!-- 注入数组 --&gt;
&lt;constructor-arg index=&quot;3&quot; name=&quot;qqs&quot;&gt;
&lt;array&gt;
&lt;value&gt;xxx&lt;/value&gt;
&lt;value&gt;222&lt;/value&gt;
&lt;value&gt;333&lt;/value&gt;
&lt;/array&gt;
&lt;/constructor-arg&gt;
&lt;!-- 注入list --&gt;
&lt;constructor-arg index=&quot;4&quot; name=&quot;habbys&quot;&gt;
&lt;list&gt;
&lt;value&gt;haoren&lt;/value&gt;
&lt;value&gt;huairen&lt;/value&gt;
&lt;value&gt;xiaodoudou&lt;/value&gt;
&lt;/list&gt;
&lt;/constructor-arg&gt;
&lt;/bean&gt;
</code></pre>
<blockquote>
<p><code>注意:构造注入并不常用,不过在一些框架类中必须使用构造注入,这里先了解其注入语法即可。</code></p>
</blockquote>
<h2 id="7自动注入">7.自动注入</h2>
<p>1.自动注入【了解】<br>
在spring工厂配置文件中通过制定自动注入的方式开启组件属性的自动赋值</p>
<p>注意:<br>
1. 底层使用原理也是SET方式注入<br>
2. 自动注入需要在对应组件标签开启才能使用<br>
3. 只能用于引用类型的注入|对象类型|组件类型的注入</p>
<p>2.自动注入语法<br>
1.需要哪个组件属性将谁声明为成员变量,并提供公开的构造方法<br>
2.在对应的组件标签上加入autowired属性并制定自动注入方式即可完成注入</p>
<p>3.注入示例</p>
<pre><code class="language-xml"> &lt;!-- 管理DAO组件 --&gt;
&lt;bean CLASS= &quot;adi.StudentDAOImpl&quot; id=&quot;studentDAO&quot;/&gt;
&lt;bean CLASS= &quot;adi.StudentDAONewImpl&quot; id=&quot;studentNewDAO&quot;/&gt;
&lt;!-- 管理Service组件 --&gt;
autowise:用来给组件中成员变量完成自动赋值操作
byType:根据类型完成自动注入,根据成员变量类型去工厂中找,找到对应类型完成赋值,找不到不赋值
byName:根据名称完成自动注入,根据成员变量名字去工厂中获取与之一致的名字,找到对应类型完成赋值,找不到不赋值
</code></pre>
<blockquote>
<ul>
<li><strong>autowire=”byName”</strong></li>
</ul>
<blockquote>
<p><code>根据注入的属性名与配置文件中bean的id匹配,一致则注入,不一致报错</code></p>
</blockquote>
<ul>
<li><strong>autowire=”byType”</strong></li>
</ul>
<blockquote>
<p><code>根据注入的属性类型,与配置文件中的类型匹配,类型一致注入(在多个实现类时,会产生歧义)</code></p>
</blockquote>
<p><code>注意: 无论使用以上那种方式注入都需要为属性提供set方法</code></p>
</blockquote>
<hr>
<h2 id="8bean的创建模式">8.bean的创建模式</h2>
<p>spring中工厂创建对象的模式:工厂默认在管理对象都是单例方式,无论在工程获取多少次始终获取的是同一个对象</p>
<p><strong>singleton:单例 默认</strong><br>
service dao 单例形式管理</p>
<pre><code> 在工厂中`全局唯一,只创建一次`
</code></pre>
<p><strong>prototype: 多例</strong><br>
struts2 action 必须以多例形式管理</p>
<pre><code> `全局不唯一,每次使用都会创建一个新的对象`
</code></pre>
<pre><code class="language-java">&lt;!-- 管理DAO组件
scope:用来指定工程创建对象的模式
默认值: singleton 单例
prototype 多例
--&gt;
&lt;bean id=&quot;tagDAO&quot; class=&quot;scope.TagDAOImpl&quot; scope=&quot;prototype&quot;&gt;&lt;/bean&gt;
service,dao -----&gt; singleton
struts2 action -----&gt; prototype
</code></pre>
<blockquote>
<p><code>注意:在项目开发中service,dao组件单例,struts2的Action必须为:多例</code></p>
</blockquote>
<hr>
<h2 id="9bean的生产原理">9.bean的生产原理</h2>
<blockquote>
<p><code>原理</code>: <code>反射+构造方法</code></p>
</blockquote>
<pre><code class="language-java"> UserDAOImpl userDAO = (UserDAOImpl) Class.forName(&quot;com.baizhi.dao.UserDAOImpl&quot;).newInstance();
System.out.println(userDAO);
</code></pre>
<hr>
<h2 id="10bean的生命周期">10.bean的生命周期</h2>
<blockquote>
<ul>
<li>
<p><code>何时创建</code></p>
<p>随着工厂启动, <strong><code>所有单例bean随之创建 非单例的bean,每次使用时创建</code></strong></p>
</li>
<li>
<p><code>何时销毁</code></p>
<p>工厂**<code>关闭,所有bean随之销毁</code>** ( 注意: <code>spring对多例bean管理松散,不会负责多例bean的销毁</code>)</p>
</li>
</ul>
</blockquote>
<h2 id="11bean工厂创建对象的好处">11.bean工厂创建对象的好处</h2>
<blockquote>
<ol>
<li><code>使用配置文件管理java类,再生产环境中更换类的实现时不需要重新部署,修改文件即可</code></li>
<li><code>spring默认使用单例的模式创建bean,减少内存的占用</code></li>
<li><code>通过依赖注入建立了类与类之间的关系(使java之间关系更为清晰,方便了维护与管理)</code></li>
</ol>
</blockquote>
">Spring01基础知识</a>
</div>