美文网首页
7.Spring Bean的作用域

7.Spring Bean的作用域

作者: Vincentl_Hui | 来源:发表于2018-03-15 21:20 被阅读0次

Bean 的作用域

在Spring中定义一个Bean,Ioc容器可限制该bean作用于某个范围.

Bean的常用作用域

singleton的作用域 :
xml中的config
<?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="car" class="com.atguigu.spring.beans.Car"
    scope="singleton">
     <property name="brand" value="Audi"></property>
     <property name="price" value="30000"></property>
    </bean>

</beans>
Main.c
public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //1. 创建Spring 的IOC容器对象
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");
        
        //2.从IOC容器中获取bean 实例
        //利用id定位到IOC 容器中的Bean
        Car car = (Car) ctx.getBean("car");
        
        Car car2 = (Car) ctx.getBean("car");
        System.out.println(car == car2);
    }

}

运行结果:
信息: Loading XML bean definitions from class path resource [beans-scope.xml]
true
说明:

singleton属性限制了在IOC容器中,不再创建新的bean对象,唯独一个对象.

prototype的作用域
xml中的config
<?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="car" class="com.atguigu.spring.beans.Car"
    scope="prototype">
     <property name="brand" value="Audi"></property>
     <property name="price" value="30000"></property>
    </bean>

</beans>
Main.c
public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //1. 创建Spring 的IOC容器对象
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-scope.xml");
        
        //2.从IOC容器中获取bean 实例
        //利用id定位到IOC 容器中的Bean
        Car car = (Car) ctx.getBean("car");
        
        Car car2 = (Car) ctx.getBean("car");
        System.out.println(car == car2);
    }

}

运行结果:
信息: Loading XML bean definitions from class path resource [beans-scope.xml]
false
说明:

每次注入或请求要给bean的时候都创建一个新的bean

Bean的常用作用域总结
作用域 描述
singleton 整个应用中,只创建一个bean
prototype 每次注入或请求要给bean的时候都创建一个新的bean。

相关文章

  • 7.Spring Bean的作用域

    Bean 的作用域 在Spring中定义一个Bean,Ioc容器可限制该bean作用于某个范围. Bean的常用作...

  • Spring笔记(二):bean的作用域

    在bean中配置作用域使用属性scope来设置bean的作用域 scope="singleton" 也是bean配...

  • SPRING BEAN的基础

    一、SPRING BEAN的定义: 二、SPRING BEAN的作用域: 作用域例子: your msg :p...

  • Spring_04_Bean的作用域

    Bean的作用域  当在Spring中定义个bean时,你必须声明bean的作用域选项.例如,为了强制Spring...

  • Spring Bean 作用域

    原文 :一文读懂Spring Bean作用域 - RelaxHeart网 Spring Bean的几种作用域 Sp...

  • Bean的作用域

    Bean的作用域: singleton 当一个bean的作用域为singleton,那么Spring IoC容器中...

  • Bean的作用域

    Bean的作用域: singleton 当一个bean的作用域为singleton,那么Spring IoC容器中...

  • Spring

    Spring Bean 作用域 Spring 3 中为 Bean 定义了 5 中作用域分别为 singleton(...

  • spring bean 配置

    bean属性: set注入、构造器注入 beans 作用域 : springBean生命周期1.Bean的作用域可...

  • Spring的作用域和生命周期

    Bean的作用域 Spring 3中为Bean定义了5中作用域,分别为singleton(单例)、prototyp...

网友评论

      本文标题:7.Spring Bean的作用域

      本文链接:https://www.haomeiwen.com/subject/ebpqqftx.html