java单例模式与多例_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 才会执行。。。

请问这是为什么啊? ~

@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操作而已

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值