Hi, I am trying to figure out why I cannot access a bean from the root-context.xml in the servlet-context.xml. I have a very simple spring mvc app (using spring 3.1.1.RELEASE) and am running into an issue where a field inside a controller annotated with
@Autowired
private TaskServiceImpl taskService;
throws
org.springframework.beans.factory.NoSuchBeanDefini tionException
Checking the log for the servlet-context.xml, where the homeController resides gives:
Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultL istableBeanFactory@1e6cecc:
defining beans [
org.springframework.web.servlet.mvc.method.annotat ion.RequestMappingHandlerMapping#0,
org.springframework.format.support.FormattingConve rsionServiceFactoryBean#0,
org.springframework.validation.beanvalidation.Loca lValidatorFactoryBean#0,
org.springframework.web.servlet.mvc.method.annotat ion.RequestMappingHandlerAdapter#0,
org.springframework.web.servlet.handler.MappedInte rceptor#0,
org.springframework.web.servlet.mvc.method.annotat ion.ExceptionHandlerExceptionResolver#0,
org.springframework.web.servlet.mvc.annotation.Res ponseStatusExceptionResolver#0,
org.springframework.web.servlet.mvc.support.Defaul tHandlerExceptionResolver#0,
org.springframework.web.servlet.handler.BeanNameUr lHandlerMapping,
org.springframework.web.servlet.mvc.HttpRequestHan dlerAdapter,
org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter,
org.springframework.web.servlet.resource.ResourceH ttpRequestHandler#0,
org.springframework.web.servlet.handler.SimpleUrlH andlerMapping#0,
org.springframework.web.servlet.view.InternalResou rceViewResolver#0,
homeController,
org.springframework.context.annotation.internalCon figurationAnnotationProcessor,
org.springframework.context.annotation.internalAut owiredAnnotationProcessor,
org.springframework.context.annotation.internalReq uiredAnnotationProcessor,
org.springframework.context.annotation.internalCom monAnnotationProcessor,
org.springframework.context.annotation.internalPer sistenceAnnotationProcessor,
org.springframework.context.annotation.Configurati onClassPostProcessor$ImportAwareBeanPostProcessor# 0];
parent: org.springframework.beans.factory.support.DefaultL istableBeanFactory@166de66
And the log for the parent root-context.xml:
Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultL istableBeanFactory@166de66:
defining beans [
dataSource,
transactionManager,
org.springframework.aop.config.internalAutoProxyCr eator,
org.springframework.transaction.annotation.Annotat ionTransactionAttributeSource#0,
org.springframework.transaction.interceptor.Transa ctionInterceptor#0,
org.springframework.transaction.config.internalTra nsactionAdvisor,
emf,
taskService]; root of factory hierarchy
As can be seen, the root context contains the taskService bean that is needed for autowiring and it is correctly configured. I have checked this by moving the taskService to the servlet-context.xml, and everything works as expected in that case.
My question is can I inject beans into controllers from the root-context? I thought I could do so, but based on this example it seems that it does not work. Can anybody help me figure out what I may be doing wrong? I am actually more interested in the reasoning behind it than finding an alternative, as I have mentioned this issue goes away if i move the bean in the servlet-context.xml.


Reply With Quote
) mistake in using the implementation class, but I was not aware that a context can only inherit beans from a parent context by interface, and not the actual defined class.
