• 【Spring】学习笔记003依赖注入


    1.构造器注入

      构造器方式注入在之前的学习笔记中已经记录:https://www.cnblogs.com/AirCL/p/14350568.html

    2.Set方式注入

      依赖注入:Set注入

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

      环境搭建:假设我们有实体类Address和Student如下:

    public class Address implements Serializable {
    
        private String address;
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    }
    public class Student implements Serializable {
    
        private String name;
        private Address address;
        private String[] books;
        private List<String> hobby;
        private Map<String,String> card;
        private Set<String> games;
        private String wife;
        private Properties info;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Address getAddress() {
            return address;
        }
    
        public void setAddress(Address address) {
            this.address = address;
        }
    
        public String[] getBooks() {
            return books;
        }
    
        public void setBooks(String[] books) {
            this.books = books;
        }
    
        public List<String> getHobby() {
            return hobby;
        }
    
        public void setHobby(List<String> hobby) {
            this.hobby = hobby;
        }
    
        public Map<String, String> getCard() {
            return card;
        }
    
        public void setCard(Map<String, String> card) {
            this.card = card;
        }
    
        public Set<String> getGames() {
            return games;
        }
    
        public void setGames(Set<String> games) {
            this.games = games;
        }
    
        public String getWife() {
            return wife;
        }
    
        public void setWife(String wife) {
            this.wife = wife;
        }
    
        public Properties getInfo() {
            return info;
        }
    
        public void setInfo(Properties info) {
            this.info = info;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", address=" + address +
                    ", books=" + Arrays.toString(books) +
                    ", hobby=" + hobby +
                    ", card=" + card +
                    ", games=" + games +
                    ", wife='" + wife + '\'' +
                    ", info=" + info +
                    '}';
        }
    }

      下面看一下各种类型的Set注入

        <bean id="addressBean" class="com.aircl.domain.Address">
            <property name="address" value="This is my address!"></property>
        </bean>
    
        <bean id="studentBean" class="com.aircl.domain.Student">
            <!--第一种:普通值注入-->
            <property name="name" value="aircl"></property>
            <!--第二种:bean注入-->
            <property name="address" ref="addressBean"></property>
            <!--第三种:数组注入-->
            <property name="books">
                <array>
                    <value>book1</value>
                    <value>book2</value>
                    <value>book3</value>
                    <value>book4</value>
                </array>
            </property>
            <!--第四种:List注入-->
            <property name="hobby">
                <list>
                    <value>hobby1</value>
                    <value>hobby2</value>
                    <value>hobby3</value>
                    <value>hobby4</value>
                </list>
            </property>
            <!--第五种:Map注入-->
            <property name="card">
                <map>
                    <entry key="idCard" value="123456"/>
                    <entry key="studentCard" value="12138"/>
                </map>
            </property>
            <!--第六种:Set注入-->
            <property name="games">
                <set>
                    <value>set1</value>
                    <value>set2</value>
                    <value>set3</value>
                </set>
            </property>
            <!--第七种:null注入-->
            <property name="wife">
                <null/>
            </property>
            <!--第八种:properties注入-->
            <property name="info">
                <props>
                    <prop key="info1">This is info1</prop>
                    <prop key="info2">This is info2</prop>
                </props>
            </property>
        </bean>

    3.拓展方式注入

    3.1 p命名空间注入

      p命名空间使用存在前提,需要先把需要的内容导入如下,同时需要无参构造方法

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

      然后进行使用

    <!--p命名空间注入-->
    <bean id="userBean" class="com.aircl.domain.User" p:name="aircl"></bean>

    3.2 c命名空间注入

      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">
    </beans>

      然后进行使用

    <!--c命名空间注入-->
    <bean id="userBean" class="com.aircl.domain.User" c:name="aircl"></bean>
  • 相关阅读:
    css display:flex 属性
    手机端设计规范(750*1334)
    太阳系动画
    关于调用三方平台接口与推送接口的总结<二>(2020.7.27)
    关于调用三方平台接口与推送接口的总结(2020.7.25)
    IntelliJ IDEA 常用快捷键列表及技巧大全
    JAVA中的泛型
    神兽保佑!
    zookeeper的图形化工具ZooInspector的使用
    Initialization failed for 'https://start.spring.io' Please check URL, network and proxy settings. Error message: Cannot download 'https://start.spring.io': connect timed out问题解决方案
  • 原文地址:https://www.cnblogs.com/AirCL/p/14359269.html
Copyright © 2020-2023  润新知