【问题标题】:Spring returns new instance each time I use instance variable每次我使用实例变量时,Spring都会返回新实例
【发布时间】:2013-01-15 06:09:14
【问题描述】:

我遇到了有线问题。

这是我的对象定义。

package unittest.prototypetest;

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

@Component("valObject")
@Scope("prototype")
public class ValueObject {
  private String value1;
  private String value2;

  //... getter and setter omitted.
}

我定义了组件扫描标签如下:

<context:component-scan base-package="unittest" scoped-proxy="targetClass" />

然后我尝试通过 ApplicatioinContext 获取它的实例,

//ApplicationContextHelper is a class written by me to easily create ApplicationContext
ValueObject valObject = ApplicationContextHelper.getBean("valObject");
valObject.setValue1("v1");
valObject.setValue2("v2");

System.out.println(valObject.getValue1());
System.out.println(valObject.getValue2());

最连线的结果如下图:

2013-01-15_14:04:02.245| DEBUG | o.s.b.f.s.DefaultListableBeanFactory | Returning cached instance of singleton bean 'valObject'
2013-01-15_14:04:02.246| DEBUG | o.s.b.f.s.DefaultListableBeanFactory | Creating instance of bean 'scopedTarget.valObject'
2013-01-15_14:04:02.246| DEBUG | o.s.b.f.s.DefaultListableBeanFactory | Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2013-01-15_14:04:02.250| DEBUG | o.s.b.f.s.DefaultListableBeanFactory | Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2013-01-15_14:04:02.250| DEBUG | o.s.b.f.s.DefaultListableBeanFactory | Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2013-01-15_14:04:02.252| DEBUG | o.s.b.f.s.DefaultListableBeanFactory | Finished creating instance of bean 'scopedTarget.valObject'
2013-01-15_14:04:02.295| DEBUG | o.s.b.f.s.DefaultListableBeanFactory | Creating instance of bean 'scopedTarget.valObject'
2013-01-15_14:04:02.296| DEBUG | o.s.b.f.s.DefaultListableBeanFactory | Finished creating instance of bean 'scopedTarget.valObject'
2013-01-15_14:04:02.296| DEBUG | o.s.b.f.s.DefaultListableBeanFactory | Creating instance of bean 'scopedTarget.valObject'
2013-01-15_14:04:02.296| DEBUG | o.s.b.f.s.DefaultListableBeanFactory | Finished creating instance of bean 'scopedTarget.valObject'
null
2013-01-15_14:04:02.296| DEBUG | o.s.b.f.s.DefaultListableBeanFactory | Creating instance of bean 'scopedTarget.valObject'
2013-01-15_14:04:02.297| DEBUG | o.s.b.f.s.DefaultListableBeanFactory | Finished creating instance of bean 'scopedTarget.valObject'
null

您可以看到,每次我使用 valObject 实例时,Spring 确实为我的访问创建了一个新实例。 以便系统输出 null,尽管我设置了值。

我做错了吗?请指教,非常感谢。

【问题讨论】:

  • 不得不提的问题不是我设置的“原型”。请看日志,每次调用同一个valObject实例的setValue(),Spring都不应该创建新实例!

标签: spring inversion-of-control


【解决方案1】:

您已将范围设置为原型,这是一个非单例。阅读更多关于它的信息here.

【讨论】:

  • 我确实希望它是一个“原型”,正如您所看到的我的代码示例,它应该保持相同的实例,因为我只问过一次。
猜你喜欢
  • 2016-10-21
  • 2018-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-25
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
相关资源
最近更新 更多