【编程不良人】快速入门Spring学习笔记03---3种注入方式

本文详细介绍了Spring框架中的依赖注入,包括属性注入、数组和集合注入、引用类型注入,以及构造注入和自动注入的语法和实测案例。通过XML配置文件展示了如何设置和获取各种类型的Bean属性,强调了自动注入的两种方式——byName和byType及其适用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1 SET方式注入(重要)

配套视频:【编程不良人】快速入门Spring,SpringBoot、SpringCloud学不好完全是因为Spring没有掌握!_哔哩哔哩_bilibili

1.1 八种基本类型+String类型 +日期类型的注入

格式:name-value

  
  <property name="name" value="zhagnsan"/>
  <property name="age" value="21"/>
  <property name="id" value="100063"/>
  <property name="bir" value="2012/12/12"/>
  <property name="price" value="23.23"/>

1.2 数组类型注入

格式:array-value/ref bean

  
  <!--注入数组类型数据-->
    <property name="qqs">
       <array>
           <value>xxx</value>
           <value>qqq</value>
           <value>vvvv</value>
        </array>
    </property>

1.3 注入引用类型和集合类型

格式:list-value/ref bean、map-key value:value/bean ref、props-key value

  
  <!--注入引用类型和对象-->
  <property name="userDAO" ref="userDAO"/>
  <property name="lists">
    <list>
      <value>aaa</value>
      <value>bbb</value>
      <value>ccc</value>
    </list>
  </property>
  <property name="maps">
    <map>
      <entry key="aa" value="xiaohei"/>
      <entry key="bb" value="xiaoming"/>
      <entry key="cc" value="xiaosan"/>
    </map>
  </property>
  <property name="props">
    <props>
      <prop key="url">jdbc:mysql://localhost:3306/test</prop>
      <prop key="driver">com.mysql.jdbc.Driver</prop>
      <prop key="username">hr</prop>
      <prop key="password">hr</prop>
    </props>
  </property>

注意: 引用类型使用ref属性注入, 基本类型使用value属性注入

props是特殊的map

1.4 实测

a.spring.xml

  
  <?xml version="1.0" encoding="UTF-8"?>
  <beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
          <bean id="deptDAO" class="di.DeptDAOImpl"></bean>
  ​
          <bean id="deptService" class="di.DeptServiceImpl">
              <property name="deptDAO2" ref="deptDAO"/>
              <property name="name" value="Li Ming"/>
              <property name="bir" value="2022/03/31 20:17:58"/>
              <property name="array">
                  <array>
                      <value>小猪</value>
                      <value>小狗</value>
                      <value>小猫</value>
                  </array>
              </property>
              <property name="deptDAOS">
                  <array>
                      <ref bean="deptDAO"/>
                      <ref bean="deptDAO"/>
                      <ref bean="deptDAO"/>
                  </array>
              </property>
              <property name="hobbies">
                  <list>
                      <value>看书</value>
                      <value>打豆豆</value>
                      <value>睡觉</value>
                  </list>
              </property>
              <property name="daos">
                  <list>
                      <ref bean="deptDAO"/>
                      <ref bean="deptDAO"/>
                      <ref bean="deptDAO"/>
                  </list>
              </property>
              <property name="maps">
                  <map>
                      <entry key="aaa" value="123"/>
                      <entry key="bbb" value="456"/>
                      <entry key="ccc" value="789"/>
                  </map>
              </property>
              <property name="properties">
                  <props>
                      <prop key="111">qwe</prop>
                      <prop key="222">asd</prop>
                      <prop key="333">zxc</prop>
                  </props>
              </property>
          </bean>
  </beans>

b.DeptServiceImpl.java

  
  package di;
  ​
  import java.util.Date;
  import java.util.List;
  import java.util.Map;
  import java.util.Properties;
  ​
  public class DeptServiceImpl implements DeptService{
      private DeptDAOImpl deptDAO2;
      private String name;
      private Date bir;
      private String[] array;
      private DeptDAO[] deptDAOS;
      private List<String> hobbies;
      private List<DeptDAO> daos;
      private Map<String,String> maps;
      private Properties properties;
  ​
      public void setDeptDAO2(DeptDAOImpl deptDAO2) {
          this.deptDAO2 = deptDAO2;
      }
  ​
      public void setName(String name) {
          this.name = name;
      }
  ​
      public void setBir(Date bir) {
          this.bir = bir;
      }
  ​
      public void setArray(String[] array) {
          this.array = array;
      }
  ​
      public void setDeptDAOS(DeptDAO[] deptDAOS) {
          this.deptDAOS = deptDAOS;
      }
  ​
      public void setHobbies(List<String> hobbies) {
          this.hobbies = hobbies;
      }
  ​
      public void setDaos(List<DeptDAO> daos) {
          this.daos = daos;
      }
  ​
      public void setMaps(Map<String, String> maps) {
          this.maps = maps;
      }
  ​
      public void setProperties(Properties properties) {
          this.properties = properties;
      }
  ​
      @Override
      public void save(String name) {
          System.out.println("DeptServiceImpl: " + name);
          deptDAO2.save(name);
          System.out.println("Name: " + this.name);
          System.out.println("Bir: " + bir);
          for (String arr:array) {
              System.out.println(arr);
          }
          for (DeptDAO dao:deptDAOS) {
              System.out.println(dao);
          }
          hobbies.forEach(hobby-> System.out.println("hobby = " + hobby));
          daos.forEach(dao-> System.out.println("dao = " + dao));
          maps.forEach((key,value)-> System.out.println("key = " + key +",value = " + value));
          properties.forEach((key,value)-> System.out.println("key = " + key +",value = " + value));
      }
  }

c.测试

  
  package di;
  ​
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  ​
  public class TestSpring {
      public static void main(String[] args) {
          ApplicationContext context = new ClassPathXmlApplicationContext("di/spring.xml");
          DeptService deptService = (DeptService) context.getBean("deptService");
          deptService.save("2022年3月31日15:23:06");
      }
  }
  ​

2 构造注入(了解)

配套视频:【编程不良人】快速入门Spring,SpringBoot、SpringCloud学不好完全是因为Spring没有掌握!_哔哩哔哩_bilibili

2.1 定义

使用类中构造方法为类中成员变量进行赋值的过程

2.2 语法

a. 需要哪个组件属性就将谁声明为成员变量,并提供公开的构造方法

b.在配置文件中对应的组件标签内部使用<constructor-arg>标签进行注入(联想记忆:set注入时使用property标签)

2.3 实测

a.DeptDAOImpl

  package cdi;
  ​
  import java.util.Date;
  import java.util.List;
  ​
  public class DeptDAOImpl implements DeptDAO{
      private String name;
      private Integer age;
      private Date bir;
      private String[] qqs;
      private List<String> hobbies;
  ​
      public DeptDAOImpl() {
      }
  ​
      public DeptDAOImpl(String name, Integer age, Date bir, String[] qqs) {
          this.name = name;
          this.age = age;
          this.bir = bir;
          this.qqs = qqs;
      }
  ​
      public DeptDAOImpl(String name, Integer age, Date bir, String[] qqs, List<String> hobbies) {
          this.name = name;
          this.age = age;
          this.bir = bir;
          this.qqs = qqs;
          this.hobbies = hobbies;
      }
  ​
      @Override
      public void save(String name) {
          System.out.println("name = " + name);
          System.out.println("this.name = " + this.name);
          System.out.println("age = " + age);
          System.out.println("bir = " + bir);
          hobbies.forEach(hobby-> System.out.println("bobby = " + hobby));
          for (String qq : qqs) {
              System.out.println("qq = " + qq);
          }
      }
  }
  ​

b.spring.xml

  
  <?xml version="1.0" encoding="UTF-8"?>
  <beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
          <bean class="cdi.DeptDAOImpl" id="deptDAO">
                  <!--index从0开始,表示第一个成员变量注入参数,以此类推-->
                  <!--String-->
                  <constructor-arg index="0" name="name" value="小崔"/>
                  <!--Integer-->
                  <constructor-arg index="1" name="age" value="20"/>
                  <!--Date-->
                  <constructor-arg index="2" name="bir" value="2022/04/01"/>
                  <!--String[]-->
                  <constructor-arg index="3" name="qqs">
                          <array>
                                  <value>小猫</value>
                                  <value>小图</value>
                                  <value>小哈</value>
                          </array>
                  </constructor-arg>
                  <!--List<String>-->
                  <constructor-arg index="4" name="hobbies">
                          <list>
                                  <value>今天</value>
                                  <value>明天</value>
                                  <value>后天</value>
                          </list>
                  </constructor-arg>
          </bean>
  </beans>

c.测试

  
  package cdi;
  ​
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  ​
  public class TestSpring {
      public static void main(String[] args) {
          ApplicationContext context = new ClassPathXmlApplicationContext("cdi/spring.xml");
          DeptDAO deptDAO = (DeptDAO)context.getBean("deptDAO");
          deptDAO.save("笑笑");
      }
  }
  ​

注意:

1.构造注入并不常用,不过在一些框架类中必须使用构造注入,这里先了解其注入语法即可。

2.数组、List等语法与set注入相同

3 自动注入(了解)

3.1 定义

在spring工厂配置文件(spring.xml)中通过autowire属性完成自动注入的方式

注意:

a.自动注入底层使用的原理仍是set注入方式

b.自动注入使用时,需要在对应组件标签上开启才能使用

c.只能完成引用类型、对象类型、组件类型的注入

3.2 语法

a. 需要注入谁,就将谁声明为成员变量,并提供公开的set方法

b. 在对应的组件标签中加入autowire属性,并指定自动注入的方式

自动注入的方式主要使用byName和byType两种:

autowire: 用来给组件中成员变量完成自动赋值操作

a.autowire=”byName”

       根据注入的名称完成自动注入,将成员变量名与配置文件中bean的名称(id)进行匹配, 找到则注入, 找不到就报错。

b.autowire=”byType”

       根据注入的属性类型完成自动注入, 将成员变量类型与配置文件中的类型匹配,类型一致时完成注入,找不到不赋值,注意:如果工厂中存在多个类型一致的组件,会产生歧义,使用类型自动注入会报错。

总结: 无论使用以上那种方式注入都需要为属性提供公开的set方法

3.3 测试

a.DeptServiceImpl.java

  
  package adi;
  ​
  /**
   * 自动注入
   */
  public class DeptServiceImpl implements DeptService{
      private DeptDAO deptDAO;
  ​
      public void setDeptDAO(DeptDAO deptDAO) {
          this.deptDAO = deptDAO;
      }
  ​
      @Override
      public void save(String name) {
          System.out.println("DeptServiceImpl = " + name);
      }
  }
  ​

b.spring.xml

  
  <?xml version="1.0" encoding="UTF-8"?>
  <beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
          <bean class="adi.DeptDAOImpl" id="deptDAO"/>
          <bean class="adi.DeptDAONewImpl" id="deptDAONew"/>
          <bean class="adi.DeptServiceImpl" id="deptService" autowire="byName"/>
  </beans>

c.测试

  
  package adi;
  ​
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  ​
  public class TestSpring {
      public static void main(String[] args) {
          ApplicationContext context = new ClassPathXmlApplicationContext("adi/spring.xml");
          DeptService deptService = (DeptService)context.getBean("deptService");
          deptService.save("小崔");
      }
  }
  ​
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值