Spring 不同类型参数(字符串、数组、Bean对象、list、map、set、空字符串、properties)的注入方式(IDEA)

这篇博客详细介绍了如何在Spring框架中使用XML配置文件进行不同类型的参数注入,包括普通值、引用bean、数组、集合、空值、Map以及Properties对象的注入。通过一个Student类的实例展示了各种注入方式的实现代码。
摘要由CSDN通过智能技术生成

Spring 不同类型参数的注入方式(IDEA)

  • 对应的pojo中的实体类:
import com.sun.javafx.collections.MappingChange;
import org.springframework.core.annotation.MergedAnnotationPredicates;
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 properties;

    public String getName() {
        return name;
    }

    public Address getAddress() {
        return address;
    }

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

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

    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 getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

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

  • 在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
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="address" class="com.whut.pojo.Address">
        <property name="address" value="武汉"/>
    </bean>

    <bean id="student.name" class="com.whut.pojo.Student">
        <!--普通值注入-->
        <property name="name" value="marks_lee"/>
        <!--引用bean注入-->
        <property name="address" ref="address"/>
        <!--数组注入-->
        <property name="books">
            <array>
                <value>Chinese</value>
                <value>English</value>
            </array>
        </property>
        <!--set注入-->
        <property name="games">
            <set>
                <value>lol</value>
                <value>bob</value>
            </set>
        </property>
        <!--list注入-->
        <property name="hobbys">
            <list>
                <value>basketball</value>
                <value>baseball</value>
            </list>
        </property>
        <!--空字符串注入-->
        <property name="wife">
            <null/>
        </property>
        <!--map注入-->
        <property name="card">
            <map>
                <entry key="身份证" value="23456789"/>
                <entry key="性别" value=""/>
            </map>
        </property>
        <!--properties注入-->
        <property name="properties">
            <props>
                <prop key="driver">com.mysql.jdbc.Driver</prop>
                <prop key="url">jdbc:mysql:localhost://3306</prop>
            </props>
        </property>
    </bean>
</beans>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值