总结:

实例代码具体解释:

文件夹结构

Car.java

package com.coslay.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...");
} }

Main.java

package com.coslay.beans.scope;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.coslay.beans.autowire.Car; 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);
}
}

beans-scope.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的scope属性来配置bean的作用域
singleton:默认值,容器初始化时创建bean实例,在整个容器的生命周期内仅仅创建这一个bean。 单例的。 prototype:原型的。容器初始化时不创建bean的实例。而在每次请求时都创建一个新的Bean实例,并返回。 -->
<bean id="car" class="com.coslay.beans.autowire.Car"
scope="prototype">
<property name="brand" value="Audi"></property>
<property name="price" value="300000000"></property>
</bean> </beans>

spring bean范围的更多相关文章

  1. Spring8:一些常用的Spring Bean扩展接口

    前言 Spring是一款非常强大的框架,可以说是几乎所有的企业级Java项目使用了Spring,而Bean又是Spring框架的核心. Spring框架运用了非常多的设计模式,从整体上看,它的设计严格 ...

  2. Spring Bean详细讲解

    什么是Bean? Spring Bean是被实例的,组装的及被Spring 容器管理的Java对象. Spring 容器会自动完成@bean对象的实例化. 创建应用对象之间的协作关系的行为称为:装配( ...

  3. Spring Bean的生命周期(非常详细)

    Spring作为当前Java最流行.最强大的轻量级框架,受到了程序员的热烈欢迎.准确的了解Spring Bean的生命周期是非常必要的.我们通常使用ApplicationContext作为Spring ...

  4. spring bean的生命周期

    掌握好spring bean的生命周期,对spring的扩展大有帮助.  spring bean的生命周期(推荐看)  spring bean的生命周期

  5. spring bean的重新加载

    架构体系 在谈spring bean的重新加载前,首先我们来看看spring ioc容器. spring ioc容器主要功能是完成对bean的创建.依赖注入和管理等功能,而这些功能的实现是有下面几个组 ...

  6. Spring Bean

    一.Spring的几大模块:Data access & Integration.Transcation.Instrumentation.Core Spring Container.Testin ...

  7. 【转】Spring bean处理——回调函数

    Spring bean处理——回调函数 Spring中定义了三个可以用来对Spring bean或生成bean的BeanFactory进行处理的接口,InitializingBean.BeanPost ...

  8. 在非spring组件中注入spring bean

    1.在spring中配置如下<context:spring-configured/>     <context:load-time-weaver aspectj-weaving=&q ...

  9. spring bean生命周期管理--转

    Life Cycle Management of a Spring Bean 原文地址:http://javabeat.net/life-cycle-management-of-a-spring-be ...

  10. Spring Bean配置默认为单实例 pring Bean生命周期

    Bean默认的是单例的. 如果不想单例需要如下配置:<bean id="user" class="..." scope="singleton&q ...

随机推荐

  1. 【MongoDB】Serveral common command of MongoDb

    In the recent days, since the overwork made me exhaused, on arrival to home I will go to bed, which ...

  2. Java Web整合开发(4) -- JSP

    JSP脚本中的9个内置对象: application:    javax.servlet.ServletContext config:          javax.servlet.ServletCo ...

  3. 基于LINUX的多功能聊天室

    原文:基于LINUX的多功能聊天室 基于LINUX的多功能聊天室 其实这个项目在我电脑已经躺了多时,最初写完项目规划后,我就认认真真地去实现了它,后来拿着这个项目区参加了面试,同样面试官也拿这个项目来 ...

  4. 错误 4 自定义工具错误: 无法生成服务引用“DepartMentService”的代码。请检查其他错

    原文:错误 4 自定义工具错误: 无法生成服务引用"DepartMentService"的代码.请检查其他错 问题:     错误 4 自定义工具错误: 无法生成服务引用" ...

  5. WP 前台或后台显示ShellToast

    原文:WP 前台或后台显示ShellToast using Microsoft.Phone.Shell; ShellToast toast = new ShellToast(); toast.Titl ...

  6. background-position 具体的使用说明

    语法: background-position : length || length background-position : position || position 值: length  : ...

  7. linux高级技巧:rsync同步(一个)

    1.rsync基本介绍         rsync这是Unix下的一款应用软件,它能同步更新两处计算机的文件与文件夹,并适当利用差分编码以降低数据传输.rsync中一项与其它大部分类似程序或协议中所未 ...

  8. 从头开始学JavaScript(一)——基础中的基础

    概要:javascript的组成. 各个组成部分的作用 . 一.javascript的组成   javascript   ECMAScript(核心) DOM(文档对象模型) BOM(浏览器对象模型) ...

  9. timesten备份和恢复

    ttIsql "DSN=ttwind;UID=cacheuser;PWD=cacheuser;OraclePWD=cacheuser;" --1.查看当前版本号 Command&g ...

  10. linux在构建SVNserver

    最近搞了一个云计算server,一些尝试部署server相关的东西.作为用显影剂server.首先要考虑的是建立SVNserver.关于构建过程记录.方便以后. 一.安装svn软件.有些云server ...