Spring03-依赖注入(DI)

依赖注入

概念
  • 依赖注入(Dependency Injection,DI)。

  • 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .

  • 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .

构造器注入

前面已经介绍过,参考4、IOC创建对象的方式

Set方式注入【重点】
  • 依赖注入:Set注入
    • 依赖:bean对象的创建依赖于容器!
    • 注入:bean对象中的所有属性,由容器来注入!

【环境搭建】

  1. 复杂类型

    public class Address {
        private String address;
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    }
    
  2. 真实测试对象

    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 wife;
        private Properties info;
    }
    
  3. beans.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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="student" class="com.study.pojo.Student">
            <!--第一种:普通值注入,value        -->
            <property name="name" value="黑心白莲"/>
        </bean>
    </beans>
    
  4. 测试类

    public class MyTest {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    
            Student student = (Student) context.getBean("student");
            System.out.println(student.getName());
        }
    }
    
  5. 完善注入信息

    <bean id="address" class="com.study.pojo.Address">
        <property name="address" value="西安"/>
    </bean>
    
    <bean id="student" class="com.study.pojo.Student">
        <!--第一种:普通值注入,value        -->
        <property name="name" value="黑心白莲"/>
    
        <!--第二种:        -->
        <property name="address" ref="address"/>
    
        <!--数组        -->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>水浒传</value>
                <value>三国演义</value>
            </array>
        </property>
    
        <!--List        -->
        <property name="hobbies">
            <list>
                <value>打篮球</value>
                <value>看电影</value>
                <value>敲代码</value>
            </list>
        </property>
    
        <!--Map        -->
        <property name="card">
            <map>
                <entry key="身份证" value="123456789987456321"/>
                <entry key="银行卡" value="359419496419481649"/>
            </map>
        </property>
    
        <!--Set        -->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>COC</value>
                <value>BOB</value>
            </set>
        </property>
    
        <!--NULL        -->
        <property name="wife">
            <null/>
        </property>
    
        <!--Properties        -->
        <property name="info">
            <props>
                <prop key="driver">20191029</prop>
                <prop key="url">102.0913.524.4585</prop>
                <prop key="user">黑心白莲</prop>
                <prop key="password">123456</prop>
            </props>
        </property>
    
    </bean>
    

P命名空间注入 : 需要在头文件中加入约束文件

导入约束 : xmlns:p="http://www.springframework.org/schema/p"
 
 <!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
 <bean id="user" class="com.study.pojo.User" p:name="狂神" p:age="18"/>

c 命名空间注入 : 需要在头文件中加入约束文件

导入约束 : xmlns:c="http://www.springframework.org/schema/c"
 <!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
 <bean id="user" class="com.study.pojo.User" c:name="狂神" c:age="18"/>

发现问题:爆红了,刚才我们没有写有参构造!

解决:把有参构造器加上,这里也能知道,c 就是所谓的构造器注入!

测试:

@Test
public void test2(){
    ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");

    User user = context.getBean("user",User.class);
    System.out.println(user);

    User user2 = context.getBean("user2",User.class);
    System.out.println(user2);
}

注意点:p命名和c命名空间不能直接使用,需要导入xml约束!

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
bean的作用域

在这里插入图片描述

几种作用域中,request、session作用域仅在基于web的应用中使用(不必关心你所采用的是什么web应用 框架),只能用在基于web的Spring ApplicationContext环境。

  1. 单例模式(Spring默认机制)

    <bean id="user2" class="com.study.pojo.User" c:name="狂神" c:age="22" scope="singleton"/>
    
  2. 原型模式:每次从容器中get的时候,都会产生一个新对象!

    <bean id="user2" class="com.study.pojo.User" c:name="狂神" c:age="22" scope="prototype"/>
    

.study.pojo.User" c:name=“狂神” c:age=“22” scope=“singleton”/>


2. 原型模式:每次从容器中get的时候,都会产生一个新对象!

```xml
<bean id="user2" class="com.study.pojo.User" c:name="狂神" c:age="22" scope="prototype"/>
  1. 其余的request、session、application、这些只能在web开发中使用到!

相关推荐

  1. SpringDI 依赖注入

    2024-06-10 01:38:01       19 阅读
  2. 3.Spring-依赖注入DI

    2024-06-10 01:38:01       14 阅读
  3. Spring的IoC(控制反转)和 DI依赖注入

    2024-06-10 01:38:01       19 阅读
  4. C# DI依赖注入

    2024-06-10 01:38:01       37 阅读
  5. Spring 依赖注入

    2024-06-10 01:38:01       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-10 01:38:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-10 01:38:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-10 01:38:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-10 01:38:01       18 阅读

热门阅读

  1. Hadoop集群安装

    2024-06-10 01:38:01       9 阅读
  2. 1731. 每位经理的下属员工数量

    2024-06-10 01:38:01       9 阅读
  3. btstack协议栈实战篇--GAP LE Advertisements Scanner

    2024-06-10 01:38:01       11 阅读
  4. WooYun-2016-199433 -phpmyadmin-反序列化RCE-getshell

    2024-06-10 01:38:01       10 阅读
  5. Vue2事件处理

    2024-06-10 01:38:01       11 阅读
  6. JVM内存分析之JVM分区与介绍

    2024-06-10 01:38:01       8 阅读
  7. 重写mybatisPlus自定义ID生成策略

    2024-06-10 01:38:01       11 阅读