• Spring配置



    学习视频来自于:秦疆(遇见狂神说)Bilibili地址
    他的自学网站:kuangstudy

    但行好事,莫问前程


    一、Spring配置

    1.1 别名

    alias 设置别名 , 为bean设置别名 , 可以设置多个别名

    <bean id="user" class="pers.tianyu.pojo.User">
        <constructor-arg name="name" value="HelloSpring"/>
    bean>
    
    <alias name="user" alias="userNew"/>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    1.2 Bean的配置

    
    
    
    <bean id="hello" name="hello2 h2,h3;h4" class="com.tianyu.pojo.Hello">
       <property name="name" value="Spring"/>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    1.3 import

    团队的合作通过import来实现。
    导入不同的Spring配置文件

    <import resource="{path}/beans.xml"/>
    
    • 1

    二、DI注入

    • 依赖注入(Dependency Injection,DI)。
    • 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源。
    • 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配

    2.1 构造器注入

    我们在之前的案例已经讲过了

    2.2 Set 注入 (重点)

    要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is。

    测试类

    Address.java

    public class Address {
        private String address;
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        @Override
        public String toString() {
            return "Address{" +
                    "address='" + address + '\'' +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    Student.java

    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;
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setAddress(Address address) {
            this.address = address;
        }
    
        public void setBooks(String[] books) {
            this.books = books;
        }
    
        public void setHobbys(List<String> hobbys) {
            this.hobbys = hobbys;
        }
    
        public void setCard(Map<String, String> card) {
            this.card = card;
        }
    
        public void setGames(Set<String> games) {
            this.games = games;
        }
    
        public void setWife(String wife) {
            this.wife = wife;
        }
    
        public void setInfo(Properties info) {
            this.info = info;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", address=" + address.toString() +
                    ", books=" + Arrays.toString(books) +
                    ", hobbys=" + hobbys +
                    ", card=" + card +
                    ", games=" + games +
                    ", wife='" + wife + '\'' +
                    ", info=" + info +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    1. 常量注入
    <bean id="student" class="pers.tianyu.pojo.Student">
            <property name="name" value="小强"/>
    bean>
    
    • 1
    • 2
    • 3
    1. Bean注入
    <bean id="student" class="pers.tianyu.pojo.Student">
            <property name="address" ref="address"/>
    bean>
    
    • 1
    • 2
    • 3
    1. 数组注入
    <bean id="student" class="pers.tianyu.pojo.Student">
            <property name="books">
                <array>
                    <value>西游记value>
                    <value>红楼梦value>
                    <value>水浒传value>
                array>
            property>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1. List注入
    <bean id="student" class="pers.tianyu.pojo.Student">
            <property name="hobbys">
                <list>
                    <value>看电影value>
                    <value>跑步value>
                    <value>爬山value>
                list>
            property>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1. Map注入
    <bean id="student" class="pers.tianyu.pojo.Student">
            <property name="card">
                <map>
                    <entry key="农业银行" value="1111-1111-1111-1111"/>
                    <entry key="建设银行" value="1111-1111-1111-1111"/>
                map>
            property>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. Set注入
    <bean id="student" class="pers.tianyu.pojo.Student">
            <property name="games">
                <set>
                    <value>LOLvalue>
                    <value>GTAvalue>
                    <value>OWvalue>
                set>
            property>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1. Null注入
    <bean id="student" class="pers.tianyu.pojo.Student">
            <property name="wife">
                <null>null>
            property>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. Properties注入
    <bean id="student" class="pers.tianyu.pojo.Student">
            <property name="info">
                <props>
                    <prop key="学号">20220315369821prop>
                    <prop key="性别">prop>
                    <prop key="姓名">安格列里奥芬尼尔prop>
                props>
            property>
    bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    测试结果

    Student{
            name='小强',
            address=Address{address='重庆'},
            books=[西游记, 红楼梦, 水浒传],
            hobbys=[看电影, 跑步, 爬山],
            card={农业银行=1111-1111-1111-1111,
                  建设银行=1111-1111-1111-1111},
            games=[LOL, GTA, OW],
            wife='null',
            info={学号=20220315369821,
                  性别=男,
                  姓名=安格列里奥芬尼尔
                  }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2.3 p命名和c命名注入

    User.java 【注意:这里没有有参构造器!】

    public class User {
        private String name;
        private int age;
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

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

    
    
    <bean class="pers.tianyu.pojo.User" p:name="tianyu" p:age="18"/>
    
    • 1
    • 2
    • 3

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

    
    
    <bean id="user" class="com.tianyu.pojo.User" c:name="tianyu" c:age="18"/>
    
    • 1
    • 2
    • 3

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

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

    2.4 Bean的作用域

    在Spring中,那些组成应用程序的主体及由Spring IoC容器所管理的对象,被称之为bean。简单地讲,bean就是由IoC容器初始化、装配及管理的对象。
    在这里插入图片描述
    几种作用域中,request、session作用域仅在基于web的应用中使用(不必关心你所采用的是什么web应用框架),只能用在基于web的Spring ApplicationContext环境。

    2.4.1 Singleton

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

    <bean id="ServiceImpl" class="pers.tianyu.service.ServiceImpl" scope="singleton">
    
    • 1

    2.4.2 Prototype

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

    <bean id="account" class="pers.tianyu.DefaultAccount" scope="prototype"/>  
      或者
    <bean id="account" class="pers.tianyu.DefaultAccount" singleton="false"/>
    
    • 1
    • 2
    • 3

    2.4.3 Request

    当一个bean的作用域为Request,表示在一次HTTP请求中,一个bean定义对应一个实例;即每个HTTP请求都会有各自的bean实例,它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext情形下有效。考虑下面bean定义:

    <bean id="loginAction" class="pers.tianyu.foo.LoginAction" scope="request"/>
    
    • 1

    针对每次HTTP请求,Spring容器会根据loginAction bean的定义创建一个全新的LoginAction bean实例,且该loginAction bean实例仅在当前HTTP request内有效,因此可以根据需要放心的更改所建实例的内部状态,而其他请求中根据loginAction bean定义创建的实例,将不会看到这些特定于某个请求的状态变化。当处理请求结束,request作用域的bean实例将被销毁。

    2.4.4 Session

    当一个bean的作用域为Session,表示在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。考虑下面bean定义:

    <bean id="userPreferences" class="pers.tianyu.foo.userPreferences" scope="session">
    
    • 1

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

  • 相关阅读:
    2018年五一杯数学建模C题江苏省本科教育质量综合评价解题全过程文档及程序
    Ubuntu18.04 velodyne 运行loam_velodyne
    【广州华锐互动】奶牛养殖难产助产3D沉浸式教学平台
    Swin-Transformer 从数据尺度变换角度解析
    【javaScript面向对象-模块化-面相关对象编程——行走的方块案例】
    数据结构-链表(java)
    Redis运行为什么快
    鸿蒙Next-Scroll滚动
    biggan:large scale gan training for high fidelity natural image synthesis
    JVM入个门(1)
  • 原文地址:https://blog.csdn.net/zhao854116434/article/details/126150135