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
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.
Add @Component annotation on your service. It should work fine
Your configuration is very strange...
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>
Bit of Spring theory - Spring uses application context hierarchy for web applications:
ContextLoaderListener
DispatcherServlet
instancesWhen 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, ...
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).
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. :)
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.
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.