依赖注入01(Dependency Injection,DI)

1、set方式注入【重点】

依赖:bean对象的创建依赖于容器;

注入:bean对象中的所有属性,有容器来注入。

1.1、环境搭建

  1. 编写实体类;

    package com.beyond.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 + '\'' +
                    '}';
        }
    }
    
    package com.beyond.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;
    
        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> getGames() {
            return games;
        }
    
        public void setGames(Set<String> games) {
            this.games = games;
        }
    
        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;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + ", \n" +
                    " address=" + address.toString() +", \n" +
                    " books=" + Arrays.toString(books) +", \n" +
                    " hobbys=" + hobbys +", \n" +
                    " card=" + card +", \n" +
                    " games=" + games +", \n" +
                    " info=" + info +", \n" +
                    " wife='" + wife + ", \n" +
                    '}';
        }
    }
    
  2. 编写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="stu" class="com.beyond.pojo.Student">
            <!--第一种,普通值的注入-->
            <property name="name" value="遇见狂神说"/>
        </bean>
    
    </beans>
    
  3. 测试。

    import com.beyond.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 stu = (Student) context.getBean("stu");
            System.out.println(stu.getName());
        }
    }
    

1.2、set注入的汇总

  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="address" class="com.beyond.pojo.Address">
            <property name="address" value="中国"/>
        </bean>
    
        <bean id="stu" class="com.beyond.pojo.Student">
            <!--第一种,普通值的注入-->
            <property name="name" value="遇见狂神说"/>
    
            <!--第二种,引用值的注入-->
            <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="111111"/>
                    <entry key="学生证" value="666666"/>
                </map>
            </property>
    
            <!--第六种,set的注入-->
            <property name="games">
                <set>
                    <value>LOL</value>
                    <value>王者</value>
                </set>
            </property>
    
            <!--第七种,property的注入
            key=value
            key=value
            key=value
            ......
            -->
            <property name="info">
                <props>
                    <prop key="班级">计科1802</prop>
                    <prop key="年龄">16</prop>
                </props>
            </property>
    
            <!--第八种,引用值的注入-->
    <!--        <property name="wife" value="null"/>-->
            <property name="wife">
                <null/>
            </property>
        </bean>
    
    </beans>
    
  2. 测试。

    import com.beyond.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 stu = (Student) context.getBean("stu");
            System.out.println(stu.toString());
    
            /*
            Student{name='遇见狂神说,
                     address=Address{address='中国'},
                     books=[数据结构, 计算机组成原理, 操作系统, 计算机网络],
                     hobbys=[敲代码, 看电影, 听歌],
                     card={身份证=111111, 学生证=666666},
                     games=[LOL, 王者],
                     info={班级=计科1802, 年龄=16},
                     wife='null,
              }
             */
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值