Spring Set依赖注入,p注入,c注入

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">

    <bean id="address" class="com.cdj.pojo.Address"/>

    <bean id="student" class="com.cdj.pojo.Student">
        <!--Bean注入,ref-->
        <property name="address" ref="address"/>

        <!--数组注入,ref-->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>三国演义</value>
                <value>西游记</value>
                <value>水浒传</value>
            </array>
        </property>
        <!--集合注入,ref-->
        <property name="hobbies">
            <list>
                <value>听歌</value>
                <value>敲代码</value>
                <value>看电影</value>
            </list>
        </property>

        <!--map注入-->
        <property name="card">
            <map>
                <entry key="身份证" value="12312321312321312"/>
                <entry key="银行卡" value="12312321312321312"/>
            </map>
        </property>


        <!--null-->
        <property name="wife">
            <null/>
        </property>


        <!--property-->
        <property name="info">
            <props>
                <prop key="id">123123213</prop>
                <prop key="sex">male</prop>
            </props>
        </property>
    </bean>


<!--p注入-->
    <bean id="student2" class="com.cdj.pojo.Student" p:name="123"></bean>
<!-- c注入(构造器注入)-->
    <bean id="user" class="com.cdj.pojo.User" c:age="123" c:name="hello" c:sex="nv"/>


</beans>
package com.cdj.pojo;



import java.util.*;

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

    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 setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

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

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

    public String getName() {
        return name;
    }

    public Address getAddress() {
        return address;
    }

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

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

    public Map<String, String> getCard() {
        return card;
    }

    public Set<String> getGames() {
        return games;
    }

    public Properties getInfo() {
        return info;
    }

    public String getWife() {
        return wife;
    }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值