【Spring注入】属性,对象,集合,数组

一、建一个spring空项目

二、新建student.java类

package com.printValue;

public class Student {
    private String age;

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public void p(){
        System.out.println("注入类");
    }

    @Override
    public String toString() {
        return "age:"+age;
    }

}

三、新建SpringCollection.java类

package com.printValue;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class SpringCollection {//集合型的注入,对象的注入,数组的注入
    private String name;
    private List<Object> list;
    private Map<String,String> map;
    private Set<String> set;
    private Student student;
    private String[] strArr;//数组

    public SpringCollection(String name){
        this.name = name;
        System.out.println("constructor注入:"+name);//构造器注入的值
    }

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

    public void setList(List<Object> list) {
        this.list = list;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public void setStrArr(String[] strArr) {
        this.strArr = strArr;
    }

    public void print(){
        System.out.println("property注入:"+name);
        System.out.println("----list----");
        for(Object l:list){
            System.out.print(l+" ");
        }
        System.out.println();
        System.out.println("---set---");
        for(String s:set){
            System.out.print(s+" ");
        }
        System.out.println();
        System.out.println("---map---");
        for(Entry<String, String> entity:map.entrySet()){
            System.out.print(entity.getKey()+":"+entity.getValue()+" ");
        }
        System.out.println();
        System.out.println("----注入类----");
        student.p();

        System.out.println();
        System.out.println("----注入数组----");
        for(String s1:strArr){
            System.out.print(s1+" ");
        }
    }
}

四、新建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.xsd">  
    <bean name="collection" class="com.printValue.SpringCollection">
        <constructor-arg index="0" type="java.lang.String" value="constructor"></constructor-arg>
        <property name="name" value="sjt"></property>
        <property name="list">
            <list>
                <value>1</value>
                <value>2</value>
                <value>1</value>
                <value>2</value>
                <ref bean="stu"/><!--对象放到list中 -->
            </list>
        </property>
        <property name="set">
            <set>
                <value>1</value>
                <value>2</value>
                <value>1</value>
                <value>2</value>
            </set>
        </property>
        <property name="map">
            <map>
                <entry key="1" value="sjt1"></entry>
                <entry key="2" value="sjt2"></entry>
                <entry key="1" value="sjt11"></entry>
                <entry key="2" value="sjt22"></entry>
            </map>
        </property>
        <property name="student" ref="stu" ></property>
        <property name="strArr">
            <list>
                <value>1</value>
                <value>2</value>
                <value>3</value>
                <value>4</value>
            </list>
        </property>
    </bean>
    <bean name="stu" class="com.printValue.Student">
        <property name="age" value="12"></property>
    </bean>
</beans>

五、新建测试类SpringCollectionTest.java

package com.nuc.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.printValue.SpringCollection;

public class SpringCollectionTest {
    ApplicationContext ac = new ClassPathXmlApplicationContext("beans_collection.xml");
    @Test
    public void test(){
        SpringCollection sc = (SpringCollection) ac.getBean("collection");
        sc.print();
    }
}

六、测试结果

这里写图片描述

七、注意事项:

  • 数组注入的时候,使用的也是list+value
<property name="strArr">
    <list>
        <value>1</value>
        <value>2</value>
        <value>3</value>
        <value>4</value>
    </list>
</property>
  • 对象注入的时候,是另外写一个bean,然后用引入到你想引入的bean
<bean name="stu" class="com.printValue.Student">
        <property name="age" value="12"></property>
</bean>
  • map输出的时候,是要用entry,并且map得调用entrySet方法。
for(Entry<String, String> entity:map.entrySet()){   
    System.out.print(entity.getKey()+":"+entity.getValue()+" ");
}
  • 直接输出对象时,需要重写toString方法
@Override
public String toString() {
    return "age:"+age;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值