-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
103 lines (49 loc) · 66 KB
/
search.xml
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
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>shell脚本基础用法</title>
<link href="/2022/09/22/shell%E8%84%9A%E6%9C%AC%E5%9F%BA%E7%A1%80%E7%94%A8%E6%B3%95/"/>
<url>/2022/09/22/shell%E8%84%9A%E6%9C%AC%E5%9F%BA%E7%A1%80%E7%94%A8%E6%B3%95/</url>
<content type="html"><![CDATA[<h1 id="shell脚本基础用法"><a href="#shell脚本基础用法" class="headerlink" title="shell脚本基础用法"></a>shell脚本基础用法</h1><h2 id="用例为hexo"><a href="#用例为hexo" class="headerlink" title="用例为hexo"></a>用例为hexo</h2><ol><li><p>编写一个脚本</p><p>一般为.sh结尾,也可为txt或者不加后缀</p><p>#后的字符可以作为注释</p><ul><li>hexo.sh</li></ul><pre class=" language-shell"><code class="language-shell">#!/bin/bash# 上面`#!`为一种约定标记,它告诉系统这个脚本需要用什么样的解释器运行# 以下为执行的代码hexo cleanhexo g # 生成静态文件hexo s # 运行服务</code></pre></li><li><p>给文件授权 chmod +x ./hexo.sh</p><p>不然运行不了</p></li></ol>]]></content>
<categories>
<category> linux </category>
<category> shell </category>
</categories>
<tags>
<tag> linux </tag>
<tag> shell </tag>
</tags>
</entry>
<entry>
<title>java-Spring</title>
<link href="/2022/09/19/java-Spring/"/>
<url>/2022/09/19/java-Spring/</url>
<content type="html"><![CDATA[<h1 id="Spring"><a href="#Spring" class="headerlink" title="Spring"></a>Spring</h1><h1 id="1、Spring"><a href="#1、Spring" class="headerlink" title="1、Spring"></a>1、Spring</h1><h2 id="1-1简介"><a href="#1-1简介" class="headerlink" title="1.1简介"></a>1.1简介</h2><ul><li>Spring:春天——>给软件行业带来了春天</li><li>2002,首次推出了Spring框架的雏形:interface框架!</li><li>Spring框架即以interface21框架为基础,经过重新设计,并不断丰富其内涵,于2004年3月24日,发布了1.0正式版</li><li>Rod Johnson,Spring Framework创始人,著名作者。很难想象Rod Johnson的学历,真的让好多人大吃一惊,他是悉尼大学的博士,然而他的专业不是计算机,而是音乐学。</li><li></li><li>spring理念:使现有的技术更加容易使用,本身是一个大杂烩。</li><li></li><li>SSH:Struct2 + Spring + Hibernate</li><li></li><li>SSM: SpringMVC + Spring + Mybatis</li><li>官网:<a href="https://spring.io/projects/spring-framework#overview">https://spring.io/projects/spring-framework#overview</a></li><li>官方下载地址:<a href="https://repo.spring.io/ui/native/release/org/springframework/spring">https://repo.spring.io/ui/native/release/org/springframework/spring</a></li><li>官方文档:<a href="https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/index.html">https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/index.html</a></li><li>maven包:</li><li>spring-webmvc</li><li>spring-jdbc</li></ul><h2 id="1-2优点"><a href="#1-2优点" class="headerlink" title="1.2优点"></a>1.2优点</h2><ul><li>Spring是一个开源的免费框架</li><li>Spring是一个轻量级的,非入侵式的框架</li><li>控制反转(IOC),面向切面编程(AOP)</li><li>支持事务的处理,对框架整合的支持</li><li>总结:Spring是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架</li></ul><h2 id="1-3组成"><a href="#1-3组成" class="headerlink" title="1.3组成"></a>1.3组成</h2><p><img src="/images/Spring%E7%BB%84%E6%88%90.jpg"></p><h2 id="1-4"><a href="#1-4" class="headerlink" title="1.4"></a>1.4</h2><p>1.UserDao接口</p><p>2.UserDaoImpl实现类</p><p>3.UserService业务接口</p><p>4.UserServiceImpl业务实现类</p><p>在之前的业务中,用户的需求可能会影响原本的代码,程序员需要根据用户的需求去修改源代码!如果程序代码量十分大,修改一次的成本代价十分昂贵!<br>使用Set接口实现,发生了革命性的变化</p><h1 id="2-IOC理论推导"><a href="#2-IOC理论推导" class="headerlink" title="2.IOC理论推导"></a>2.IOC理论推导</h1><p>控制反转IoC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现IoC的一种方法,也有人认为DI只是IoC的另一种说法。在没有IoC的程序中,我们使用面向对象编程,对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方,个人认为所谓控制反转就是:获得依赖对象的方式反转了。<br>采用XML方式配置Bean的时候,Bean的定义信息是和实现分离的,而采用注解的方式可以把二者合为一体,Bean的定义信息直接以注解的形式定义在实现类中,从而达到了零配置的目的。<br>控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入(Dependenccy Injection,DI)。</p><p>IOC是一种编程思想,由主动编程变成了被动接收<br>可以通过newClassPathXmlApplicationContext去浏览一下底层源码,<br>ok,到了现在,我们彻底不用再去程序中进行改动了,要实现不同的操作,只需要在xml配置文件中进行修改,所谓的IOC,一句话搞定:对象由Spring创建,管理,装配!</p><h1 id="3、IOC创建对象的方式"><a href="#3、IOC创建对象的方式" class="headerlink" title="3、IOC创建对象的方式"></a>3、IOC创建对象的方式</h1><h2 id="3-1使用无参构造创建对象,默认!"><a href="#3-1使用无参构造创建对象,默认!" class="headerlink" title="3.1使用无参构造创建对象,默认!"></a>3.1使用无参构造创建对象,默认!</h2><p>*必须有无参构造方法</p><p>3.2、假设我们要使用有参构造构建对象</p><ul><li>下标赋值</li></ul><pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true"><!--下标赋值--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>user<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.gty.pojo.User<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>constructor-arg</span> <span class="token attr-name">index</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>0<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>tiantian<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span></code></pre><ul><li>类型赋值</li></ul><pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true"><!--类型赋值(不建议使用)--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>user<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.gty.pojo.User<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>constructor-arg</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>java.lang.String<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>tiantian<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span></code></pre><ul><li>参数名赋值</li></ul><pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true"><!--参数名赋值--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>user<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.gty.pojo.User<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>constructor-arg</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>name<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>tiantian<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span></code></pre><p>当在xml配置文件加载时,容器中管理里的对象就已经初始化了(注册bean时就已经被实例化了),无需等到getBean()</p><h1 id="4、Spring配置"><a href="#4、Spring配置" class="headerlink" title="4、Spring配置"></a>4、Spring配置</h1><h2 id="4-1-别名"><a href="#4-1-别名" class="headerlink" title="4.1 别名"></a>4.1 别名</h2><pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true"><!--别名,如果添加了别名,也可以通过别名获取到这个对象--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>alias</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>user<span class="token punctuation">"</span></span> <span class="token attr-name">alias</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>usernew<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span></code></pre><pre class=" language-java"><code class="language-java">ApplicationContext context<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">ClassPathXmlApplicationContext</span><span class="token punctuation">(</span><span class="token string">"beans.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> User user<span class="token operator">=</span><span class="token punctuation">(</span>User<span class="token punctuation">)</span>context<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token string">"usernew"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> user<span class="token punctuation">.</span><span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 输出内容</span></code></pre><h2 id="4-2-Bean的配置"><a href="#4-2-Bean的配置" class="headerlink" title="4.2 Bean的配置"></a>4.2 Bean的配置</h2><pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true"><!-- id:bean的唯一标识符,相当于学过的对象名 class:bean对象所对应的全限定名:包名+类型 name:也是别名,name可以取多个别名 --></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>usertt<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.gty.pojo.UserT<span class="token punctuation">"</span></span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>usergg,useryy userty;usergty<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>name<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>gtygty<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span></code></pre><h2 id="4-3-import"><a href="#4-3-import" class="headerlink" title="4.3 import"></a>4.3 import</h2><p>import一般用于团队开发使用,他可以将多个配置文件导入合并为一个。<br>假设现在项目中有多个人开发,这三个人负责不同的类开发,不同的类需要注册在不同的bean中,我们可以利用import将所有人的beans.xml合并为一个总的applicationContext.xml</p><pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>import</span> <span class="token attr-name">resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>beans.xml<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>import</span> <span class="token attr-name">resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>bean1.xml<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>import</span> <span class="token attr-name">resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>bean2.xml<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>import</span> <span class="token attr-name">resource</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>bean3.xml<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span></code></pre><h1 id="5、依赖注入"><a href="#5、依赖注入" class="headerlink" title="5、依赖注入"></a>5、依赖注入</h1><h2 id="5-1-构造器注入"><a href="#5-1-构造器注入" class="headerlink" title="5.1 构造器注入"></a>5.1 构造器注入</h2><h2 id="5-2-Set方式注入"><a href="#5-2-Set方式注入" class="headerlink" title="5.2 *Set方式注入"></a>5.2 *Set方式注入</h2><p>依赖注入:set注入依赖注入:set注入</p><ul><li><p>依赖:bean对象的创建依赖于容器</p></li><li><p>注入:bean对象中的所有属性,由容器来注入</p></li><li><p>beans.xml<br>各种方式注入</p></li></ul><pre class=" language-java"><code class="language-java"> <span class="token keyword">private</span> String name<span class="token punctuation">;</span> <span class="token keyword">private</span> String address<span class="token punctuation">;</span> <span class="token keyword">private</span> String<span class="token punctuation">[</span><span class="token punctuation">]</span> books<span class="token punctuation">;</span> <span class="token keyword">private</span> List<span class="token operator"><</span>String<span class="token operator">></span> hobbies<span class="token punctuation">;</span> <span class="token keyword">private</span> Map<span class="token operator"><</span>String<span class="token punctuation">,</span>String<span class="token operator">></span> card<span class="token punctuation">;</span> <span class="token keyword">private</span> Set<span class="token operator"><</span>String<span class="token operator">></span> games<span class="token punctuation">;</span> <span class="token keyword">private</span> String wife<span class="token punctuation">;</span> <span class="token keyword">private</span> Properties info<span class="token punctuation">;</span></code></pre><p>完善注入</p><pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>address<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.gty.pojo.Address<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>address<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>xuzhou<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>student<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.gty.pojo.Student<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token comment" spellcheck="true"><!--第一种,普通值注入,value--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>name<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>天天<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token comment" spellcheck="true"><!--第二种,Bean注入,ref--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>address<span class="token punctuation">"</span></span> <span class="token attr-name">ref</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>address<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token comment" spellcheck="true"><!--第三种,数组注入,--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>books<span class="token punctuation">"</span></span> <span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>array</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>红楼梦<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>水浒传<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>西游记<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>array</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>property</span><span class="token punctuation">></span></span> <span class="token comment" spellcheck="true"><!--第四种,List集合注入--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>hobbies<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>list</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>读书<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>画画<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>跑步<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>list</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>property</span><span class="token punctuation">></span></span> <span class="token comment" spellcheck="true"><!--第五种,Map集合注入--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>card<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>map</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>entry</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>银行卡<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>545121212<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>entry</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>身份证<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>545121212<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>map</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>property</span><span class="token punctuation">></span></span> <span class="token comment" spellcheck="true"><!--第六种,Set集合注入--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>games<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>set</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>ppt<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>doc<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>value</span><span class="token punctuation">></span></span>vvc<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>value</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>set</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>property</span><span class="token punctuation">></span></span> <span class="token comment" spellcheck="true"><!--第七种,null--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>wife<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>null<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token comment" spellcheck="true"><!--第八种,properties--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>info<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>props</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>prop</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>学号<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>2050214801<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>prop</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>prop</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>性别<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>男<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>prop</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>prop</span> <span class="token attr-name">key</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>姓名<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>张飒<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>prop</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>props</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>property</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span></code></pre><h2 id="5-3-扩展方式注入"><a href="#5-3-扩展方式注入" class="headerlink" title="5.3 扩展方式注入"></a>5.3 扩展方式注入</h2><p>我们可以使用p命令空间和c命令空间进行注入</p><ul><li>使用</li></ul><pre class=" language-xml"><code class="language-xml"><span class="token prolog"><?xml version="1.0" encoding="UTF-8"?></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>beans</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.springframework.org/schema/beans<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xmlns:</span>p</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.springframework.org/schema/p<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xmlns:</span>c</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.springframework.org/schema/c<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token comment" spellcheck="true"><!--p命名空间注入,可以直接注入属性的值,property--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>user<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.gty.pojo.User<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">p:</span>name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>tiantian<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">p:</span>age</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>19<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token comment" spellcheck="true"><!--c命名空间注入,通过构造器注入,construct-args--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>user2<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.gty.pojo.User<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">c:</span>age</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>23<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">c:</span>name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>gty<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>beans</span><span class="token punctuation">></span></span></code></pre><ul><li>测试</li></ul><pre class=" language-java"><code class="language-java"><span class="token annotation punctuation">@Test</span><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">test2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">&</span>#<span class="token number">123</span><span class="token punctuation">;</span>ApplicationContext context<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">ClassPathXmlApplicationContext</span><span class="token punctuation">(</span><span class="token string">"userbeans.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>User user<span class="token operator">=</span> context<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">,</span>User<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span> </code></pre><h2 id="5-4-Bean的作用域"><a href="#5-4-Bean的作用域" class="headerlink" title="5.4 Bean的作用域"></a>5.4 Bean的作用域</h2><p><img src="image/Bean%E4%BD%9C%E7%94%A8%E5%9F%9F.jpg"></p><ul><li><p>单例模式(spring默认机制)</p><bean id="user2" class="com.gty.pojo.User" c:age="23" c:name="gty" scope="singleton"/></li><li><p>原型模式:每次从容器中get时,都会产生一个新的对象</p><bean id="user2" class="com.gty.pojo.User" c:age="23" c:name="gty" scope="prototype"/></li><li><p>重点在scope</p></li></ul><h1 id="6-自动装配"><a href="#6-自动装配" class="headerlink" title="6.自动装配"></a>6.自动装配</h1><ul><li>自动装配是Spring满足bean依赖的一种方式</li><li>Spring会在上下文中自动寻找,并自动给bean装配属性<br>在Spring中有三种装配方式:<br>1.在xml中显式配置<br>2.在java中显式配置<br>3.隐式的自动装配bean(重要)</li></ul><h2 id="6-1-ByName自动装配"><a href="#6-1-ByName自动装配" class="headerlink" title="6.1 ByName自动装配"></a>6.1 ByName自动装配</h2><pre class=" language-xml"><code class="language-xml"><span class="token comment" spellcheck="true"><!--byName:会自动在容器上下文查找,和自己对象set方法后面的值对应的bean id--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>person<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.gty.pojo.Person<span class="token punctuation">"</span></span> <span class="token attr-name">autowire</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>byName<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>name<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>天天<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token comment" spellcheck="true"><!-- <property name="cat" ref="cat"/>--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span></code></pre><h2 id="6-2-ByType自动装配"><a href="#6-2-ByType自动装配" class="headerlink" title="6.2 ByType自动装配"></a>6.2 ByType自动装配</h2><pre class=" language-xml"><code class="language-xml"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>cat<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.gty.pojo.Cat<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>dog<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.gty.pojo.Dog<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span> <span class="token comment" spellcheck="true"><!--byType:会自动在容器上下文查找,和自己对象属性类型相同的bean--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>bean</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>person<span class="token punctuation">"</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>com.gty.pojo.Person<span class="token punctuation">"</span></span> <span class="token attr-name">autowire</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>byType<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>property</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>name<span class="token punctuation">"</span></span> <span class="token attr-name">value</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>天天<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span><span class="token comment" spellcheck="true"><!-- <property name="cat" ref="cat"/>--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>bean</span><span class="token punctuation">></span></span></code></pre><ul><li>注意autowire</li><li>byName,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致。</li><li>byType,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致。</li></ul><h2 id="6-3-使用注解实现自动装配"><a href="#6-3-使用注解实现自动装配" class="headerlink" title="6.3 使用注解实现自动装配"></a>6.3 使用注解实现自动装配</h2><p>jdk1.5支持的注解,Spring2.5就支持注解了<br>使用注解须知:<br>1.导入约束:context约束<br>2.配置注解的支持:context:annotation-config/</p><ul><li>beans.xml的依赖</li></ul><pre class=" language-xml"><code class="language-xml"><span class="token prolog"><?xml version="1.0" encoding="UTF-8"?></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>beans</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.springframework.org/schema/beans<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xmlns:</span>context</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.springframework.org/schema/context<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xmlns:</span>aop</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.springframework.org/schema/aop<span class="token punctuation">"</span></span> <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token comment" spellcheck="true"><!--开启注解支持--></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token namespace">context:</span>annotation-config</span><span class="token punctuation">/></span></span></code></pre><p><code>@Autowired</code>:<br>直接在属性上使用即可!也可以在set方式上使用!</p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">People</span> <span class="token operator">&</span>#<span class="token number">123</span><span class="token punctuation">;</span> <span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> Cat cat<span class="token punctuation">;</span> <span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> Dog dog<span class="token punctuation">;</span> <span class="token annotation punctuation">@Autowired</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setCat</span><span class="token punctuation">(</span>Cat cat<span class="token punctuation">)</span> <span class="token operator">&</span>#<span class="token number">123</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>cat <span class="token operator">=</span> cat<span class="token punctuation">;</span> <span class="token operator">&</span>#<span class="token number">125</span><span class="token punctuation">;</span></code></pre><ul><li>使用Autowired我们可以不用编写Set方法,前提是这个自动装配的属性在IOC(Spring)容器中存在,且符合名字byName!</li><li>如果<code>@Autowired</code>自动装配的环境比较复杂,自动装配无法通过一个注解<code>@Autowired</code>完成的时候 ,可以使用<code>@Qualifier(“xxx”)</code>去配合<code>@Autowired</code>的使用,即既无法通过<code>byName</code>又无法通过<code>byType</code>时,指定一个唯一的bean对象注入</li></ul><pre class=" language-java"><code class="language-java"> <span class="token comment" spellcheck="true">// 此为beans.xml </span> <span class="token operator"><</span>bean id<span class="token operator">=</span><span class="token string">"cat1111"</span> <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"com.gty.pojo.Cat"</span><span class="token operator">/</span><span class="token operator">></span> <span class="token operator"><</span>bean id<span class="token operator">=</span><span class="token string">"cat222"</span> <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"com.gty.pojo.Cat"</span><span class="token operator">/</span><span class="token operator">></span> <span class="token operator"><</span>bean id<span class="token operator">=</span><span class="token string">"dog1111"</span> <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"com.gty.pojo.Dog"</span><span class="token operator">/</span><span class="token operator">></span> <span class="token operator"><</span>bean id<span class="token operator">=</span><span class="token string">"dog222"</span> <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"com.gty.pojo.Dog"</span><span class="token operator">/</span><span class="token operator">></span> <span class="token comment" spellcheck="true">// 测试类</span> <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">People</span> <span class="token operator">&</span>#<span class="token number">123</span><span class="token punctuation">;</span> <span class="token annotation punctuation">@Autowired</span> <span class="token annotation punctuation">@Qualifier</span><span class="token punctuation">(</span><span class="token string">"cat222"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> Cat cat<span class="token punctuation">;</span> <span class="token annotation punctuation">@Autowired</span> <span class="token annotation punctuation">@Qualifier</span><span class="token punctuation">(</span><span class="token string">"dog222"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> Dog dog<span class="token punctuation">;</span></code></pre><p><code>@Resource注解</code></p><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">People</span> <span class="token operator">&</span>#<span class="token number">123</span><span class="token punctuation">;</span> <span class="token annotation punctuation">@Resource</span><span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"cat1111"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> Cat cat<span class="token punctuation">;</span> <span class="token annotation punctuation">@Resource</span> <span class="token keyword">private</span> Dog dog<span class="token punctuation">;</span></code></pre><p><code>@Autowired</code>和<code>@Resource</code>区别:</p><ul><li>都是用来自动装配的,都可以放在属性字段上</li><li>@Autowired通过byType实现,必须要求这个对象存在</li><li>@Resoutce默认通过byName方式实现,如果找不到名字,则通过byType实现</li><li>执行顺序不同:@Autowired通过byType实现,@Resoutce通过byName实现</li></ul><h1 id="7-使用Java方式配置Spring"><a href="#7-使用Java方式配置Spring" class="headerlink" title="7.使用Java方式配置Spring"></a>7.使用Java方式配置Spring</h1><p>可以完全不使用Spring的xml配置,全权交给Java实现!<br>JavaConfig是Spring的一个子项目,在Spring4之后变为核心功能。</p><ul><li>实体类</li></ul><pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">//@Component意思是这个类被Spring托管了,注册到了容器中</span><span class="token annotation punctuation">@Component</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token operator">&</span>#<span class="token number">123</span><span class="token punctuation">;</span> <span class="token keyword">private</span> String name<span class="token punctuation">;</span> <span class="token keyword">public</span> String <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&</span>#<span class="token number">123</span><span class="token punctuation">;</span> <span class="token keyword">return</span> name<span class="token punctuation">;</span> <span class="token operator">&</span>#<span class="token number">125</span><span class="token punctuation">;</span> <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"tiantian"</span><span class="token punctuation">)</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span>String name<span class="token punctuation">)</span> <span class="token operator">&</span>#<span class="token number">123</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span> <span class="token operator">&</span>#<span class="token number">125</span><span class="token punctuation">;</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> String <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&</span>#<span class="token number">123</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token string">"User&#123;"</span> <span class="token operator">+</span> <span class="token string">"name='"</span> <span class="token operator">+</span> name <span class="token operator">+</span> <span class="token string">'\''</span> <span class="token operator">+</span> <span class="token string">'&#125;'</span><span class="token punctuation">;</span> <span class="token operator">&</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token operator">&</span>#<span class="token number">125</span><span class="token punctuation">;</span></code></pre><ul><li>配置类</li></ul><pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">//@Configuration是一个配置类,和之前的beans.xml是一样的</span><span class="token annotation punctuation">@Configuration</span><span class="token comment" spellcheck="true">//这个也会被Spring容器托管,注册到容器中,因为它本来也是一个@Component</span><span class="token annotation punctuation">@Import</span><span class="token punctuation">(</span>GtyConfig2<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">GtyConfig</span> <span class="token operator">&</span>#<span class="token number">123</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//注册一个Bean,就相当于之前写的一个Bean标签</span> <span class="token comment" spellcheck="true">//这个方法的名字,相当于bean标签中的id属性</span> <span class="token comment" spellcheck="true">//这个方法的返回值,就相当于bean标签中的class属性</span> <span class="token annotation punctuation">@Bean</span> <span class="token keyword">public</span> User <span class="token function">getUser</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">&</span>#<span class="token number">123</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//就是返回要注入到bean的对象</span> <span class="token operator">&</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token operator">&</span>#<span class="token number">125</span><span class="token punctuation">;</span></code></pre><ul><li>测试类</li></ul><pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyTest</span> <span class="token operator">&</span>#<span class="token number">123</span><span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token operator">&</span>#<span class="token number">123</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//如果完全使用了配置类方式去做,我们就只能通过AnnotationConfig上下文来获取容器,通过配置类的class对象加载</span> ApplicationContext context<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">AnnotationConfigApplicationContext</span><span class="token punctuation">(</span>GtyConfig<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> User getUser<span class="token operator">=</span>context<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span><span class="token string">"getUser"</span><span class="token punctuation">,</span>User<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>getUser<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">&</span>#<span class="token number">125</span><span class="token punctuation">;</span><span class="token operator">&</span>#<span class="token number">125</span><span class="token punctuation">;</span></code></pre>]]></content>
<categories>
<category> java </category>
</categories>
<tags>
<tag> java </tag>
<tag> spring </tag>
</tags>
</entry>
<entry>
<title>debian安装Docker</title>
<link href="/2022/09/12/debian%E5%AE%89%E8%A3%85Docker/"/>
<url>/2022/09/12/debian%E5%AE%89%E8%A3%85Docker/</url>
<content type="html"><![CDATA[<h1 id="debian安装Docker"><a href="#debian安装Docker" class="headerlink" title="debian安装Docker"></a>debian安装Docker</h1><h2 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h2><h2 id="系统要求"><a href="#系统要求" class="headerlink" title="系统要求"></a>系统要求</h2><p><a href="https://so.csdn.net/so/search?q=Docker&spm=1001.2101.3001.7020">Docker</a> 支持以下版本的 Debian (opens new window)操作系统:</p><pre><code>Debian Bullseye 11Debian Buster 10</code></pre><h2 id="卸载旧版本"><a href="#卸载旧版本" class="headerlink" title="卸载旧版本"></a>卸载旧版本</h2><p>旧版本的 Docker 称为 docker 或者 docker-engine,使用以下命令卸载旧版本:</p><pre class=" language-shell"><code class="language-shell">$ sudo apt-get remove docker \ docker-engine \ docker.io</code></pre><h2 id="使用-APT-安装"><a href="#使用-APT-安装" class="headerlink" title="使用 APT 安装"></a>使用 APT 安装</h2><p>由于 apt 源使用 HTTPS 以确保软件下载过程中不被篡改。因此,我们首先需要添加使用 HTTPS 传输的软件包以及 CA 证书。</p><pre class=" language-shell"><code class="language-shell">$ sudo apt-get update$ sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg \ lsb-release</code></pre><p>为了确认所下载软件包的合法性,需要添加软件源的 GPG 密钥。</p><pre class=" language-shell"><code class="language-shell">$ curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg## 官方源# $ curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg</code></pre><p>然后,我们需要向 sources.list 中添加 Docker 软件源:</p><p>在一些基于 Debian 的 Linux 发行版中 $(lsb_release -cs) 可能不会返回 Debian 的版本代号,例如 Kail Linux (opens new window)、 BunsenLabs Linux (opens new window)。<br>在这些发行版中我们需要将下面命令中的 $(lsb_release -cs) 替换为 <a href="https://mirrors.aliyun.com/docker-ce/linux/debian/dists/">https://mirrors.aliyun.com/docker-ce/linux/debian/dists/</a> 中支持的 Debian 版本代号,例如 buster。</p><pre class=" language-shell"><code class="language-shell">$ echo \ "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/debian \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null# 官方源# $ echo \# "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \# $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null</code></pre><h2 id="启动-Docker"><a href="#启动-Docker" class="headerlink" title="启动 Docker"></a>启动 Docker</h2><pre class=" language-shell"><code class="language-shell">$ sudo systemctl enable docker$ sudo systemctl start docker</code></pre>]]></content>
<categories>
<category> docker </category>
</categories>
<tags>
<tag> docker </tag>
<tag> linux </tag>
</tags>
</entry>
<entry>
<title>Hello World</title>
<link href="/2022/09/11/hello-world/"/>
<url>/2022/09/11/hello-world/</url>
<content type="html"><![CDATA[<p>Welcome to <a href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues">GitHub</a>.</p><h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><pre class=" language-bash"><code class="language-bash">$ hexo new <span class="token string">"My New Post"</span></code></pre><p>More info: <a href="https://hexo.io/docs/writing.html">Writing</a></p><h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><pre class=" language-bash"><code class="language-bash">$ hexo server</code></pre><p>More info: <a href="https://hexo.io/docs/server.html">Server</a></p><h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><pre class=" language-bash"><code class="language-bash">$ hexo generate</code></pre><p>More info: <a href="https://hexo.io/docs/generating.html">Generating</a></p><h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><pre class=" language-bash"><code class="language-bash">$ hexo deploy</code></pre><p>More info: <a href="https://hexo.io/docs/one-command-deployment.html">Deployment</a></p>]]></content>
</entry>
</search>