17

I have a class named Bar with the following annotation: @Configurable(autowire = Autowire.BY_TYPE)

On a private member I have the following annotation:

@Autowired(required = true)
private Foo foo;

In the spring configuration I have a bean of class Foo. If the bean is defined with scope="prototype" it doesn't work and I get the following exception:

NoSuchBeanDefinitionException: No matching bean of type Foo found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency

Once I change the injected bean scope to "singleton" it works fine.

Isn't auto wiring of prototype scoped bean allowed?

Is there any workaround (beside getting the bean manually)?

Thanks in advance, Avner

Avner Levy
  • 6,601
  • 9
  • 53
  • 92

3 Answers3

18

The following link provide alternative solutions for such scenarios: http://whyjava.wordpress.com/2010/10/30/spring-scoped-proxy-beans-an-alternative-to-method-injection/

The link talks about adding to Foo:

@Component
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "prototype")
class Foo

Which will cause a new instance for every call.

Avner Levy
  • 6,601
  • 9
  • 53
  • 92
-1

I believe its the prototype/singleton thing declared in your xml for that bean is the issue.

Isn't auto wiring of prototype scoped bean allowed?

I think it is not allowed. The logic is if it is allowed, then whenever you use that class, then it needs to reinstantiate that bean always as its field. Which is weird especially if the class that this bean is autowired as a field is a singleton itself.

is there any workaround (beside getting the bean manually)?

Just try to remove the scope attribute, cause if it is of prototype attribute, it won't be retrieved. If those beans(services and DAO) are declared in your applicationContext, just let the autowire annotation get it as singleton since by default beans are singleton, which it should be.

vine
  • 846
  • 6
  • 10
  • Thanks for your answer but removing the scope attribute won't fix my code since all Bar instances will share the same Foo instance which isn't what I need. In additional the Foo instance should be instantiated once per Bar object creation / injection and not per field usage (as far as my understanding). – Avner Levy Jul 18 '12 at 06:33
  • i dont know if there is an alternative for your specific design, tell me if there is. But as far as design is concerned, beans should be used in a stateless way, it means be careful in your fields, make sure fields use in those beans are not global fields but method fields, in this way even its is singleton, you won't worry if it is shared by many classes since no global variable is shared,because it is Stateless. – vine Sep 25 '12 at 07:56
-2

If the injected bean scope is 'Singleton', the same instance of the bean will be auto-wired. If the injected bean scope is 'prototype', new instance will be created as part of auto-wire process.

What version of Spring you are using and also attach the spring-context.xml for more details.

user1268571
  • 79
  • 1
  • 2
  • 3
    I'm aware of the differences between prototype and singleton, I just don't understand why should the bean scope have influence on the auto-wiring. I'm using spring 3. – Avner Levy Mar 22 '12 at 11:55