一、什么是依赖注入
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方法
<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> {
<span style="color:#770088">private</span> <span style="color:#008855">double</span> <span style="color:#000000">hight</span>;
<span style="color:#770088">private</span> <span style="color:#008855">String</span> <span style="color:#000000">education</span>;
<span style="color:#555555">@Override</span>
<span style="color:#770088">public</span> <span style="color:#008855">String</span> <span style="color:#000000">toString</span>() {
<span style="color:#770088">return</span> <span style="color:#aa1111">"GirlfriendOYY{"</span> <span style="color:#981a1a">+</span>
<span style="color:#aa1111">"hight="</span> <span style="color:#981a1a">+</span> <span style="color:#000000">hight</span> <span style="color:#981a1a">+</span>
<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>
<span style="color:#aa1111">'}'</span>;
}
<span style="color:#770088">public</span> <span style="color:#008855">double</span> <span style="color:#000000">getHight</span>() {
<span style="color:#770088">return</span> <span style="color:#000000">hight</span>;
}
<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>) {
<span style="color:#770088">this</span>.<span style="color:#000000">hight</span> <span style="color:#981a1a">=</span> <span style="color:#000000">hight</span>;
}
<span style="color:#770088">public</span> <span style="color:#008855">String</span> <span style="color:#000000">getEducation</span>() {
<span style="color:#770088">return</span> <span style="color:#000000">education</span>;
}
<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>) {
<span style="color:#770088">this</span>.<span style="color:#000000">education</span> <span style="color:#981a1a">=</span> <span style="color:#000000">education</span>;
}
}</span></span>
1.2 Spring的xml配置文件
<span style="background-color:#f8f8f8"><span style="color:#333333"> <span style="color:#aa5500"><!--1.1 通过构造方法创建对象--></span>
<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">"com.dyh.pojo.impl.GirlfriendOYY"</span>
<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>
<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>
<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>
<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>
<span style="color:#117700"></</span><span style="color:#117700">property</span><span style="color:#117700">></span>
<span style="color:#117700"></</span><span style="color:#117700">bean</span><span style="color:#117700">></span></span></span>
1.3 测试
<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> {
<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>) {
<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>);
<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>);
<span style="color:#000000">System</span>.<span style="color:#000000">out</span>.<span style="color:#000000">println</span>(<span style="color:#000000">gf</span>);
}
}</span></span>
运行结果
2 通过构造方法注入
Bean 对象中需要提供有参的构造方法
name:根据参数名称识别参数
index:根据参数的位置来识别参数
type:根据参数的类型识别参数
2.1 POJO中添加有参构造方法
<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> {
<span style="color:#770088">private</span> <span style="color:#008855">double</span> <span style="color:#000000">hight</span>;
<span style="color:#770088">private</span> <span style="color:#008855">String</span> <span style="color:#000000">education</span>;
<span style="color:#770088">public</span> <span style="color:#000000">GirlfriendOYY</span>(<span style="color:#008855&#