dimanche 1 mars 2015

Autowiring fails in implementation class

I have a class implementing an interface as below:



@Service
public class BaseImpl implements Base{
@Autowired
Details details;
...
public void doSomething(){
}
}


Code for the interface:



@Service
public interface Base{
void doSomething();
}


My configuration class:



@Configuration
public class AppConfig {
@Bean(name="samplebean")
public Base getImpl(){
return new BaseImpl();
}
}


Finally, I call the BaseImpl class as below:



@RestController
@RequestMapping(value = "...")
public class Caller{
public void foo(){
Base b = (Base) context.getBean("samplebean");
b.doSomething();
}
}


I get the following error:



Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.model.Details com.pkg.BaseImpl.details; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.model.Details] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 35 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.model.Details] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1308)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 37 more


However, when I do the same auto-wiring in the Caller class, it seems to work fine. I'm new to Spring, so I might be missing something here.


Thanks.


Aucun commentaire:

Enregistrer un commentaire