spring02

本部分是依赖注入。

1、环境(pojo)

**本部分摘抄自秦老师的笔记 **

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

本次联系所使用的类有:

  • User

    package com.nevesettle.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> game;
        private Properties info;
        private String wife;
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", address=" + address.toString() +
                    ", books=" + Arrays.toString(books) +
                    ", hobbys=" + hobbys +
                    ", card=" + card +
                    ", game=" + game +
                    ", info=" + info +
                    ", wife='" + wife + '\'' +
                    '}';
        }
    
        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> getHobbys() {
            return hobbys;
        }
    
        public void setHobbys(List<String> hobbys) {
            this.hobbys = hobbys;
        }
    
        public Map<String, String> getCard() {
            return card;
        }
    
        public void setCard(Map<String, String> card) {
            this.card = card;
        }
    
        public Set<String> getGame() {
            return game;
        }
    
        public void setGame(Set<String> game) {
            this.game = game;
        }
    
        public Properties getInfo() {
            return info;
        }
    
        public void setInfo(Properties info) {
            this.info = info;
        }
    
        public String getWife() {
            return wife;
        }
    
        public void setWife(String wife) {
            this.wife = wife;
        }
    }
    
    
  • Address

    package com.nevesettle.pojo;
    
    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、注入

注入的主要操作还是在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="address" class="com.nevesettle.pojo.Address">
        <property name="address" value="河北"/>
    </bean>

    <bean id="student" class="com.nevesettle.pojo.Student">
        <!--普通注入-->
        <property name="name" value="不将就"/>

        <!--bean注入-->
        <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>
            </list>
        </property>

        <!--map注入-->
        <property name="card">
            <map>
                <entry key="身份证" value="130494111111111111"/>
                <entry key="银行卡" value="13115165165165165"/>
            </map>
        </property>

        <!--set注入 -->
        <property name="game">
            <set>
                <value>LOL</value>
            </set>
        </property>

        <!--properties注入-->
        <property name="info">
            <props>
                <prop key="学号">20201515</prop>
                <prop key="性别">男</prop>
            </props>
        </property>

        <!--null注入-->
        <property name="wife">
            <null/>
        </property>

    </bean>

</beans>
posted @ 2020-03-09 19:15  Nevesettle  阅读(106)  评论(0)    收藏  举报