Spring Autowiring Service doesn't work in my Controller

前端 未结 7 1979
时光说笑
时光说笑 2021-02-04 13:43

i\'ve problems in order to autowire a service in my controller. I\'ve this error:

org.springframework.beans.factory.BeanCreationException: Error creating bean wi         


        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-04 14:29

    Your configuration is very strange...

    First rule out the obvious

    I don't see root web application context configuration in your web.xml. Could it be that you forgot to add this piece of code?

    
        contextConfigLocation
        
            WEB-INF/app-config.xml
        
    
    
    
        org.springframework.web.context.ContextLoaderListener
    
    

    Now a little bit of theory

    Bit of Spring theory - Spring uses application context hierarchy for web applications:

    • top level web application context is loaded by ContextLoaderListener
    • then there are separate contexts for each DispatcherServlet instances

    When a new bean is being instantiated, it can get dependencies either from the context where it is being defined or from parent context. This makes possible to define common beans in the root context (services, DAO, ...) and have the request handling beans in servlet application contexts as each servlet can have its own set of controllers, view handers, ...

    Last, but not least - your errors

    You are configuring MVC in your root context. That is just wrong. Remove the context from there.

    You are also registering your controllers in the root context via the on your base package. Make the component scan just on the services package or separate your classes into two top level packages core (for the root beans) and servlet (for servlet beans).

提交回复
热议问题