首页 > 其他分享 >spring之依赖注入

spring之依赖注入

时间:2025-02-07 10:20:21浏览次数:2  
标签:dogs 依赖 String spring public books cats id 注入

如下代码中的Student类,有基础类型,数组类型,列表类型,字典类型,该如何进行依赖注入呢

package com.loubin.pojo;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class Student {
    private int id;
    private String name;
    private String[] books;
    private List<Cat> cats;
    private Map<String, Dog> dogs;

    public int getId() {
        return id;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", books=" + Arrays.toString(books) +
                ", cats=" + cats +
                ", dogs=" + dogs +
                '}';
    }

    public void setId(int id) {
        this.id = id;
    }

    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<Cat> getCats() {
        return cats;
    }

    public void setCats(List<Cat> cats) {
        this.cats = cats;
    }

    public Map<String, Dog> getDogs() {
        return dogs;
    }

    public void setDogs(Map<String, Dog> dogs) {
        this.dogs = dogs;
    }
}

 

注入方式如下所示

<?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.xsd">

    <bean class="com.loubin.pojo.Cat" id="cat1">
        <property name="name" value="花花"></property>
    </bean>
    <bean class="com.loubin.pojo.Cat" id="cat2">
        <property name="name" value="咪咪"/>
    </bean>

    <bean class="com.loubin.pojo.Dog" id="dog1">
        <property name="name" value="旺旺"></property>
    </bean>
    <bean class="com.loubin.pojo.Dog" id="dog2">
        <property name="name" value="小黑"></property>
    </bean>

    <bean class="com.loubin.pojo.Student" id="student">
        <property name="id" value="1" />
        <property name="name" value="张三"/>
        <property name="books">
        <array value-type="java.lang.String">
            <value>红楼梦</value>
            <value>朝花夕拾</value>
            <value>三国演义</value>
        </array>
         </property>
        <property name="cats">
            <list value-type="com.loubin.pojo.Cat">
                <ref bean="cat1"></ref>
                <ref bean="cat2"></ref>
            </list>
        </property>

        <property name="dogs">
            <map key-type="java.lang.String" value-type="com.loubin.pojo.Dog">
                <entry key="旺旺" value-ref="dog1"></entry>
                <entry key="小黑" value-ref="dog2"></entry>
            </map>
        </property>
    </bean>
</beans>

 

标签:dogs,依赖,String,spring,public,books,cats,id,注入
From: https://www.cnblogs.com/loubin/p/18702130

相关文章