• 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

      

  • 相关阅读:
    Objective-C传递数据小技巧
    得到当前活动的controller
    ios7去除手势滑动返回
    生活小常识
    通过email分享
    release下去除nslog宏
    AFNetworking VS ASIHTTPRequest
    web服务器和应用服务器
    mac 搭建git服务器
    UIKit基础:14-序列帧动画的简单介绍
  • 原文地址:https://www.cnblogs.com/wujixing/p/5453950.html
Copyright © 2020-2023  润新知