Spring Autowiring Service doesn't work in my Controller

前端 未结 7 1975
时光说笑
时光说笑 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:12

    On first glance the config seems ok, yet there may be some smaller tripwires that might be not that obvious.

    a) implemented UserService interface, is it the same as the controller needs? Dumb question, I know, but just be on the safe side.

    b) bean name: Try eradicating the value-value (ba-da-tush) from the @Service annotation, its superflous anyway. Or be more specific with the help of an @Qualifier.

    c) package scanning: Double check if your implemented service is really within es.unican.meteo. Sometimes its the small things.

    0 讨论(0)
  • 2021-02-04 14:12

    Add @Component annotation on your service. It should work fine

    0 讨论(0)
  • 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?

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            WEB-INF/app-config.xml
        </param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    

    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 <mvc: context from there.

    You are also registering your controllers in the root context via the <context:component-scan> 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).

    0 讨论(0)
  • 2021-02-04 14:29

    You need to change the way you have autowired the service in the controller.

    Change the following code

    @Autowired
    private UserService userService;
    

    with following

    @Resource(name="userService")
    private UserService userService;
    

    Because in the UserServiceImpl you have defined the @Service annotation with alias "userService".

    I hope this would resolve your problem. :)

    0 讨论(0)
  • 2021-02-04 14:31

    Make sure that your UserServiceImpl is in same package as defined in context:component-scan. If it's not, spring will not be able to detect it. Also, try removing value attribute from UserServiceImpl definition, since there is only 1 bean of that type. Spring will be able to autowire it by type.

    0 讨论(0)
  • 2021-02-04 14:35

    when ever you face such kind of problem kindly check, what is the path for context:component-scan basepackage

    it should be root name Like if I am taking com.mike as package name & which contain bean,controller,dao,service folder in its structure then, in such condition you have to follow Like ----context:component-scan basepackaage="com.mike.*"

    where * means all the folder (bean,service,dao,controller and theie corresponding classes) will be scaned.

    0 讨论(0)
提交回复
热议问题