首页 > 编程语言 >javaee spring注解设置单例模式和懒加载模式

javaee spring注解设置单例模式和懒加载模式

时间:2023-09-03 12:36:18浏览次数:44  
标签:name spring price Drink javaee 模式 sugar public String


@Lazy懒加载
@Scope(scopeName =“prototype” )设置多例模式,不加默认单例模式

@Lazy
@Component
@Scope(scopeName ="prototype" )
public class Drink {
    @Value("橙汁")
    private String name;
    @Value("半糖")
    private String sugar;
    @Value("18")
    private float price;

    public String getName() {
        return name;
    }

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

    public String getSugar() {
        return sugar;
    }

    public void setSugar(String sugar) {
        this.sugar = sugar;
    }

    public float getPrice() {
        return price;
    }

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

    public Drink() {
        System.out.println("创建一杯饮料");
    }

    public Drink(String name) {
        this.name = name;
    }

    public Drink(String name, String sugar) {
        this.name = name;
        this.sugar = sugar;
    }

    public Drink(String name, float sugar) {
        this.name = name;
        this.price = sugar;
    }

    public Drink(float sugar, String name) {
        this.name = name;
        this.price = sugar;
    }

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


标签:name,spring,price,Drink,javaee,模式,sugar,public,String
From: https://blog.51cto.com/u_15295225/7339391

相关文章