调用规则为,单例调用多例--多例又调用单例;
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")
定义为prototype类型的实例创建之后spring就不在管理了,它只是做了new操作而已