I can’t figure out why autowiring doesn’t work as it should. I’ve a non-web application which uses spring 3 and so in the application’s main method I’ve set to create the application context like this:
ApplicationContext ctx = new ClassPathXmlApplicationContext(
DEFAULT_APP_CONTEXT);
And the .xml file has at the top the needed stuff for autowiring to work:
<context:annotation-config />
<context:component-scan base-package="star,hw" />
However it doesn’t work. I’ve a service class with a class level @Repository annotation under hw package, but when trying to @Autowire it I get:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'inventoryDAO' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:527)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1083)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:274)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
... 77 more
One weird thing i noticed in the log file:
Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4440ece0: defining beans [*skipping*, inventoryDAOImpl, *skipping*]
That is the class I’ve @Repository used on, but it implements InventoryDAO. Isn’t that wrong that it shows there inventoryDAOImpl? What can I do to figure out what’s wrong?
Edit: Okay, probably the problem isn’t in @Autowire but in @Repository for not making the bean correctly.
Try
@Repository("inventoryDAO").