美文网首页
6 DI 依赖注入

6 DI 依赖注入

作者: Messix_1102 | 来源:发表于2022-04-11 13:35 被阅读0次

    6.1 构造器注入

    前面已经讲过

    6.2 Set方式注入【重点】

    • 依赖注入:Set注入!
      依赖:bean对象的创建依赖于容器!
      注入:bean 对象中的所以属性,有容器来注入!
      【环境搭建】
    1. 复杂类型
    package com.hunter.pojo;
    
    public class Address {
        private String address;
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    }
    
    1. 真实测试对象
    package com.hunter.pojo;
    
    import java.util.*;
    
    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 Properties info;
        private String wife;
        ... Get Set ToString
    }
    
    
    1. beans
    <?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">
    
        <!-- more bean definitions go here -->
        <bean id="address" class="com.hunter.pojo.Address">
            <property name="address" value="xian"></property>
        </bean>
    
        <bean id="student" class="com.hunter.pojo.Student">
            <!-- 第一种,普通值注入, value-->
            <property name="name" value="hunter"></property>
            <!-- 第二种,bean注入, ref-->
            <property name="address" ref="address"></property>
            <!-- 第三种,数组注入-->
            <property name="books">
                <array>
                    <value>red building dream</value>
                    <value>west gogogo</value>
                    <value>water tiger roll</value>
                    <value>three kingdoms</value>
                </array>
            </property>
            <!-- 第四种,List注入-->
            <property name="hobbys">
                <list>
                     <value>watch movie</value>
                     <value>listen music</value>
                </list>
            </property>
            <!--第五种,Map注入-->
            <property name="card">
                <map>
                     <entry key="IDCard" value="658745545645" />
                     <entry key="BankCard" value="2545861235" />
                </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="Num">201110437</prop>
                    <prop key="Gender">male</prop>
                    <prop key="Url">http://www.baidu.com</prop>
                    <prop key="un">root</prop>
                    <prop key="pwd">123456</prop>
                </props>
            </property>
        </bean>
    
    </beans>
    
    1. 测试类
    import com.hunter.pojo.Student;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MyTest {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
            Student student = context.getBean("student", Student.class);
            System.out.println(student.toString());
            /*
            Student{
                name='hunter',
                address=Address{address='xian'},
                books=[
                    red building dream,
                    west gogogo,
                    water tiger roll,
                    three kingdoms],
                hobbys=[
                    watch movie,
                    listen music],
                card={
                    IDCard=658745545645,
                    BankCard=2545861235
                    },
                games=[lol, coc, bob],
                info={
                    un=root,
                    Gender=male,
                    pwd=123456,
                    Num=201110437,
                    Url=http://www.baidu.com
                    },
                wife='null'}
            * */
        }
    }
    
    

    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
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!-- more bean definitions go here -->
        <!-- P 命名空间注入, 可以直接注入属性的值-->
        <bean id="user" class="com.hunter.pojo.User" p:name="hunter" p:age="18"></bean>
        <!-- c 命名空间注入, 构造器注入-->
        <bean id="user2" class="com.hunter.pojo.User" c:name="hunter2" c:age="19"></bean>
    </beans>
    
    • 测试
    public class MyTest {
        @Test
        public void test2(){
            ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
            User user = context.getBean("user2", User.class);
            System.out.println(user.toString());
        }
    }
    

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

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

    6.4 bean 的作用域

    截图
    • singleton 单例全局共享一个,为bean scope的默认值
    <bean id="user2" class="com.hunter.pojo.User" c:name="hunter2" c:age="19" scope="singleton"></bean>
    
    • 原型模式(prototype) 每次从容器中get的时候都会产生一个新对象
    <bean id="user2" class="com.hunter.pojo.User" c:name="hunter2" c:age="19" scope="prototype"></bean>
    
    • 其余的request、session、application只能在web开发中使用!

    相关文章

      网友评论

          本文标题:6 DI 依赖注入

          本文链接:https://www.haomeiwen.com/subject/gduaqrtx.html