zoukankan      html  css  js  c++  java
  • Spring-06-依赖注入(DI)

    依赖注入(DI)

    1 构造器注入

    (前面已经说过了)

    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;
        }
    }
    
    1. 真实测试对象
    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;
        }
    
    1. 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.kuang.pojo.Student">
            <!--第一种,普通值注入,用value-->
            <property name="name" value="huba"/>
        </bean>
    </beans>
    
    1. 测试类
    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.getName());
    }
    

    完善注入信息:

    <?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="address" class="com.kuang.pojo.Address"/>
    
        <bean id="student" class="com.kuang.pojo.Student">
            <!--第一种,普通值注入,用value-->
            <property name="name" value="huba"/>
            <!--第二种 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="111111222222223333"/>
                    <entry key="银行卡" value="123123123123123"/>
                    
                </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">1601400105</prop>
                    <prop key="url">男</prop>
                    <prop key="username">root</prop>
                    <prop key="password">123456</prop>
                </props>
            </property>
        </bean>
    </beans>
    

    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">
    
        <!--p命名空间注入 p=properties-->
        <bean id="user" class="com.kuang.pojo.User" p:name="huba" p:age="18">
    
        </bean>
        <!--c命名空间注入 c=construct-args -->
        <bean id="user2" class="com.kuang.pojo.User" c:age="88" c:name="CodeHuba"/>
    
    </beans>
    

    测试:

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

    注意点:

    需要导入命名空间约束!

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

    4 Bean的作用域(Scope)

    在这里插入图片描述

    1. 单例模式(Spring默认机制)
    <bean id="user2" class="com.kuang.pojo.User" c:age="88" c:name="CodeHuba" scope="singleton"/>
    
    1. 原型模式:每次从容器中get的时候,都会产生一个新对象

    在这里插入图片描述

    <bean id="user2" class="com.kuang.pojo.User" c:age="88" c:name="CodeHuba" scope="prototype"/>
    
    1. 其余的request、session、application,这些个只能在web开发中使用!
  • 相关阅读:
    我不喜欢的 Rust 特性 (之一) eager drop
    为 Windows Phone 8.1 app 解决“The type does not support direct content.”的问题
    输入10个互不相同的数字并分成5对,问有多少种分法。
    code wars quiz: toInteger
    my first emacs custom key binding
    http协议消息报头学习笔记
    移动端经常遇到的小bug
    js小技巧
    ajax
    js正则表达
  • 原文地址:https://www.cnblogs.com/CodeHuba/p/13545663.html
Copyright © 2011-2022 走看看