1,Bean对象
package com.songyan.scope; public class Bean4 { }
2,配置文件
<?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="bean4" class="com.songyan.scope.Bean4" scope="singleton"></bean> </beans>
3,测试类
package com.songyan.scope; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ScopeTest { public static void main(String[] args) { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/songyan/scope/Beans4.xml"); Bean4 bean4=(Bean4)applicationContext.getBean("bean4"); System.out.println(bean4); bean4=(Bean4)applicationContext.getBean("bean4"); System.out.println(bean4); } }
4,结果
5,当作用域改为"prototype"
<?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="bean4" class="com.songyan.scope.Bean4" scope="prototype"></bean> </beans>
6,输出结果