2.2 Spring属性注入-构造方法

代码:

spring2.2-构造方法注入.zip - 蓝奏云文件大小:14.9 K|https://www.lanzouw.com/iLbBBvpug4b

1、定义Category和Product类,定义有参构造函数,

package com.how2j.pojo;

public class Category {

    private int id;
    private String name;

    //定义无参构造函数,如果提供了Set方法,则可以使用Set方法注入属性
    public Category() {

    }

    //定义有参构造函数,可以使用构造方法来注入属性
    public Category(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package com.how2j.pojo;

public class Product {
    //普通属于类型
    private int id;
    private String name;
    //引用类型
    private Category category;

    public Product() {

    }

    public Product(int id, String name, Category category) {
        this.id = id;
        this.name = name;
        this.category = category;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    @Override
    public String toString() {
        return "Product{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", category=" + category +
                '}';
    }
}

2、编写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="c" class="com.how2j.pojo.Category">
        <constructor-arg name="id" value="1"/>
        <constructor-arg name="name" value="category 1"/>
    </bean>


    <bean id="p" class="com.how2j.pojo.Product">
<!--        普通属性-->
        <constructor-arg name="id"  value="1"/>
        <constructor-arg name="name" value="product 1"/>
<!--        引用属性-->
        <constructor-arg name="category" ref="c"/>
    </bean>
</beans>

3、测试

package test;

import com.how2j.pojo.Category;
import com.how2j.pojo.Product;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {

    @Test
    //spring的控制翻转
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml" );

        Category c = (Category) context.getBean("c");

        System.out.println(c);
    }

    @Test
    //测试spring的属性注入
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml" );

        Product p = (Product) context.getBean("p");

        System.out.println(p);
    }
}

补充:目录结构如下:

Guess you like

Origin blog.csdn.net/stay_zezo/article/details/120938178