Spring依赖注入-直接注入&Set注入

一个包含多个复杂属性的实体类如下

public class Student {
    private String name;
    private Address address;
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", books=" + books +
                ", hobby=" + hobby +
                ", cards=" + cards +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    public List<String> getBooks() {
        return books;
    }
    public void setBooks(List<String> books) {
        this.books = books;
    }
    public Map<String, String> getHobby() {
        return hobby;
    }
    public void setHobby(Map<String, String> hobby) {
        this.hobby = hobby;
    }
    public Set<String> getCards() {
        return cards;
    }
    public void setCards(Set<String> cards) {
        this.cards = cards;
    }
    public String getWife() {
        return wife;
    }
    public void setWife(String wife) {
        this.wife = wife;
    }
    public Properties getInfo() {
        return info;
    }
    public void setInfo(Properties info) {
        this.info = info;
    }
    private List<String> books;
    private Map<String,String> hobby;
    private Set<String> cards;
    private String wife;
    private Properties info;
}
一、直接值注入:

在Beans.xml中直接注入:

<bean id="student" class="com.tt.pojo.Student">
        <property name="name" value="晚安世界"/>
    </bean>

在测试类里面拿到student对象,取出name属性的值

 public static void main(String[] args) {
        //获取ApplicationContext对象,拿到Spring容器
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        Student student = (Student) context.getBean("student");
      System.out.println(student.getName());
    }
二、Set注入
  <bean id="address" class="com.tt.pojo.Address">
        <property name="address" value="重庆"/>
    </bean>

    <bean id="student" class="com.tt.pojo.Student">
        <!--         普通值注入-->
        <property name="name" value="晚安世界"/>

        <!--    Bean注入-->
        <property name="address" ref="address"/>

         <!--   数组注入     -->
        <property name="books" >
            <array>
                <value>水浒传</value>
                <value>西游记</value>
                <value>三国演义</value>
                <value>红楼梦</value>
            </array>
        </property>

<!--        list注入-->
        <property name="classmates">
            <list>
                <value>张三</value>
                <value>李四</value>
                <value>王五</value>
                <value>田六</value>
            </list>
        </property>

<!--        Map注入-->
        <property name="hobby">
            <map>
                <entry key="学生证" value="2018305086"/>
                <entry key="身份证" value="123121231212123"/>
            </map>
        </property>

<!--        Set注入-->
        <property name="cards">
            <set>
                <value>学生证</value>
                <value>身份证</value>
                <value>银行卡</value>
            </set>
        </property>

<!--        空值注入-->
        <property name="wife">
            <null/>
        </property>

<!--        properties-->
        <property name="info">
            <props>
                <prop key="username">admin</prop>
                <prop key="password">ftt123123</prop>
            </props>
        </property>

    </bean>

打印数据
public class MyTest {
    public static void main(String[] args) {
        //获取ApplicationContext对象,拿到Spring容器
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
    }
/*
* Student{name='晚安世界',
*  address=Address{address='重庆'}, 
* books=[水浒传, 西游记, 三国演义, 红楼梦], 
* classmates=[张三, 李四, 王五, 田六], 
* hobby={学生证=2018305086, 身份证=123121231212123}, 
* cards=[学生证, 身份证, 银行卡], 
* wife='null', i
* nfo={password=ftt123123, username=admin}}
* */
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值