Spring——Bean属性赋值的三种方式


先创建一个普通的java类Car.java:

包含了构造函数以及get函数set函数、toString方法

package di;

public class Car {
    private String brand;
    private String crop;
    private Double price;

    public Car() {
    }

    public Car(String brand, String crop, Double price) {
        this.brand = brand;
        this.crop = crop;
        this.price = price;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getCrop() {
        return crop;
    }

    public void setCrop(String crop) {
        this.crop = crop;
    }

    public Double getPrice() {
        return price;
    }

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

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", crop='" + crop + '\'' +
                ", price=" + price +
                '}';
    }
}

Bean属性赋值的第一种方式——set方式:

 

 测试函数:

 

 测试结果:

 

 

 

第二种赋值方式——构造器的方式:

 

 测试函数及结果如下:

 

 

 

第三种方式——p命名空间的方式

xmlns:p="http://www.springframework.org/schema/p"

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM