1

I went through the following page to bootstrap an applicationContext.xml in Java.

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

My applicationContext has something like:

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

I need to refer to "foo" in Java as follows:

@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:

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.

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".

  1. How do I set Foo as new instance all the time to FooRepository when FooRepository is created?
  2. The instance of Foo should be from the XML file.
serverfaces
  • 1,155
  • 4
  • 22
  • 49
  • why is foo annotated with `**prototype**` instead of `prototype`? http://www.tutorialspoint.com/spring/spring_bean_scopes.htm And can you provide the souce of Foo? is it annotated with something as well like Component? – Zarathustra Apr 24 '15 at 05:12
  • I wanted to use the bold font for it. Actually it is just "prototype". I am editing it now... – serverfaces Apr 24 '15 at 05:16

2 Answers2

1

You need to remove the entry of Foo from xml. 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.

@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));
    }
}
Community
  • 1
  • 1
Naman Gala
  • 4,670
  • 1
  • 21
  • 55
-1

First Approach:

Foo should be an prototype as well.

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);
            }
}
Zarathustra
  • 2,853
  • 4
  • 33
  • 62