• 初学Java ssh之Spring 第三篇


      在这篇中,我学习了依赖注入的两种方式:设值注入和构造注入。

      在我们以前的思维中,如果调用一个类时,我们都需要将其手动实例化,当我们创建被调用的工作不需要我们完成时,这就是控制反转,当这个将被调用的实例过程被Spring完成,并注入调用者时,这就是依赖注入。

      一、先来学习学习设值注入:

      我们先来新建两个接口规范,分别命名为Computer.java 和 Key.java,其路径均在com.sep.basic.service包下:

      Computer.java代码如下:

    1 package com.sep.basic.service;
    2 
    3 public interface Computer {
    4     public void useKey();
    5 }

      Key.java代码如下:

    1 package com.sep.basic.service;
    2 
    3 public interface Key {
    4     //一个方法
    5     public String print();
    6 }

      接下来写两个实现类,分别命名为Lenovo.java和LogiTech.java,分别实现Computer和Key接口,路径放在com.sep.basic.service.impl包下。

      Lenovo.java代码如下:

     1 package com.sep.basic.service.impl;
     2 
     3 import com.sep.basic.service.Computer;
     4 import com.sep.basic.service.Key;
     5 
     6 public class Lenovo implements Computer{
     7     private Key key;
     8     
     9     public Key getKey() {
    10         return key;
    11     }
    12     
    13     //设值注入所需的setter方法
    14     public void setKey(Key key) {
    15         this.key = key;
    16     }
    17     
    18     //实现Computer接口中使用键盘的属性
    19     public void useKey() {
    20         System.out.println(key.print());
    21     }
    22 
    23 }

      LogicTech.java的代码如下:

     1 package com.sep.basic.service.impl;
     2 
     3 import com.sep.basic.service.Key;
     4 
     5 public class LogiTech implements Key{
     6     //实现Key接口中打印方法
     7     public String print() {
     8         return "用键盘打字";
     9     }
    10 
    11 }

      接下来我们配置applicationContext.xml文件,在其中加入如下代码:

    1     <!-- 将配置Lenovo实例 -->
    2     <bean id="lenovo" class="com.sep.basic.service.impl.Lenovo">
    3         <!-- 将logicTech实例注入到key属性 -->
    4         <property name="key" ref="logicTech"></property>
    5     </bean>
    6     <!-- 将配置LogicTech实例 -->
    7     <bean id="logicTech" class="com.sep.basic.service.impl.LogicTech"></bean>

      ok,接下来回到我们之前测试用的主方法,SpringTest中,在其中加入如下代码:

    //通过Spring来完成注入
    Lenovo lenovo = ctx.getBean("lenovo", Lenovo.class);
    lenovo.useKey();

      现在运行,可以看到控制台输出“用键盘打字”。

      在applicationContext.xml中<bean>标签中的id为bean的唯一标识,class为对应的需要实例化的类的地址。

      通过写在SpringTest中代码可以看出我们调用的lenovo类,我们并没有手动实例化,而且其中调用的key,我们也没有做过多余的操作,所有的工作都是spring帮我们完成的,它通过配置文件,不仅仅创建了我们的Lenovo实例,而且将其中设值注入的key实例也帮我们建好了。

      假如我们到时候需要改变key的值,我们只需要写好对应的key的实现类,在配置文件中将

    1 <!-- 将配置LogicTech实例 -->
    2  <bean id="logicTech" class="com.sep.basic.service.impl.LogicTech"></bean>

      改为

    <!-- 将配置LogicTech实例 -->
    7     <bean id="logicTech" class="对应的实现类全名称"></bean>

    即可。

      二、我现在开始学习构造注入:

      简单从字面意思上来看,构造注入是利用构造器来完成设置依赖关系的方式。

      首先我们需要对之前的Lenovo.class进行修改,修改后的代码如下:

     1 package com.sep.basic.service.impl;
     2 
     3 import com.sep.basic.service.Computer;
     4 import com.sep.basic.service.Key;
     5 
     6 public class Lenovo implements Computer{
     7     private Key key;
     8     
     9     //默认构造器
    10     public Lenovo(){};
    11     
    12     //构造注入带参数的构造器
    13     public Lenovo(Key key) {
    14         this.key = key;
    15     }
    16 
    17     //设值注入所需的setter方法
    18     public void setKey(Key key) {
    19         this.key = key;
    20     }
    21     
    22     //实现Computer接口中使用键盘的属性
    23     public void useKey() {
    24         System.out.println(key.print());
    25     }
    26 
    27 }

      同样,配置文件需要做出对应的修改,构造注入需要使用<constructor-arg```>标签来完成,修改后代码如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans
     3     xmlns="http://www.springframework.org/schema/beans"
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     5     xmlns:p="http://www.springframework.org/schema/p"
     6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
     7     <!-- 将配置Lenovo实例 -->
     8     <bean id="lenovo" class="com.sep.basic.service.impl.Lenovo">
     9         <constructor-arg ref="logicTech"></constructor-arg>
    10     </bean>
    11     <!-- 将配置LogicTech实例 -->
    12     <bean id="logicTech" class="com.sep.basic.service.impl.LogicTech"></bean>
    13 </beans>

      运行,同样可以看到语句输出:“用键盘打字”。

      ok,两种方法的依赖注入我就都实现了,需要消化的东西很多啊,我还得自己琢磨琢磨,话说打这么多字很累啊,要不是最近工作不怎么忙,我估计得打字打到吐血啊!

     

  • 相关阅读:
    第01组 Alpha冲刺(3/4)
    第01组 Alpha冲刺(2/4)
    第01组 Alpha冲刺(1/4)
    [2019BUAA软件工程]个人期末总结感想
    [2019BUAA软件工程]结对编程感想
    [2019BUAA软件工程]结对作业
    [2019BUAA软件工程]第1次阅读作业
    [2019BUAA软件工程]第0次个人作业
    [2019BUAA软工]第0次代码作业
    OO学习体会与阶段总结(测试与论证)
  • 原文地址:https://www.cnblogs.com/speedwade/p/3974483.html
Copyright © 2020-2023  润新知