spring单例和多例——如何在单例中调用多例对象

spring生成对象默认是单例的。通过scope属性可以更改为多例。

<bean id="user" class="modle.User" scope="prototype">
  </bean>

现在有这么一种情况.
User类调用一个service, 这个service又调用一个tool。

有时我们希望User是多例的,service是单例的,而tool又是多例的。

很自然地想法是配置文件这些写

<bean id="user" class="modle.User" scope="prototype">
    <property name="service" ref="userservice"></property>
  </bean>
  
  <bean id="userservice" class="service.userService" >
    <property name="tool" ref="tool"></property>
  </bean>
  
  <bean id="tool" class="service.ToolImpl" scope="prototype"></bean>

但是这种写法是错误的! 不能使用spring的自动注入!

由于service是单例的,所以这种方法的结果是:User多例,service和tool都是单例。
正确的写法是,是每次调用tool时都生成一个新的tool对象。但是我们又不能手动new一个,要借助BeanFactory。

public class User {
 
  private userService service;
  private int age;
  private Date date;
  private String name;
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public Date getDate() {
    return date;
  }
  public void setDate(Date date) {
    this.date = date;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public userService getService() {
    return service;
  }
  public void setService(userService service) {
    this.service = service;
  }
  
}

UserService 通过实现 BeanFactoryAware 接口来获得factory
由于不使用spring的自动注入,set方法要去掉!

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
public class userService implements BeanFactoryAware{
	  private Tool tool;
	  private BeanFactory factory;
	  public void service(){
	    this.tool = (Tool)factory.getBean("tool");
	    System.out.println(this+":service");
	    tool.work();
	  }
	  public Tool getTool() {
	    return tool;
	  }
	@Override
	public void setBeanFactory(BeanFactory f) throws BeansException {
	    factory = f;
	  }
  
}

配置文件,不能再使用注入。因此要把tool对象的注入去掉!

<bean id="user" class="com.my.spring.User" scope="prototype">
    	<property name="service" ref="userservice"></property>
  	</bean>
  
	  <bean id="userservice" class="com.my.spring.userService" >
	  </bean>
	  <bean id="tool" class="com.my.spring.ToolImpl" scope="prototype"></bean>
public interface Tool {
  public void work();
}

public class ToolImpl implements Tool{
 
  public void work() {
    System.out.println(this+":Tool Work");
  }
  
}

测试类:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    User user = (User)context.getBean("user");
    User user2 =  (User)context.getBean("user");
    System.out.println(user);
    user.getService().service();
    System.out.println();
    System.out.println(user2);
    user2.getService().service();
  }
}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值