spring属性注入

一些特殊字段的属性注入方法

实体类代码

package com.xxr.pojo;

import java.util.*;

public class student {

    private String name;
    private Address address;
    private String[] books;
    private List<String> list;
    private Map<String,String> map;
    private Set<String> set;
    private String wife ;// null
    private Properties info;

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

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

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

    public void setList(List<String> 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 setWife(String wife) {
        this.wife = wife;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    public String getName() {
        return name;
    }

    public Address getAddress() {
        return address;
    }

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

    public List<String> getList() {
        return list;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public Set<String> getSet() {
        return set;
    }

    public String getWife() {
        return wife;
    }

    public Properties getInfo() {
        return info;
    }

    @Override
    public String toString() {
        return "student{" +
                "name='" + name + '\'' +
                ", address=" +address.getAddress() +
                ", books=" + Arrays.toString(books) +
                ", list=" + list +
                ", map=" + map +
                ", set=" + set +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

Addressd代码

package com.xxr.pojo;

public class Address {


    private String  Address;

    public String getAddress() {
        return Address;
    }

    public void setAddress(String address) {
        Address = address;
    }

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

benas。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 id="student" class="com.xxr.pojo.student">
        <property name="name" value="set方法已经写了"></property>
        <property name="address" ref="AddressSend"></property>
        <property name="books"  >
            <array>
                <value>array数组赋值1</value>
                <value>array数组赋值2</value>
                <value>array数组赋值3</value>
                <value>array数组赋值4</value>
            </array>
        </property>
<property name="list">
    <list>
        <value>list赋值1</value>
        <value>list赋值2</value>
        <value>list赋值3</value>
        <value>list赋值4</value>
    </list>
</property>
        <property name="wife">
            <null></null>
        </property>
        <property name="set">
            <set>
                <value>set赋值1</value>
                <value>set赋值2</value>
                <value>set赋值3</value>
                <value>set赋值4</value>
            </set>
        </property>
        <property name="map">
            <map>
                <entry key="map一号赋值键" value="map赋值值"></entry>
                <entry key="map二号赋值键" value="map赋值值"></entry>
                <entry key="map三号赋值键" value="map赋值值"></entry>

            </map>
        </property>
        <property name="info">
            <props>
                <prop key="properties一号">注入</prop>
                <prop key="properties二号">注入</prop>
                <prop key="properties三号">注入</prop>
            </props>
        </property>
    </bean>

    <bean id="AddressSend" class="com.xxr.pojo.Address">
        <property name="address" value="Address传值"></property>
    </bean>



</beans>

使用junit测试

   @Test
    public void test2(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        student student = (com.xxr.pojo.student) context.getBean("student");
        String s = student.toString();
        System.out.println(s);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值