ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Spring_DI注入依赖

2022-03-28 12:02:30  阅读:127  来源: 互联网

标签:依赖 return String DI Spring void private address public


依赖注入:set注入

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

实体类:

点击查看代码
package com.cn.demo;

import java.util.*;

public class Student {
    private String name;//value
    private Address address;//ref
    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 + '\'' +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", info=" + info +
                ", wife='" + wife + '\'' +
                '}';
    }
}


点击查看代码
package com.cn.demo;

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 + '\'' +
                '}';
    }
}


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



    <bean id="address" class="com.cn.demo.Address">
        <property name="address" value="武汉市"/>
    </bean>

    <bean id="student2" class="com.cn.demo.Student">
        <!--普通值注入-->
        <property name="name" value="杨晨"/>

        <!--bean注入-->
        <property name="address" ref="address"/>

        <!--数组注入-->
        <property name="books">
            <array>
                <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="020321752326"/>
                <entry key="学院" value="计算机学院"/>
                <entry key="班级" value="523"/>
            </map>
        </property>

        <!--set注入-->
        <property name="games">
            <set>
                <value>英雄联盟</value>
                <value>王者荣耀</value>
            </set>
        </property>

        <!--null注入-->
        <property name="wife">
            <null/>
        </property>
        <!--Properties注入-->
        <property name="info">
            <props>
                <prop key="学号">020321752326</prop>
                <prop key="姓名">杨晨</prop>
            </props>
        </property>
    </bean>




</beans>

测试:

点击查看代码
import com.cn.demo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StudentTest{
    public static void main(String[] args) {
        /*获取Spring的上下文对象*/
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        /*我们的对象交给Spring去管理了,我们要使用,直接去取出来即可*/
        Student student = (Student) context.getBean("student2");
        System.out.println(student.toString());


    }

        }

结果截图:

标签:依赖,return,String,DI,Spring,void,private,address,public
来源: https://www.cnblogs.com/youngchen/p/16066452.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有