redfox26 redfox26 - 3 months ago 7
Java Question

Autowired problem with a bean

with spring 2.5,

i have a class StockDwr

StockDwr it's a bean name stock who have a prototype scope

in this class, i have an attribute ModRepository with @Autowired annotation

ModRepository is usedd only by this class

in this class i only have that (because i try to find the problem)

public void read(String fileName) {
System.out.println(fileName);
}


when i go to the jsp who use stock i get

Error creating bean with name 'stock': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.domain.ModRepository com.web.StockDwr.modRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.domain.ModRepository] is defined: Unsatisfied dependency of type [com.domain.ModRepository]: expected at least 1 matching bean

i don't defined ModRepository in xml file, i tried to do it, but it get the same problem

any idea?

Answer

Probably you have made incorrect bean definition for ModRepository in the xml file. spring is not able to find the bean com.domain.ModRepository

Autowiring happens byType. Is your ModRepository implementation of some interface? In that case if there are many implementation spring will not be able to identify a bean uniquely.

To avoid that use @Qualifier annotation. Qualifier makes sure the autowiring happens byName.

Alternatively you can try with the annotation, Put a @Component on top of ModRepository class and make sure you have these two lines in your applicationContext.xml

  <context:annotation-config />
  <context:component-scan base-package="com.domain" />
Comments