1. 程式人生 > >spring的多例和單例模式

spring的多例和單例模式

   呼叫規則為,單例呼叫多例--多例又呼叫單例;

1、建立一個單例:

/**
 */
package com.ec.hongjia.agri.resource.service.impl;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

/**
* @author owenhuang
* @description classDescription
* @version 建立時間:2018年3月8日 下午1:56:38
*/
@Component
public class NormalSingleton implements BeanFactoryAware, InitializingBean, DisposableBean{

	private BeanFactory beanFactory;
	
	public void hello(String str){
		NormalPrototype normalPrototype = (NormalPrototype) this.beanFactory.getBean("NormalPrototype");
		if(normalPrototype!=null){
			normalPrototype.test(str);
		}
		else{
			System.out.println("null");
		}
	}
	 
	@Override
	public void setBeanFactory(BeanFactory bf) throws BeansException {
		System.out.println("==========");
		this.beanFactory = bf;
		
	}
 
	@Override
	public void destroy() throws Exception {
		System.out.println("destory---NormalSingleton");
	}

	 
	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("afterPropertiesSet-NormalSingleton");
	}
	
	
	
}

2、建立一個多例

/**
 */
package com.ec.hongjia.agri.resource.service.impl;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
* @author owenhuang
* @description classDescription
* @version 建立時間:2018年3月8日 下午1:55:02
*/
@Component("NormalPrototype")
@Scope("prototype")
public class NormalPrototype implements InitializingBean, DisposableBean{
	
	private StringBuilder sb = new StringBuilder();
	
	private byte[] buffer = new byte[1024*1024*256];
	
	@Autowired
	private NormalSingleTonByPro normalSingleTonByPro;
	
	public void test(String str){
		sb.append(str);
		System.out.println(sb.toString()+"/"+buffer.length);
		this.normalSingleTonByPro.world();
		
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("初始化bean");
	}
 
	@Override
	public void destroy() throws Exception {
		System.out.println("銷燬bean-NormalPrototype");
	}
}

3、建立一個單例

/**
 */
package com.ec.hongjia.agri.resource.service.impl;

import org.springframework.stereotype.Component;

/**
* @author owenhuang
* @description classDescription
* @version 建立時間:2018年3月8日 下午3:31:36
*/
@Component
public class NormalSingleTonByPro {
	
	private final static byte b[] = new byte[1024*1024*512];
	
	public void world(){
		System.out.println(b.length);
	}
	
}

測試

/**
 */
package com.ec.hongjia.agri.test;

import java.io.IOException;

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.ec.hongjia.agri.resource.service.impl.NormalSingleton;

/**
* @author owenhuang
* @description 計算方式
* @version 建立時間:2017年10月16日 下午6:05:23
*/

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:META-INF/spring/spring-module-mall.xml")
public class CalcuTest2 {
	
	@Autowired
	private NormalSingleton normalSingleton;
	
	@org.junit.Test
	public void tstst(){
		
		for (int i=0;i<20; i++) {
			normalSingleton.hello("aaa-"+i);
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	 
		try {
			System.in.read();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	 
	
}

發現當scope="prototype" ,spring容器銷燬物件的時候 @PreDestory 註釋的方法不會執行, 
scope="singleton" 的 @PreDestory 才會執行。。。 

請問這是為什麼啊? ~ 

  1. @Component("pService")  
  2. @Scope("prototype")  
  3. public class PersonServiceImpl implements PersonService {  
  4.     @PostConstruct  
  5.     public void init() {  
  6.         System.out.println("初始化咯~");  
  7.     }  
  8.     @PreDestroy  //這裡spring容器銷燬的時候並沒有執行....  
  9.     public void destory(){  
  10.         System.out.println("銷燬了~");  
  11.     }  
  12. }  

這裡是因為:

  1. @Scope("prototype")   
定義為prototype型別的例項建立之後spring就不在管理了,它只是做了new操作而已