4、依赖注入

1构造器注入
前面已经讲过
2Set注入重点
依赖注入本质:Set注入
依赖:bean对象的创建依赖于容器(beans)
注入:bean对象中的所有属性,由容器来注入
环境搭建

@Data
public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String, String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

}
@Data
public class Address {
    private String address;
}

配置文件

<?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就是java对象 , 由Spring创建和管理
    id="hello" 是要创建的对象的变量名字
    class="cn.zhy.pojo.Hello" 是要创建的对象
    <property name="name" value="Spring"/>
    name="name"对应Hello类中的属性名
    value="Spring"  是给对应的属性设置值,是通过set方法设置的
    -->
    <bean id="address" class="cn.zhy.pojo.Address">
        <property name="address" value="中国"/>
    </bean>
    <bean id="student" class="cn.zhy.pojo.Student">
        <!--第一种  普通类型value直接注入-->
        <property name="name" value="张三"/>
        <!--第二种,Bean注入,ref -->
        <property name="address" ref="address"/>
        <!--第三种,数组注入 -->
        <property name="books">
            <array>
                <value>西游记</value>
                <value>红楼梦</value>
                <value>三国</value>
                <value>水浒</value>
            </array>
        </property>
        <!--第三种,List注入 -->
        <property name="hobbys">
            <list>
                <value>玩游戏</value>
                <value>听歌</value>
                <value>学习</value>
            </list>
        </property>
        <!--第四种,Map注入 -->
        <property name="card">
            <map>
                <entry key="学生证" value="1312431"/>
                <entry key="银行卡" value="4122133"/>
            </map>
        </property>
        <!--第四种,Set注入 -->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>原神</value>
                <value>王者</value>
            </set>
        </property>
        <!--第五种,null值注入 -->
        <property name="wife">
            <null/>
        </property>
        <!--第六种,Properties注入 -->
        <property name="info">
            <props>
                <prop key="学号">123131</prop>
                <prop key="性别"></prop>
            </props>
        </property>
    </bean>
</beans>

测试

public class MyTest01 {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student);
        //Student(name=张三, address=Address(address=中国),
        // books=[西游记, 红楼梦, 三国, 水浒],
        // hobbys=[玩游戏, 听歌, 学习],
        // card={学生证=1312431, 银行卡=4122133},
        // games=[LOL, 原神, 王者],
        // wife=null,
        // info={学号=123131, 性别=男})
    }
}

6、3拓展方式注入(非重点)
6、4Bean的作用域主要Singleton(单例),Prototype(原型)
在这里插入图片描述

Singleton
当一个bean的作用域为Singleton,那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。Singleton是单例类型,就是在创建起容器时就同时自动创建了一个bean的对象,不管你是否使用,他都存在了,每次获取到的对象都是同一个对象。注意,Singleton作用域是Spring中的缺省作用域。要在XML中将bean定义成singleton,可以这样配置:

 <bean id="ServiceImpl" class="cn.csdn.service.ServiceImpl" scope="singleton">

Prototype
当一个bean的作用域为Prototype,表示一个bean定义对应多个对象实例。Prototype作用域的bean会导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)时都会创建一个新的bean实例。Prototype是原型类型,它在我们创建容器的时候并没有实例化,而是当我们获取bean的时候才会去创建一个对象,而且我们每次获取到的对象都不是同一个对象。根据经验,对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。在XML中将bean定义成prototype,可以这样配置:

 <bean id="account" class="com.foo.DefaultAccount" scope="prototype"/>

Request
当一个bean的作用域为Request,表示在一次HTTP请求中,一个bean定义对应一个实例;即每个HTTP请求都会有各自的bean实例,它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext情形下有效。考虑下面bean定义:
<bean id=“loginAction” class=cn.csdn.LoginAction" scope=“request”/>
Session
当一个bean的作用域为Session,表示在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。考虑下面bean定义:

 <bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>

针对某个HTTP Session,Spring容器会根据userPreferences bean定义创建一个全新的userPreferences bean实例,且该userPreferences bean仅在当前HTTP Session内有效。与request作用域一样,可以根据需要放心的更改所创建实例的内部状态,而别的HTTP Session中根据userPreferences创建的实例,将不会看到这些特定于某个HTTP Session的状态变化。当HTTP Session最终被废弃的时候,在该HTTP Session作用域内的bean也会被废弃掉。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值