Spring DI依赖注入(maven项目)

5 篇文章 0 订阅
4 篇文章 0 订阅

1、spring maven 项目环境的创建请参考我的这篇博客。

idea创建HelloSpring(maven项目)

2、项目结构如下:

在这里插入图片描述

3、创建实体类

创建com.sw.entity package包,在entity包下新建Address和Student实体类。

Address.java

package com.sw.entity;

public class Address {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Address{" +
                "name='" + name + '\'' +
                '}';
    }
}

Student.java

package com.sw.entity;

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 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> 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 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) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

4、创建spring容器

在resource目录下创建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.sw.entity.Address">
        <property name="name" value="狗蛋儿"/>
    </bean>

    <bean name="student" class="com.sw.entity.Student">

        <!--普通值注入   value-->
        <property name="name" value="xiaowu"/>

        <!--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>rap</value>
                <value>篮球</value>
            </list>
        </property>

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

        <!--set注入-->
        <property name="games">
            <set>
                <value>AOA</value>
                <value>BOB</value>
                <value>COC</value>
                <value>LOL</value>
            </set>
        </property>

        <!--null注入-->
        <property name="wife">
            <null/>
        </property>
        
 		<!--properties注入-->
        <property name="info">
            <props>
                <prop key="driver">musql</prop>
                <prop key="rul">www.baidu.com</prop>
                <prop key="username">root</prop>
                <prop key="pwd">123456</prop>
            </props>
        </property>
    </bean>
</beans>

5、创建测试类

DiTest.java

import com.sw.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DiTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student.toString());

    }

}

输出结果:
Student{
name=‘xiaowu’,
address=Address{name=‘狗蛋儿’},
books=[西游记, 三国演义, 水浒传, 红楼梦],
hobbys=[唱, 跳, rap, 篮球],
card={身份证=1234567890,
银行卡=0987654321},
games=[AOA, BOB, COC, LOL],
wife=‘null’,
info={driver=musql, pwd=123456, username=root, rul=www.baidu.com}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值