Spring依赖注入

时间:2024-10-30 10:24:03

一、什么是依赖注入

DI (Dependency Injection):依赖注入是指在 Spring IOC 容器创建对象的过程中,将所依赖的对象通过配置进行注入。我们可以通过依赖注入的方式来降低对象间的耦合度。

在软件工程中,对象之间的耦合度就是对象之间的依赖性。对象之间的耦合越高,维护成本越高,因此对象的设计应使对象之间的耦合越小越好。

1 类的关系

继承、实现、依赖、关联、聚合、组合。

2 关系强度

继承 = 实现 > 组合 > 聚合 > 关联 > 依赖

二、为什么使用依赖注入

1 开闭原则

1.1 定义

OCP (Open Close Principle): 软件本身应该是可扩展的,而不可修改的。也就是,对扩展开放,对修改封闭的

1.2 开闭原则优点

易扩展。开闭原则的定义就要求对扩展开放。

易维护。软件开发中,对现有代码的修改是一件很有风险的事情,符合开闭原则的设计在扩展时无需修改现有代码,规避了这个风险,大大提交了可维护性。

2 高内聚,低耦合

高内聚是指相关度比较高的部分尽可能的集中,不要分散。

低耦合就是说两个相关的模块尽可以能把依赖的部分降低到最小,不要产生强依赖。

三、依赖注入的方式

在使用依赖注入时,如果注入的是 Bean 对象,那么要求注入的 Bean 对象与被注入的Bean 对象都需要 Spring IOC 容器来实例化。

1 通过 Set 方法注入

需要为注入的成员变量提供 Set 方法。

1.1 POJO中添加属性、set方法和toString方法

  1. <span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">GirlfriendOYY</span> <span style="color:#770088">implements</span> <span style="color:#000000">Girlfriend</span> {
  2.    <span style="color:#770088">private</span> <span style="color:#008855">double</span> <span style="color:#000000">hight</span>;
  3.    <span style="color:#770088">private</span> <span style="color:#008855">String</span> <span style="color:#000000">education</span>;
  4.    <span style="color:#555555">@Override</span>
  5.    <span style="color:#770088">public</span> <span style="color:#008855">String</span> <span style="color:#000000">toString</span>() {
  6.        <span style="color:#770088">return</span> <span style="color:#aa1111">"GirlfriendOYY{"</span> <span style="color:#981a1a">+</span>
  7.                <span style="color:#aa1111">"hight="</span> <span style="color:#981a1a">+</span> <span style="color:#000000">hight</span> <span style="color:#981a1a">+</span>
  8.                <span style="color:#aa1111">", education='"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">education</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">'\''</span> <span style="color:#981a1a">+</span>
  9.                <span style="color:#aa1111">'}'</span>;
  10.   }
  11.    <span style="color:#770088">public</span> <span style="color:#008855">double</span> <span style="color:#000000">getHight</span>() {
  12.        <span style="color:#770088">return</span> <span style="color:#000000">hight</span>;
  13.   }
  14.    <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#000000">setHight</span>(<span style="color:#008855">double</span> <span style="color:#000000">hight</span>) {
  15.        <span style="color:#770088">this</span>.<span style="color:#000000">hight</span> <span style="color:#981a1a">=</span> <span style="color:#000000">hight</span>;
  16.   }
  17.    <span style="color:#770088">public</span> <span style="color:#008855">String</span> <span style="color:#000000">getEducation</span>() {
  18.        <span style="color:#770088">return</span> <span style="color:#000000">education</span>;
  19.   }
  20.    <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#000000">setEducation</span>(<span style="color:#008855">String</span> <span style="color:#000000">education</span>) {
  21.        <span style="color:#770088">this</span>.<span style="color:#000000">education</span> <span style="color:#981a1a">=</span> <span style="color:#000000">education</span>;
  22.   }
  23. }</span></span>

1.2 Spring的xml配置文件

  1. <span style="background-color:#f8f8f8"><span style="color:#333333">    <span style="color:#aa5500"><!--1.1 通过构造方法创建对象--></span>
  2.    <span style="color:#117700"><</span><span style="color:#117700">bean</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"gf1"</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">""</span>
  3.          <span style="color:#0000cc">scope</span>=<span style="color:#aa1111">"prototype"</span> <span style="color:#0000cc">lazy-init</span>=<span style="color:#aa1111">"false"</span><span style="color:#117700">></span>
  4.        <span style="color:#117700"><</span><span style="color:#117700">property</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"education"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"本科"</span> <span style="color:#117700">/></span>
  5.        <span style="color:#117700"><</span><span style="color:#117700">property</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"hight"</span> <span style="color:#117700">></span>
  6.            <span style="color:#117700"><</span><span style="color:#117700">value</span><span style="color:#117700">></span>1.65<span style="color:#117700"></</span><span style="color:#117700">value</span><span style="color:#117700">></span>
  7.        <span style="color:#117700"></</span><span style="color:#117700">property</span><span style="color:#117700">></span>
  8.    <span style="color:#117700"></</span><span style="color:#117700">bean</span><span style="color:#117700">></span></span></span>

1.3 测试

  1. <span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">TestGirlfriend</span> {
  2.    <span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
  3.        <span style="color:#000000">ApplicationContext</span> <span style="color:#000000">ac</span> <span style="color:#981a1a">=</span> <span style="color:#770088">new</span> <span style="color:#000000">ClassPathXmlApplicationContext</span>(<span style="color:#aa1111">"application_context.xml"</span>);
  4.        <span style="color:#000000">Girlfriend</span> <span style="color:#000000">gf</span> <span style="color:#981a1a">=</span> (<span style="color:#000000">Girlfriend</span>) <span style="color:#000000">ac</span>.<span style="color:#000000">getBean</span>(<span style="color:#aa1111">"gf1"</span>);
  5.        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">gf</span>);
  6.   }
  7. }</span></span>

运行结果

2 通过构造方法注入

Bean 对象中需要提供有参的构造方法

name:根据参数名称识别参数

index:根据参数的位置来识别参数

type:根据参数的类型识别参数

2.1 POJO中添加有参构造方法

  1. <span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">GirlfriendOYY</span> <span style="color:#770088">implements</span> <span style="color:#000000">Girlfriend</span> {
  2.    <span style="color:#770088">private</span> <span style="color:#008855">double</span> <span style="color:#000000">hight</span>;
  3.    <span style="color:#770088">private</span> <span style="color:#008855">String</span> <span style="color:#000000">education</span>;
  4.    <span style="color:#770088">public</span> <span style="color:#000000">GirlfriendOYY</span>(<span style="color:#008855">double</span> <span style="color:#000000">hight</span>, <span style="color:#008855">String</span> <span style="color:#000000">education</span>) {
  5.        <span style="color:#770088">this</span>.<span style="color:#000000">hight</span> <span style="color:#981a1a">=</span> <span style="color:#000000">hight</span>;
  6.        <span style="color:#770088">this</span>.<span style="color:#000000">education</span> <span style="color:#981a1a">=</span> <span style="color:#000000">education</span>;
  7.   }
  8. }</span></span>

2.2 Spring的xml配置文件

  1. <span style="background-color:#f8f8f8"><span style="color:#333333">    <span style="color:#117700"><</span><span style="color:#117700">bean</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"gf4"</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">""</span><span style="color:#117700">></span>
  2.        <span style="color:#117700"><</span><span style="color:#117700">constructor-arg</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"education"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"专科"</span><span style="color:#117700">/></span>
  3.        <span style="color:#117700"><</span><span style="color:#117700">constructor-arg</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"hight"</span> <span style="color:#117700">></span>
  4.            <span style="color:#117700"><</span><span style="color:#117700">value</span><span style="color:#117700">></span>1.65<span style="color:#117700"></</span><span style="color:#117700">value</span><span style="color:#117700">></span>
  5.        <span style="color:#117700"></</span><span style="color:#117700">constructor-arg</span><span style="color:#117700">></span>
  6.    <span style="color:#117700"></</span><span style="color:#117700">bean</span><span style="color:#117700">></span>
  7.    <span style="color:#117700"><</span><span style="color:#117700">bean</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"gf5"</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">""</span><span style="color:#117700">></span>
  8.        <span style="color:#117700"><</span><span style="color:#117700">constructor-arg</span> <span style="color:#0000cc">index</span>=<span style="color:#aa1111">"0"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"1.68"</span> <span style="color:#117700">/></span>
  9.        <span style="color:#117700"><</span><span style="color:#117700">constructor-arg</span> <span style="color:#0000cc">index</span>=<span style="color:#aa1111">"1"</span> <span style="color:#117700">></span>
  10.            <span style="color:#117700"><</span><span style="color:#117700">value</span><span style="color:#117700">></span>高中<span style="color:#117700"></</span><span style="color:#117700">value</span><span style="color:#117700">></span>
  11.        <span style="color:#117700"></</span><span style="color:#117700">constructor-arg</span><span style="color:#117700">></span>
  12.    <span style="color:#117700"></</span><span style="color:#117700">bean</span><span style="color:#117700">></span>
  13.    <span style="color:#117700"><</span><span style="color:#117700">bean</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"gf6"</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">""</span><span style="color:#117700">></span>
  14.        <span style="color:#117700"><</span><span style="color:#117700">constructor-arg</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"double"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"1.73"</span> <span style="color:#117700">/></span>
  15.        <span style="color:#117700"><</span><span style="color:#117700">constructor-arg</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">""</span> <span style="color:#117700">></span>
  16.            <span style="color:#117700"><</span><span style="color:#117700">value</span><span style="color:#117700">></span>硕士<span style="color:#117700"></</span><span style="color:#117700">value</span><span style="color:#117700">></span>
  17.        <span style="color:#117700"></</span><span style="color:#117700">constructor-arg</span><span style="color:#117700">></span>
  18.    <span style="color:#117700"></</span><span style="color:#117700">bean</span><span style="color:#117700">></span></span></span>

2.3 测试

  1. <span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">TestGirlfriend</span> {
  2.    <span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
  3.        <span style="color:#000000">ApplicationContext</span> <span style="color:#000000">ac</span> <span style="color:#981a1a">=</span> <span style="color:#770088">new</span> <span style="color:#000000">ClassPathXmlApplicationContext</span>(<span style="color:#aa1111">"application_context.xml"</span>);
  4.        <span style="color:#000000">Girlfriend</span> <span style="color:#000000">gf</span> <span style="color:#981a1a">=</span> (<span style="color:#000000">Girlfriend</span>) <span style="color:#000000">ac</span>.<span style="color:#000000">getBean</span>(<span style="color:#aa1111">"gf4"</span>);
  5.        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">gf</span>);
  6.         <span style="color:#000000">gf</span> <span style="color:#981a1a">=</span> (<span style="color:#000000">Girlfriend</span>) <span style="color:#000000">ac</span>.<span style="color:#000000">getBean</span>(<span style="color:#aa1111">"gf5"</span>);
  7.        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">gf</span>);
  8.         <span style="color:#000000">gf</span> <span style="color:#981a1a">=</span> (<span style="color:#000000">Girlfriend</span>) <span style="color:#000000">ac</span>.<span style="color:#000000">getBean</span>(<span style="color:#aa1111">"gf6"</span>);
  9.        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">gf</span>);
  10.   }
  11. }</span></span>

运行结果:

3 自动注入

自动注入的方式有两种,一种是全局配置自动注入,另一种是局部配置自动注入

无论全局配置或局部单独配置,都有 5 个值可以选择:

no:当 autowire 设置为 no 的时候,Spring 就不会进行自动注入。

byName:在 Spring 容器中查找 id 与属性名相同的 bean,并进行注入。需要提供 set 方法。

byType:在 Spring 容器中查找类型与属性名的类型相同的 bean,并进行注入。需要提供 set 方法。

constructor:仍旧是使用 byName 方式,只不过注入的时候,使用构造方式进行注入。

default:全局配置的 default 相当于 no,局部的 default 表示使用全局配置设置。

3.1 局部自动注入

通过 bean 标签中的 autowire 属性配置自动注入。

有效范围:仅针对当前 bean 标签生效。

3.1.1 POJO

接口:

  1. <span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#770088">public</span> <span style="color:#770088">interface</span> <span style="color:#0000ff">GirlfriendProxyService</span> {
  2.    <span style="color:#000000">Girlfriend</span> <span style="color:#000000">findGirlFriend</span>();
  3. }</span></span>

实现类:

  1. <span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">GirlfriendProxyServiceImpl</span> <span style="color:#770088">implements</span> <span style="color:#000000">GirlfriendProxyService</span> {
  2.    <span style="color:#000000">Girlfriend</span> <span style="color:#000000">girlfriend</span>;
  3.    <span style="color:#770088">public</span> <span style="color:#000000">GirlfriendProxyServiceImpl</span>(){}
  4.    <span style="color:#770088">public</span> <span style="color:#000000">GirlfriendProxyServiceImpl</span>(<span style="color:#000000">Girlfriend</span> <span style="color:#000000">girlfriend</span>) {
  5.        <span style="color:#770088">this</span>.<span style="color:#000000">girlfriend</span> <span style="color:#981a1a">=</span> <span style="color:#000000">girlfriend</span>;
  6.   }
  7.    <span style="color:#770088">public</span> <span style="color:#000000">Girlfriend</span> <span style="color:#000000">getGirlfriend</span>() {
  8.        <span style="color:#770088">return</span> <span style="color:#000000">girlfriend</span>;
  9.   }
  10.    <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#000000">setGirlfriend</span>(<span style="color:#000000">Girlfriend</span> <span style="color:#000000">girlfriend</span>) {
  11.        <span style="color:#770088">this</span>.<span style="color:#000000">girlfriend</span> <span style="color:#981a1a">=</span> <span style="color:#000000">girlfriend</span>;
  12.   }
  13.    <span style="color:#555555">@Override</span>
  14.    <span style="color:#770088">public</span> <span style="color:#000000">Girlfriend</span> <span style="color:#000000">findGirlFriend</span>() {
  15.        <span style="color:#770088">return</span> <span style="color:#770088">this</span>.<span style="color:#000000">girlfriend</span>;
  16.   }
  17. }</span></span>

3.1.2 Spring的xml配置

  1. <span style="background-color:#f8f8f8"><span style="color:#333333">    <span style="color:#117700"><</span><span style="color:#117700">bean</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"gf6"</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">""</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"girlfriend"</span><span style="color:#117700">></span>
  2.        <span style="color:#117700"><</span><span style="color:#117700">constructor-arg</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">"double"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"1.73"</span> <span style="color:#117700">/></span>
  3.        <span style="color:#117700"><</span><span style="color:#117700">constructor-arg</span> <span style="color:#0000cc">type</span>=<span style="color:#aa1111">""</span> <span style="color:#117700">></span>
  4.            <span style="color:#117700"><</span><span style="color:#117700">value</span><span style="color:#117700">></span>硕士<span style="color:#117700"></</span><span style="color:#117700">value</span><span style="color:#117700">></span>
  5.        <span style="color:#117700"></</span><span style="color:#117700">constructor-arg</span><span style="color:#117700">></span>
  6.    <span style="color:#117700"></</span><span style="color:#117700">bean</span><span style="color:#117700">></span>
  7.    <span style="color:#aa5500"><!--autowire表示自动装配 :</span>
  8.    <span style="color:#aa5500">byName 根据bean的名字去自动转配(name属性值需要跟构造方法中的name属性值一致),</span>
  9.     <span style="color:#aa5500">byType根据类型自动装配--></span>
  10.    <span style="color:#117700"><</span><span style="color:#117700">bean</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"gfService"</span>
  11.          <span style="color:#0000cc">class</span>=<span style="color:#aa1111">""</span> <span style="color:#0000cc">autowire</span>=<span style="color:#aa1111">"byName"</span><span style="color:#117700">></span>
  12.        <span style="color:#aa5500"><!--<property name="girlfriend" ></span>
  13.            <span style="color:#aa5500"><ref bean="gf6"/></span>
  14.        <span style="color:#aa5500"></property>--></span>
  15.    <span style="color:#117700"></</span><span style="color:#117700">bean</span><span style="color:#117700">></span></span></span>

3.1.3 测试

  1. <span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">TestGFServiceByName</span> {
  2.    <span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
  3.        <span style="color:#000000">ApplicationContext</span> <span style="color:#000000">ac</span> <span style="color:#981a1a">=</span> <span style="color:#770088">new</span> <span style="color:#000000">ClassPathXmlApplicationContext</span>(<span style="color:#aa1111">"application_context.xml"</span>);
  4.        <span style="color:#000000">GirlfriendProxyServiceImpl</span> <span style="color:#000000">gfService</span> <span style="color:#981a1a">=</span> (<span style="color:#000000">GirlfriendProxyServiceImpl</span>) <span style="color:#000000">ac</span>.<span style="color:#000000">getBean</span>(<span style="color:#aa1111">"gfService"</span>);
  5.        <span style="color:#000000">Girlfriend</span> <span style="color:#000000">girlfriend</span> <span style="color:#981a1a">=</span> <span style="color:#000000">gfService</span>.<span style="color:#000000">getGirlfriend</span>();
  6.        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">girlfriend</span>);
  7.   }
  8. }</span></span>

运行结果:

 

如果将上述Spring的xml配置中的autowire属性值改成byType,表示根据类型自动装配,则会报如下错误

3.2 全局自动注入

通过 beans 标签中的default-autowire  属性配置自动注入。

有效范围:配置文件中的所有 bean 标签都生效。

四、依赖注入的数据类型

1 注入 Bean 对象

  1. <span style="background-color:#f8f8f8"><span style="color:#333333">    <span style="color:#117700"><</span><span style="color:#117700">bean</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"gfService"</span>
  2.          <span style="color:#0000cc">class</span>=<span style="color:#aa1111">""</span> <span style="color:#0000cc">autowire</span>=<span style="color:#aa1111">"byType"</span><span style="color:#117700">></span>
  3.        <span style="color:#117700"><</span><span style="color:#117700">property</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"girlfriend"</span> <span style="color:#117700">></span>
  4.            <span style="color:#117700"><</span><span style="color:#117700">ref</span> <span style="color:#0000cc">bean</span>=<span style="color:#aa1111">"gf6"</span><span style="color:#117700">/></span>
  5.        <span style="color:#117700"></</span><span style="color:#117700">property</span><span style="color:#117700">></span>
  6.    <span style="color:#117700"></</span><span style="color:#117700">bean</span><span style="color:#117700">></span></span></span>

2 注入基本数据类型和字符串

  1. <span style="background-color:#f8f8f8"><span style="color:#333333">   <span style="color:#117700"><</span><span style="color:#117700">bean</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"gf1"</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">""</span>
  2.          <span style="color:#0000cc">scope</span>=<span style="color:#aa1111">"prototype"</span> <span style="color:#0000cc">lazy-init</span>=<span style="color:#aa1111">"false"</span><span style="color:#117700">></span>
  3.        <span style="color:#117700"><</span><span style="color:#117700">property</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"education"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"本科"</span> <span style="color:#117700">/></span>
  4.        <span style="color:#117700"><</span><span style="color:#117700">property</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"hight"</span> <span style="color:#117700">></span>
  5.            <span style="color:#117700"><</span><span style="color:#117700">value</span><span style="color:#117700">></span>1.65<span style="color:#117700"></</span><span style="color:#117700">value</span><span style="color:#117700">></span>
  6.        <span style="color:#117700"></</span><span style="color:#117700">property</span><span style="color:#117700">></span>
  7.    <span style="color:#117700"></</span><span style="color:#117700">bean</span><span style="color:#117700">></span></span></span>

3 注入 List

3.1 POJO

  1. <span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">GirlfriendOYY</span> <span style="color:#770088">implements</span> <span style="color:#000000">Girlfriend</span> {
  2.    <span style="color:#770088">private</span> <span style="color:#008855">double</span> <span style="color:#000000">hight</span>;
  3.    <span style="color:#770088">private</span> <span style="color:#008855">String</span> <span style="color:#000000">education</span>;
  4.    <span style="color:#770088">private</span> <span style="color:#000000">List</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#000000">hobby</span>; <span style="color:#aa5500">// 兴趣爱好</span>
  5.    <span style="color:#770088">public</span> <span style="color:#000000">List</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#000000">getHobby</span>() {
  6.        <span style="color:#770088">return</span> <span style="color:#000000">hobby</span>;
  7.   }
  8.    <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#000000">setHobby</span>(<span style="color:#000000">List</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#000000">hobby</span>) {
  9.        <span style="color:#770088">this</span>.<span style="color:#000000">hobby</span> <span style="color:#981a1a">=</span> <span style="color:#000000">hobby</span>;
  10.   }    
  11.    <span style="color:#555555">@Override</span>
  12.    <span style="color:#770088">public</span> <span style="color:#008855">String</span> <span style="color:#000000">toString</span>() {
  13.        <span style="color:#770088">return</span> <span style="color:#aa1111">"GirlfriendOYY{"</span> <span style="color:#981a1a">+</span>
  14.                <span style="color:#aa1111">"hight="</span> <span style="color:#981a1a">+</span> <span style="color:#000000">hight</span> <span style="color:#981a1a">+</span>
  15.                <span style="color:#aa1111">", education='"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">education</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">'\''</span> <span style="color:#981a1a">+</span>
  16.                <span style="color:#aa1111">", hobby="</span> <span style="color:#981a1a">+</span> <span style="color:#000000">hobby</span> <span style="color:#981a1a">+</span>
  17.                <span style="color:#aa1111">'}'</span>;
  18.   }
  19. }</span></span>

3.2 Spring的xml配置

  1. <span style="background-color:#f8f8f8"><span style="color:#333333">   <span style="color:#117700"><</span><span style="color:#117700">bean</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"gf1"</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">""</span>
  2.          <span style="color:#0000cc">scope</span>=<span style="color:#aa1111">"prototype"</span> <span style="color:#0000cc">lazy-init</span>=<span style="color:#aa1111">"false"</span><span style="color:#117700">></span>
  3.        <span style="color:#117700"><</span><span style="color:#117700">property</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"education"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"本科"</span> <span style="color:#117700">/></span>
  4.        <span style="color:#117700"><</span><span style="color:#117700">property</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"hight"</span> <span style="color:#117700">></span>
  5.            <span style="color:#117700"><</span><span style="color:#117700">value</span><span style="color:#117700">></span>1.65<span style="color:#117700"></</span><span style="color:#117700">value</span><span style="color:#117700">></span>
  6.        <span style="color:#117700"></</span><span style="color:#117700">property</span><span style="color:#117700">></span>
  7.        <span style="color:#117700"><</span><span style="color:#117700">property</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"hobby"</span><span style="color:#117700">></span>
  8.            <span style="color:#117700"><</span><span style="color:#117700">list</span><span style="color:#117700">></span>
  9.                <span style="color:#117700"><</span><span style="color:#117700">value</span><span style="color:#117700">></span>看电影<span style="color:#117700"></</span><span style="color:#117700">value</span><span style="color:#117700">></span>
  10.                <span style="color:#117700"><</span><span style="color:#117700">value</span><span style="color:#117700">></span>旅游<span style="color:#117700"></</span><span style="color:#117700">value</span><span style="color:#117700">></span>
  11.            <span style="color:#117700"></</span><span style="color:#117700">list</span><span style="color:#117700">></span>
  12.        <span style="color:#117700"></</span><span style="color:#117700">property</span><span style="color:#117700">></span>
  13.    <span style="color:#117700"></</span><span style="color:#117700">bean</span><span style="color:#117700">></span></span></span>

3.3 测试

  1. <span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">TestGirlfriend</span> {
  2.    <span style="color:#770088">public</span> <span style="color:#770088">static</span> <span style="color:#008855">void</span> <span style="color:#000000">main</span>(<span style="color:#008855">String</span>[] <span style="color:#000000">args</span>) {
  3.        <span style="color:#000000">ApplicationContext</span> <span style="color:#000000">ac</span> <span style="color:#981a1a">=</span> <span style="color:#770088">new</span> <span style="color:#000000">ClassPathXmlApplicationContext</span>(<span style="color:#aa1111">"application_context.xml"</span>);
  4.        <span style="color:#000000">Girlfriend</span> <span style="color:#000000">gf</span> <span style="color:#981a1a">=</span> (<span style="color:#000000">Girlfriend</span>) <span style="color:#000000">ac</span>.<span style="color:#000000">getBean</span>(<span style="color:#aa1111">"gf1"</span>);
  5.        <span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">gf</span>);
  6.   }
  7. }</span></span>

运行结果:

4 注入 Set

同上。

5 注入 Map

5.1 POJO修改

  1. <span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#770088">public</span> <span style="color:#770088">class</span> <span style="color:#0000ff">GirlfriendOYY</span> <span style="color:#770088">implements</span> <span style="color:#000000">Girlfriend</span> {
  2.    <span style="color:#770088">private</span> <span style="color:#008855">double</span> <span style="color:#000000">hight</span>;
  3.    <span style="color:#770088">private</span> <span style="color:#008855">String</span> <span style="color:#000000">education</span>;
  4.    <span style="color:#770088">private</span> <span style="color:#000000">List</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#000000">hobby</span>;
  5.    <span style="color:#770088">private</span> <span style="color:#000000">Map</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span>, <span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#000000">parents</span>;    <span style="color:#aa5500">// 父母情况</span>
  6.    <span style="color:#770088">public</span> <span style="color:#000000">Map</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span>, <span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#000000">getParents</span>() {
  7.        <span style="color:#770088">return</span> <span style="color:#000000">parents</span>;
  8.   }
  9.    <span style="color:#770088">public</span> <span style="color:#008855">void</span> <span style="color:#000000">setParents</span>(<span style="color:#000000">Map</span><span style="color:#981a1a"><</span><span style="color:#008855">String</span>, <span style="color:#008855">String</span><span style="color:#981a1a">></span> <span style="color:#000000">parents</span>) {
  10.        <span style="color:#770088">this</span>.<span style="color:#000000">parents</span> <span style="color:#981a1a">=</span> <span style="color:#000000">parents</span>;
  11.   }
  12.    <span style="color:#555555">@Override</span>
  13.    <span style="color:#770088">public</span> <span style="color:#008855">String</span> <span style="color:#000000">toString</span>() {
  14.        <span style="color:#770088">return</span> <span style="color:#aa1111">"GirlfriendOYY{"</span> <span style="color:#981a1a">+</span>
  15.                <span style="color:#aa1111">"hight="</span> <span style="color:#981a1a">+</span> <span style="color:#000000">hight</span> <span style="color:#981a1a">+</span>
  16.                <span style="color:#aa1111">", education='"</span> <span style="color:#981a1a">+</span> <span style="color:#000000">education</span> <span style="color:#981a1a">+</span> <span style="color:#aa1111">'\''</span> <span style="color:#981a1a">+</span>
  17.                <span style="color:#aa1111">", hobby="</span> <span style="color:#981a1a">+</span> <span style="color:#000000">hobby</span> <span style="color:#981a1a">+</span>
  18.                <span style="color:#aa1111">", parents="</span> <span style="color:#981a1a">+</span> <span style="color:#000000">parents</span> <span style="color:#981a1a">+</span>
  19.                <span style="color:#aa1111">'}'</span>;
  20.   }
  21. }</span></span>

5.2 Spring的xml配置修改

  1. <span style="background-color:#f8f8f8"><span style="color:#333333">    <span style="color:#117700"><</span><span style="color:#117700">bean</span> <span style="color:#0000cc">id</span>=<span style="color:#aa1111">"gf1"</span> <span style="color:#0000cc">class</span>=<span style="color:#aa1111">""</span>
  2.          <span style="color:#0000cc">scope</span>=<span style="color:#aa1111">"prototype"</span> <span style="color:#0000cc">lazy-init</span>=<span style="color:#aa1111">"false"</span><span style="color:#117700">></span>
  3.        <span style="color:#117700"><</span><span style="color:#117700">property</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"education"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"本科"</span> <span style="color:#117700">/></span>
  4.        <span style="color:#117700"><</span><span style="color:#117700">property</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"hight"</span> <span style="color:#117700">></span>
  5.            <span style="color:#117700"><</span><span style="color:#117700">value</span><span style="color:#117700">></span>1.65<span style="color:#117700"></</span><span style="color:#117700">value</span><span style="color:#117700">></span>
  6.        <span style="color:#117700"></</span><span style="color:#117700">property</span><span style="color:#117700">></span>
  7.        <span style="color:#117700"><</span><span style="color:#117700">property</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"hobby"</span><span style="color:#117700">></span>
  8.            <span style="color:#117700"><</span><span style="color:#117700">list</span><span style="color:#117700">></span>
  9.                <span style="color:#117700"><</span><span style="color:#117700">value</span><span style="color:#117700">></span>看电影<span style="color:#117700"></</span><span style="color:#117700">value</span><span style="color:#117700">></span>
  10.                <span style="color:#117700"><</span><span style="color:#117700">value</span><span style="color:#117700">></span>旅游<span style="color:#117700"></</span><span style="color:#117700">value</span><span style="color:#117700">></span>
  11.            <span style="color:#117700"></</span><span style="color:#117700">list</span><span style="color:#117700">></span>
  12.        <span style="color:#117700"></</span><span style="color:#117700">property</span><span style="color:#117700">></span>
  13.        <span style="color:#117700"><</span><span style="color:#117700">property</span> <span style="color:#0000cc">name</span>=<span style="color:#aa1111">"parents"</span> <span style="color:#117700">></span>
  14.            <span style="color:#117700"><</span><span style="color:#117700">map</span><span style="color:#117700">></span>
  15.                <span style="color:#117700"><</span><span style="color:#117700">entry</span> <span style="color:#0000cc">key</span>=<span style="color:#aa1111">"father"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"zs"</span> <span style="color:#117700">/></span>
  16.                <span style="color:#117700"><</span><span style="color:#117700">entry</span> <span style="color:#0000cc">key</span>=<span style="color:#aa1111">"mother"</span> <span style="color:#0000cc">value</span>=<span style="color:#aa1111">"ls"</span> <span style="color:#117700">/></span>
  17.            <span style="color:#117700"></</span><span style="color:#117700">map</span><span style="color:#117700">></span>
  18.        <span style="color:#117700"></</span><span style="color:#117700">property</span><span style="color:#117700">></span>
  19.     <span style="color:#117700"></</span><span style="color:#117700">bean</span><span style="color:#117700">></span></span></span>

测试类不用修改,运行结果:

6 注入Properties

略。

总结