Spring03-singleton bean与prototype bean
1. singleton bean
- singleton是bean的默认值。只要bean的id相同,随便拿多次,还是同一个对象。该单个实例存储在缓存中,对该bean所有以后请求和引用都将返回这个缓存中的对象实例。可以更好的重用对象,节省了重复创建对象的开销.如果id不同,则拿到的对象不一样。
- id相同时:
<?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="someService1" class="com.caorui.service.impl.SomeServiceImpl"></bean> <bean id="someService2" class="com.caorui.service.impl.SomeServiceImpl"></bean> </beans>
@Test public void testSome01() { //创建容器对象,ApplicationContext初始化时,所有容器中beans创建完毕 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); SomeService service1 = ac.getBean("someService1", SomeService.class); SomeService service2 = ac.getBean("someService1", SomeService.class); System.out.println(service1 == service2); //ture }
- id不同时:
<?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="someService1" class="com.caorui.service.impl.SomeServiceImpl"></bean> <bean id="someService2" class="com.caorui.service.impl.SomeServiceImpl"></bean> </beans>
@Test public void testSome01() { //创建容器对象,ApplicationContext初始化时,所有容器中beans创建完毕 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); SomeService service1 = ac.getBean("someService1", SomeService.class); SomeService service2 = ac.getBean("someService2", SomeService.class); System.out.println(service1 == service2); //false }
- id相同时:
2. prototype bean
- IoC容器导致每次对该特定的bean请求时创建一个新的bean实例.从某种意义上来说,Spring容器在prototype bean上的作用等同于java的new操作符。
- id相同时:
<?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="someService1" class="com.caorui.service.impl.SomeServiceImpl" scope="prototype"></bean> <bean id="someService2" class="com.caorui.service.impl.SomeServiceImpl"></bean> </beans>
@Test public void testSome01() { //创建容器对象,ApplicationContext初始化时,所有容器中beans创建完毕 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); SomeService service1 = ac.getBean("someService1", SomeService.class); SomeService service2 = ac.getBean("someService1", SomeService.class); System.out.println(service1 == service2); //false }
- id不同时:
<?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="someService1" class="com.caorui.service.impl.SomeServiceImpl" scope="prototype"></bean> <bean id="someService2" class="com.caorui.service.impl.SomeServiceImpl"></bean> </beans>
@Test public void testSome01() { //创建容器对象,ApplicationContext初始化时,所有容器中beans创建完毕 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); SomeService service1 = ac.getBean("someService1", SomeService.class); SomeService service2 = ac.getBean("someService2", SomeService.class); System.out.println(service1 == service2); //false }
- id相同时: