spring5 ioc bean管理

1.创建对象

 

2.注入属性

2.1 set注入

<?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="user" class="com.cj.spring5.User"></bean>
    <bean id="book" class="com.cj.spring5.Book">
        <property name="name" value="java入门到精通"></property>
        <property name="price" value="88"></property>
    </bean>
</beans>
package com.cj.spring5;

public class Book {
    private String name;
    private Integer price;
    public void setName(String name) {
        this.name = name;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

 2.2 依赖注入

package com.cj.spring5;

public class Orders {
    private Integer id;
    private Integer price;

    public Orders(Integer id, Integer price) {
        this.id = id;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Orders{" +
                "id=" + id +
                ", price=" + price +
                '}';
    }
}
<bean id="orders" class="com.cj.spring5.Orders">
        <constructor-arg name="id" value="1"/>
        <constructor-arg name="price" value="12"/>
    </bean>

 

posted @ 2022-08-18 00:14  写代码的小哥哥  阅读(21)  评论(0)    收藏  举报