依赖注入 Set注入

本文详细介绍了如何在Spring Boot中使用pojo类Address和Student,并通过beans1.xml进行配置。展示了如何实例化Student对象并注入Address对象,以及各种属性的设置和获取方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先写一个address.class

package com.spring.pojo;

public class Address {
    private String address;

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

在写一个student.class

package com.spring.pojo;

import java.util.*;

public class Student {
    private Address address;
    private String name;
    private String[] books;
    private List<String> hobbies;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

    public Student() {
    }

    public Student(String name) {
        this.name = name;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

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

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    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;
    }

    @Override
    public String toString() {
        return "Student{" +
                "address=" + address +
                ", name='" + name + '\'' +
                ", books=" + Arrays.toString(books) +
                ", hobbies=" + hobbies +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

再创建一个beans1.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: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">
<!--p命名方式-->
    <bean id="address" class="com.spring.pojo.Address" p:address="山东"/>
<!--c命名方式
这两种都需要引入约束-->
    <bean id="student" class="com.spring.pojo.Student" c:name="小申">
<!--        &lt;!&ndash;第一种:普通值注入:value&ndash;&gt;-->
<!--        <property name="name" value="小申"/>-->
<!--        第二种,Bean注入,ref-->
        <property name="address" ref="address"/>
<!--        数组-->
        <property name="books">
            <array>
                <value>西游记</value>
                <value>水浒传</value>
                <value>三国演义</value>
                <value>红楼梦</value>
            </array>
        </property>
<!--        List-->
        <property name="hobbies">
            <list>
                <value>唱歌</value>
                <value>游泳</value>
                <value>画画</value>
            </list>
        </property>
<!--        Map-->
        <property name="card">
            <map>
                <entry key="身份证" value="13213213"></entry>
                <entry key="校园卡" value="1321546"></entry>
            </map>
        </property>
<!--        Set-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>COC</value>
                <value>CSGO</value>
            </set>
        </property>
        <property name="wife">
            <null/>
        </property>
<!--        Properties-->
        <property name="info">
            <props>
                <prop key="性别">男</prop>
                <prop key="password">1111111</prop>
                <prop key="id">123456</prop>
            </props>
        </property>
    </bean>
</beans>

最后创建一个TestStudent.class

import com.spring.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestStudent {
    public static void main(String[] args) {
       ApplicationContext context = new ClassPathXmlApplicationContext("beans1.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
/*
Student{
address=Address{address='山东'},
name='小申',
books=[西游记, 水浒传, 三国演义, 红楼梦],
hobbies=[唱歌, 游泳, 画画],
card={身份证=13213213, 校园卡=1321546},
games=[LOL, COC, CSGO],
wife='null',
info={性别=男, password=1111111, id=123456}}
 */
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值