Spring-part06bean作用域

6、依赖注入

6.1、构造器注入

之前讲过

6.2、Set方式注入【重点】

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

【环境搭建】

  1. 复杂类型

    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 + '\'' +
                    '}';
        }
    }
    
  2. 真实测试对象

    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;
    }
    

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 http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--第一种普通值注入,value-->
    <bean id="student" class="com.shuang.pojo.Student">
        <property name="name" value="Double"/>
    </bean>
</beans>

4.测试类

 public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
        Student student=(Student) context.getBean("student");
        System.out.println(student.getName());
    }

5.完善注册信息

<?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.shuang.pojo.Address">
        <property name="address" value="济南"/>
    </bean>

    <bean id="student" class="com.shuang.pojo.Student">
        <!--普通值注入,value-->
        <property name="name" value="Double"/>

        <!--Bean注入,ref-->
        <property name="address" ref="address"/>

        <!--数组注入,array-value-->
        <property name="books" >
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>水浒传</value>
                <value>三国演义</value>
            </array>
        </property>

        <!--List数组,list-value-->
        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>打代码</value>
                <value>看电影</value>
            </list>
        </property>

        <!--Map注入,map-entry-->
        <property name="card">
            <map>
                <entry key="身份证" value="8945346794987"/>
                <entry key="银行卡" value="1231312312313321"/>
            </map>
        </property>

        <!--Set注入,set-value-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>COC</value>
                <value>BOB</value>
            </set>
        </property>

        <!--NULL,NULL-->
        <property name="wife">
            <null/>
        </property>

        <!--Properties,props-prop-->
        <property name="info">
            <props>
                <prop key="学号">20211119258</prop>
                <prop key="性别"></prop>
                <prop key="年级">2021</prop>
            </props>
        </property>

    </bean>


</beans>

6.3、拓展方式注入

我们可以使用p命名空间和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:p="http://www.springframework.org/schema/p"
       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">

    <!--p命名空间注入,可以直接注入属性的值:property-->
    <bean id="user" class="com.shuang.pojo.User" p:age="18" p:name="小明"></bean>

    <!--c命名空间注入,通过构造器注入:construct-args-->
    <bean id="user2" class="com.shuang.pojo.User" c:age="19" c:name="小红"></bean>
</beans>

测试:

 @Test
    public  void test2(){
        ApplicationContext context=new ClassPathXmlApplicationContext("userbeans.xml");
        User user=context.getBean("user2", User.class);
        System.out.println(user);

    }

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

       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"

6.4、bean作用域

在这里插入图片描述

单例模式

在这里插入图片描述

原型模式

在这里插入图片描述

1.单例模式(Spring默认机制)Singleton
只会创建一个对象

<bean id="user2" class="com.shuang.pojo.User" c:age="19" c:name="小红" scope="singleton"></bean>

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

<bean id="user2" class="com.shuang.pojo.User" c:age="19" c:name="小红" scope="prototype"></bean>
<!--创建多个对象-->

3.其余的request、session、application、这些只在web开发中用到

此文章来源于哔哩哔哩狂神说学习笔记

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值