Spring Prototype Scope行为

时间:2015-01-05 09:33:08

标签: java spring

据我从spring文档中读到,如果我将原型scoped bean放在一个更大的scoped bean中,原型scoped bean的行为就好像它也是一个视图范围的bean。意思是,只有在初始化容器bean时才会初始化它。

在我的原型scoped 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( ) ) ;
    }
}

我的视图范围bean自动装配它并尝试使用它。

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

好像,春天以某种方式给了我一个新的实例,用于" proto"的每个引用,无论是成员访问还是方法调用。

我的代码显然比这更复杂,但这就是设计应该做的事情。所以,我的问题是,我是否期待出错?是否存在可能导致此问题的配置问题?或者这只是其他地方的一个错误的症状?

谢谢。

更新1: 根据{{​​1}}的建议,我在println语句的末尾添加了类名。更新代码和输出以反映更改。

还在bean上添加了范围定义。

[1]

skaffman

1 个答案:

答案 0 :(得分:3)

您的预感是正确的:每次访问this.proto时,您都会得到一个新的原型。为了实现这一点,Spring将代理连接到字段中。每次访问方法时,Spring都会创建一个新实例,调用方法,......最后,您会发现自己遇到了问题。

因此,将this.proto的结果分配给局部变量并不会有帮助。 Spring并不包含对字段的访问权限,因此访问它的方式并不重要。

这意味着对于范围为prototype的bean,您必须非常非常小心。使用它们的最佳方法是只调用一个方法然后去除它们。我的解决方案是在代码中添加一个测试用例,该代码扫描整个spring配置,对于BeanDefinition.SCOPE_PROTOTYPE范围内的任何bean都失败,并使用我自己的工厂。

如果您需要有状态原型bean,那么有两个选项:

  1. 创建一个单件工厂,并在需要时使用它来创建实例。
  2. 使用Spring在创建bean实例后自动装配它。
  3. 第一步在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;相反,你必须自己调用这些方法。

    相关:

相关问题