- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
就我从 spring 文档中读到的而言,如果我将一个原型(prototype)作用域 bean 放在一个更大的作用域 bean 中,原型(prototype)作用域 bean 就像它也是一个 View 作用域 bean 一样。也就是说,它只会在容器 bean 初始化时被初始化。
在我的原型(prototype)作用域 bean 中,我有一个简单的属性和一个 getter setter。
@Repository
@Transactional( readonly=true )
@MyPrototypeScope // see definition of this one at [1]
class MyPrototypeScopedBean {
protected String member ;
public String getMember( ) { return this.member ; }
public void setMember( String member ) {
this.member = member ;
System.err.println( "member is set to " + this.member + ", class is : "
+ this.getClass( ).getName( ) ) ;
}
protected void someMethod( ) {
System.err.println( "The member is " + member + ", class is : "
+ this.getClass( ).getName( ) ) ;
}
}
我的 View 作用域 bean Autowiring 它并尝试使用它。
class MyViewScopedBean {
@Autowired
MyPrototypeScopedBean proto ;
....
public void problematicMethod( ) {
MyPrototypeScopedBean tmpReference = this.proto ; // to be sure we access it only once
tmpReference.setMember( "X" ) ;
System.err.println( "Value of member is " + tmpReference.getMember( ) + ", class is : "
+ this.getClass( ).getName( ) ) ;
this.proto.someMethod( ) ;
}
}
在上述情况下,我希望看到如下输出:
member is set to X, class is : com.xxx.MyPrototypeScopedBean$$EnhancerBySpringCGLIB$$xxxxxx
Value of member is X, class is : com.xxx.MyPrototypeScopedBean
The member is X, class is : com.xxx.MyPrototypeScopedBean
但是,我看到的与下面类似:
member is set to X, class is : com.xxx.MyPrototypeScopedBean$$EnhancerBySpringCGLIB$$xxxxxx
Value of member is null, class is : com.xxx.MyPrototypeScopedBean
The member is null, class is : com.xxx.MyPrototypeScopedBean
好像,spring 以某种方式为“原型(prototype)”的每个引用提供了一个新实例,无论是成员访问还是方法调用。
我的代码显然比这复杂,但这是设计应该做的。所以,我的问题是,我是否期待有什么问题?是否存在任何可能导致此问题的配置问题?或者这只是其他地方出现错误的症状?
谢谢。
更新 1:按照 skaffman
的建议,我在 println 语句的末尾添加了类名。更新代码和输出以反射(reflect)更改。
还在 bean 上添加了范围定义。
[1]
@Target( { ElementType.TYPE, ElementType.METHOD } )
@Retention( RetentionPolicy.RUNTIME )
@Documented
@Scope( BeanDefinition.SCOPE_PROTOTYPE )
public @interface MyPrototypeScope {
ScopedProxyMode proxyMode( ) default ScopedProxyMode.TARGET_CLASS ;
}
最佳答案
您的预感是正确的:每次访问 this.proto
时,您都会得到一个新的原型(prototype)。为实现这一点,Spring 将代理连接到该字段。每次您访问一个方法时,Spring 都会创建一个新实例,调用该方法,...最终,您会意识到自己遇到了问题。
因此将 this.proto
的结果分配给局部变量没有帮助。 Spring 不包装对该字段的访问,因此您如何访问它并不重要。
这意味着对于范围为 prototype
的 bean,您必须非常非常小心。使用它们的最佳方法是只调用一个方法,然后摆脱它们。我的解决方案是在扫描整个 spring 配置的代码中添加一个测试用例,在 BeanDefinition.SCOPE_PROTOTYPE
范围内的任何 bean 都失败,然后改用我自己的工厂。
如果您需要有状态的原型(prototype) bean,那么有两种选择:
第一步在 Java 配置中运行良好:
@Autowired
private BarFactory barFactory;
@Bean
public Foo foo() {
Foo result = new Foo();
result.setBar(barFactory.create());
return result;
}
第二个使用Spring的BeanFactory
:
@Autowired
private ApplicationContext appContext;
....
public void problematicMethod( ) {
MyPrototypeBean tmp = new MyPrototypeBean();
appContex.getappContext.getAutowireCapableBeanFactory().autowireBean( tmp );
...
}
我认为在这种情况下你不能依赖@PostConstruct
;相反,您必须自己调用这些方法。
相关:
关于java - Spring 原型(prototype)作用域行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27776672/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!