简体   繁体   English

Spring @Configuration中的Spring原型bean参考

[英]Spring prototype bean reference in Spring @Configuration

I went through the following page to bootstrap an applicationContext.xml in Java. 我浏览了以下页面,以引导Java中的applicationContext.xml。

http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch06.html http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/ch06.html

My applicationContext has something like: 我的applicationContext具有以下内容:

<beans>
    <bean id="foo" class="com.mypackage.Foo" scope="prototype">       
    </bean>
</beans>

I need to refer to "foo" in Java as follows: 我需要在Java中引用“ foo”,如下所示:

@Configuration
@AnnotationDrivenConfig 
@ImportXml("classpath:applicationContext.xml")
public class Config {

    @Autowired Foo foo;

    @Bean(name="fooRepository")
    @Scope("prototype")
    public FooRepository fooRepository() {
        return new JdbcFooRepository(foo);
    }


}

I am creating a reference of FooRepository as follows: 我正在创建FooRepository的引用,如下所示:

ApplicationContext ctx = 
                   new AnnotationConfigApplicationContext(Config.class);

FooRepository fr = ctx.getBean("fooRepository", FooRepository.class);

Every time I invoke it, I get a new instance of FooRepository which is defined as "prototype" and this is fine with me. 每次调用它时,我都会获得一个新的FooRepository实例,该实例被定义为“原型”,这对我来说很好。

But when an instance of FooRepository is returned I see the same instance of "foo" is used though "foo" in the XML file is "prototype". 但是,当返回FooRepository的实例时,尽管XML文件中的“ foo”为“ prototype”,但我看到的是“ foo”的相同实例。

  1. How do I set Foo as new instance all the time to FooRepository when FooRepository is created? 创建FooRepository后,如何始终将Foo设置为FooRepository的新实例?
  2. The instance of Foo should be from the XML file. Foo的实例应来自XML文件。

You need to remove the entry of Foo from xml. 您需要从xml中删除Foo的条目。 You can define it this way. 您可以通过这种方式进行定义。

@Configuration
@AnnotationDrivenConfig
@ImportXml("classpath:applicationContext.xml")
public class Config {

    @Bean(name = "fooRepository")
    @Scope("prototype")
    public FooRepository fooRepository() {
        return new JdbcFooRepository(foo());
    }

    @Bean(name = "foo")
    @Scope("prototype")
    public Foo foo() {
        return new foo();
    }
}

Approach 2 : You can refer this SO Answer . 方法2 :您可以参考此SO Answer

@Configuration
@AnnotationDrivenConfig
@ImportXml("classpath:applicationContext.xml")
public class Config {

    @Autowired
    private ApplicationContext context;

    @Bean(name = "fooRepository")
    @Scope("prototype")
    public FooRepository fooRepository() {
        return new JdbcFooRepository(context.getBean("foo", Foo.class));
    }
}

First Approach: 第一种方法:

Foo should be an prototype as well. Foo也应该是原型。

Second Approach: 第二种方法:

@Configuration 
@AnnotationDrivenConfig 
@ImportXml("classpath:applicationContext.xml") 
public class Config {

            @Bean(name = "fooRepository")
            @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
            public FooRepository fooRepository(Foo foo) {
                return new JdbcFooRepository(foo);
            }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM