No matching bean type ... found for dependency

after several days of trying and waiting for answers in the springsource forums, I will try it here. Running my application results in this exception:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.example.my.services.user.UserService] 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)} org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924) org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793) org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707) org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478) org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631) org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588) org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645) org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508) org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449) org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133) javax.servlet.GenericServlet.init(GenericServlet.java:212) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602) org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) java.lang.Thread.run(Thread.java:662) 

Here is the corresponding code

application context:

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.postgresql.Driver" /> <property name="url" value="jdbc:postgresql://localhost:5432/test" /> <property name="username" value="test" /> <property name="password" value="test" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="com.example.my.entities.*" /> <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <tx:annotation-driven /> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> 

com.example.my.entities.user:

 @Entity @Table( name = "tbl_users" ) public class User { @Id @Column( name = "id" ) @GeneratedValue private int id; @Column( name = "username" ) private String username; @Column( name = "password" ) private String password; public void setId( int id ) { this.id = id; } public int getId() { return id; } public void setUsername( String username ) { this.username = username; } public String getUsername() { return username; } public void setPassword( String password ) { this.password = password; } public String getPassword() { return password; } } 

service:

 @Service public class UserServiceImpl implements UserService { @Autowired private UserDAO userDAO; @Override @Transactional public void addUser( User user ) { userDAO.addUser( user ); } @Override @Transactional public List<User> listUsers() { return userDAO.listUsers(); } @Override @Transactional public void removeUser( int id ) { userDAO.removeUser( id ); } } 
+56
java spring spring-mvc hibernate autowired
Jan 22 2018-12-22T00:
source share
8 answers

This can lead to several things, I did not check your entire repository, so I go out on a limb here.

First, you can lose the annotation (@Service or @Component) from the implementation of com.example.my.services.user.UserService if you use annotations for customization. If you are using (only) xml, you probably lack the <bean> -definition to implement the UserService.

If you use annotations and the implementation is annotated correctly, check that the package in which the implementation is located is checked (check your <context:component-scan base-package= -value).

+118
Jan 22 '12 at 13:33
source share

Add this to you applicationContext:

  <bean id="userService" class="com.example.my.services.user.UserServiceImpl "> 
+13
Jan 22 2018-12-22T00:
source share

Add the @Repository annotation to the chapter userDao Class.If userDao is the interface, add this annotation to the interface tools.

+10
Jul 18 '12 at 3:15
source share

I have a similar problem in a test config due to the use of AOP. I added this line of code in spring -config.xml

 <aop:config proxy-target-class="true"/> 

And it works!

+6
Dec 23 '13 at 13:24
source share

I had a similar problem but I was missing (@Service or @Component) from the implementation of com.example.my.services.myUser.MyUserServiceImpl

+1
02 Oct '12 at 20:25
source share

IF this only happens during deployment, make sure you have a dependency on the package you are referencing in .war. For example, this worked locally on my machine, and the debug configurations worked fine, but after deploying to Amazon Elastic Beanstalk, I received this error and noticed that one of the dependencies was not included in the .war package.

+1
Jul 15 '14 at 22:37
source share

In my case, it was the wrong dependency for CrudRepository. My IDE is also added:

  <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> <version>1.11.2.RELEASE</version> </dependency> 

But I just need to:

  <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>RELEASE</version> </dependency> 

I deleted the first one and everything was fine.

+1
May 09 '17 at 9:05 a.m.
source share

I had the same problem, but in my case, the implemented class accidentally became "abstract", as a result of which outsourcing was not possible.

0
Nov 21 '13 at 7:08
source share



All Articles