Spring--Bean scope

singleton, prototype,request, session, global session

bean.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-2.5.xsd">

  <bean name="u" class="com.bjsxt.dao.impl.UserDAOImpl">
  </bean>
	
  <bean id="userService" class="com.bjsxt.service.UserService" scope="prototype">  	
  	<property name="userDAO" ref="u" />  	
  </bean>
</beans>

UserServiceTest.java:

package com.bjsxt.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bjsxt.model.User;

//Dependency Injection
//Inverse of Control
public class UserServiceTest {
	@Test
	public void testAdd() throws Exception {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");	
		UserService service = (UserService)ctx.getBean("userService");
		UserService service2 = (UserService)ctx.getBean("userService");		
		System.out.println(service == service2);

	}

}

结果:false

xml改成singleton结果就是true

 

  

posted @ 2016-05-03 10:00  wujixing909  阅读(134)  评论(0编辑  收藏  举报