spring的多例和單例模式
阿新 • • 發佈:2019-02-19
呼叫規則為,單例呼叫多例--多例又呼叫單例;
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 才會執行。。。
請問這是為什麼啊? ~
- @Component("pService")
- @Scope("prototype")
- public class PersonServiceImpl implements PersonService {
- @PostConstruct
- public void init() {
- System.out.println("初始化咯~");
- }
- @PreDestroy //這裡spring容器銷燬的時候並沒有執行....
- public void destory(){
- System.out.println("銷燬了~");
- }
- }
這裡是因為:
- @Scope("prototype")