gpt4 book ai didi

java - Spring 原型(prototype)作用域行为

转载 作者:搜寻专家 更新时间:2023-11-01 03:04:22 25 4
gpt4 key购买 nike

就我从 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,那么有两种选择:

  1. 创建一个单例工厂,并在需要时使用它来创建实例。
  2. 在创建 bean 实例后使用 Spring Autowiring 它。

第一步在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com