【Spring】06 Bean 作用域

本文内容纲要:

- Bean 作用域

- 作用域

- singleton

- prototype

- Car.java(com.test.sping.beans.autowire.Car)

Bean 作用域

  • 作用域

    • singleton
      • 第一种情况
      • 第二种情况

    • prototype
      • 第一种情况
      • 第二种情况

    Car.java(com.test.sping.beans.autowire.Car)

作用域

singleton

默认值。 容器初始时创建 bean 实例,在整个容器的生命周期内只创建这一个 bean,单例的。

  • beans-scope.xml

    <bean id="car"

    class="com.test.sping.beans.autowire.Car"

    scope="singleton">

    <property name="brand" value="Audi"></property>

    <property name="price" value="300000"></property>

    </bean>

第一种情况

  • Main.java

    package com.test.sping.beans.scope;

    import com.test.sping.beans.autowire.Car;

    import org.springframework.context.ApplicationContext;

    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class Main {

    public static void main(String[] args) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");

    }

    }

第二种情况

  • Main.java

    package com.test.sping.beans.scope;

    import com.test.sping.beans.autowire.Car;

    import org.springframework.context.ApplicationContext;

    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class Main {

    public static void main(String[] args) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");

    Car car = (Car) ctx.getBean("car");

    Car car2 = (Car) ctx.getBean("car");

    System.out.println(car == car2);

    }

    }

prototype

原型的。 容器初始化时不创建 bean 的实例。 而在每次请求时都创建一个新的 Bean 实例,并返回。

  • beans-scope.xml

    <bean id="car"

    class="com.test.sping.beans.autowire.Car"

    scope="prototype">

    <property name="brand" value="Audi"></property>

    <property name="price" value="300000"></property>

    </bean>

第一种情况

  • Main.java

    package com.test.sping.beans.scope;

    import com.test.sping.beans.autowire.Car;

    import org.springframework.context.ApplicationContext;

    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class Main {

    public static void main(String[] args) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");

    }

    }

第二种情况

  • Main.java

    package com.test.sping.beans.scope;

    import com.test.sping.beans.autowire.Car;

    import org.springframework.context.ApplicationContext;

    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class Main {

    public static void main(String[] args) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");

    Car car = (Car) ctx.getBean("car");

    Car car2 = (Car) ctx.getBean("car");

    System.out.println(car == car2);

    }

    }

Car.java(com.test.sping.beans.autowire.Car)

package com.test.sping.beans.autowire;

public class Car {

private String brand;

private double price;

public String getBrand() {

return brand;

}

public void setBrand(String brand) {

this.brand = brand;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

@Override

public String toString() {

return "Car{" +

"brand='" + brand + '\'' +

", price=" + price +

'}';

}

public Car() {

System.out.println("Car's Constructor...");

}

}

本文内容总结:Bean 作用域,作用域,singleton,prototype,Car.java(com.test.sping.beans.autowire.Car),

原文链接:https://www.cnblogs.com/zmemory/p/12755898.html

以上是 【Spring】06 Bean 作用域 的全部内容, 来源链接: utcz.com/z/362433.html

回到顶部