DI的三种依赖注入方式和底层实现

依赖注入(DI) 有三种注入编写方法:
第一种:设值注入
    底层实现  setter方法
    name与对应类在中属性的set方法名一致
    使用无参构造创建对象 并赋值
    -->
   <bean id="Students" class="com.gxy.entity.Student">
    <property nameStudents="stu_id" value="111"/>
    <property name="stu_name" value="张三"/>
    <property name="stu_age"  value="18"/>
    <property name="stu_gender" value="男"/>
    <property name="tea_id"  value="1"/>
    <property name="teacher" ref="teacher/>
  </bean>

第二种:构造注入
 <!--
       
        底层实现  构造方法注入
        使用有参构造创建对象 并赋值  
        -->
    <bean id="teacher" class="com.gxy.entity.Teacher">
        <constructor-arg index="0" value="小苗"/>
        <constructor-arg index="1" value="18"/>
    </bean>
      <bean id="Students" class="com.gxy.entity.Student">
          <constructor-arg name="stu_id" value="111"/>
          <constructor-arg name="stu_name" value="张三"/>
          <constructor-arg  name="stu_age" value="18"/>
          <constructor-arg name="stu_gender" value="男"/>
          <constructor-arg  name="tea_id" value="11"/>
          <constructor-arg name="teacher" ref="teacher"/>
      </bean>


第三种:自动装配:分两种ByName和ByType
ByName自动装配
autowire=“byName”

运行过程:先去实体类中找对应的

 <!--
    byName:会自动在容器上下文中查找  和自己对象的set方法后面 的值相同

    -->
    <bean id="People" class="com.test.entity.People" autowire="byName">
        <property name="name" value="张三"/>
    </bean>

ByType自动装配
autowire=“byType”

 byType:会自动在容器上下文中查找  和自己对象类型相同  并且用的时候是唯一
    -->
    <bean id="People" class="com.test.entity.People" autowire="byType">
        <property name="name" value="张三"/>
    </bean>

1. 有参构造器注入
        这种方式的前提是,在bean所对应的类中显式定义有参构造函数,涉及到的属性才能够使用<constructor-arg/>标签进行赋值。具体参照链接Spring IOC(控制反转)_鲸鱼-D的博客-CSDN博客中IOC创建对象方式中的方式2。

2.Set方式注入(重点)
        前提: 类中有无参构造函数(默认or显式定义)以及setter方法。

Student类:类中的成员属性包括各种数据类型的变量
public class Student {
 
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbies;
    private Map<String, String> card;
    private Set<String> games;
    private String wife1;
    private String wife2;
    private Properties info;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Address getAddress() {
        return address;
    }
 
    public void setAddress(Address address) {
        this.address = address;
    }
 
    public String[] getBooks() {
        return books;
    }
 
    public void setBooks(String[] books) {
        this.books = books;
    }
 
    public List<String> getHobbies() {
        return hobbies;
    }
 
    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }
 
    public Map<String, String> getCard() {
        return card;
    }
 
    public void setCard(Map<String, String> card) {
        this.card = card;
    }
 
    public Set<String> getGames() {
        return games;
    }
 
    public void setGames(Set<String> games) {
        this.games = games;
    }
 
    public String getWife1() {
        return wife1;
    }
 
    public void setWife1(String wife1) {
        this.wife1 = wife1;
    }
 
    public String getWife2() {
        return wife2;
    }
 
    public void setWife2(String wife2) {
        this.wife2 = wife2;
    }
 
    public Properties getInfo() {
        return info;
    }
 
    public void setInfo(Properties info) {
        this.info = info;
    }
 
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address.toString() +
                ", books=" + Arrays.toString(books) +
                ", hobbies=" + hobbies +
                ", card=" + card +
                ", games=" + games +
                ", wife1='" + wife1 + '\'' +
                ", wife2='" + wife2 + '\'' +
                ", info=" + info +
                '}';
    }
}

配置文件
<?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="address" class="com.jing.pojo.Address">
        <property name="address" value="Beijing"/>
    </bean>
 
    <bean id="student" class="com.jing.pojo.Student">
        <!--基本数据类型herString注入-->
        <property name="name" value="YuJing"/>
        <!--引用类型注入-->
        <property name="address" ref="address"/>
        <!--数组类型注入-->
        <property name="books">
            <array value-type="java.lang.String">
                <value>红楼梦</value>
                <value>西游记</value>
                <value>水浒传</value>
                <value>三国演义</value>
            </array>
        </property>
        <!--单列集合List注入-->
        <property name="hobbies">
            <list value-type="java.lang.String">
                <value>看综艺</value>
                <value>敲代码</value>
                <value>听歌</value>
            </list>
        </property>
        <!--双列结合Map注入-->
        <property name="card">
            <map key-type="java.lang.String" value-type="java.lang.String">
                <entry key="YuJing" value="123456"/>
                <entry key="JingYu" value="654321"/>
            </map>
        </property>
        <!--单列结合Set-->
        <property name="games">
            <set value-type="java.lang.String">
                <value>LOL</value>
                <value>COC</value>
                <value>BOB</value>
            </set>
        </property>
        <!--空值null-->
        <property name="wife1">
            <null/>
        </property>
        <!--空字符串注入-->
        <property name="wife2" value=""/>
        <!--Properties注入-->
        <property name="info">
            <props>
                <prop key="driver">com.mysql.jdbc.driver</prop>
                <prop key="url">http://mysql</prop>
                <prop key="username">root</prop>
                <prop key="password">root</prop>
            </props>
        </property>
    </bean>
 
</beans>

3. 扩展方式注入 
        为了简化上述两种配置方式,Spring为我们提供了与上述两种方式分别对应的扩展方式:

c命名空间:简化方式1,除了方式1中的前提以外,还需要在配置文件中加入约束xmlns:c="http://www.springframework.org/schema/c"
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    
       xmlns:c="http://www.springframework.org/schema/c"
 
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <!--C命名空间注入,可以通过构造器注入:construct-args-->
    <bean id="student1" class="com.jing.pojo.Student" c:name="郭靖" c:wife="黄蓉"></bean>
 
</beans>
p命名空间:简化方式2,除了方式2中的前提以外,还需要再配置文件中加入约束xmlns:p="http://www.springframework.org/schema/p"
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    
       xmlns:p="http://www.springframework.org/schema/p"
 
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
 
    <!--P命名空间注入,可以通过构造器注入:property-->
    <bean id="student2" class="com.jing.pojo.Student" p:name="郭靖" p:wife="黄蓉"></bean>
 
</beans>

小结:

ByName:byname的时候要保证bean的id唯一,并且这个bean需要和自动注入的属性的set方法中的值一致。

ByType:bytype的时候要保证所有bean的class唯一,并且这个bean需要和自动注入的类型一致。
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值