Bean的作用域
// src/com.lijunyang.model.Personpublic class Car { private String brand; private double price;}// src/com.lijunyang.test.Main.javapublic class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-autowire.xml"); Car car = (car) ctx.getBean("car"); Car car2 = (car) ctx.getBean("car"); System.out.println(car === car2); /* 当配置文件的bean的scope未设置时,结果为true scope="prototype" 原型的,容器初始化时不创建,只有当每次获取bean的时候都创建一个,结果为false scope="singleton" 单例的,容器初始化时创建 结果为true */ }}// src/beans-spring.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="car" class="com.lijunyang.model.Car" // scope="" // singleton | session | request | prototype p:brand="Audi" p:price="200000" /></beans>