没有匹配的types的bean …发现依赖
经过几天的尝试,并在springource论坛上等待的答案,我会在这里尝试。 运行我的应用程序导致这些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)
这是相关的代码
应用上下文:
<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 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 ); } }
多件事情可能会导致这种情况,我没有麻烦检查您的整个存储库,所以我在这里出去。
首先,如果您使用注释进行configuration,则可能会缺lesscom.example.my.services.user.UserService
实现的注释(@Service或@Component)。 如果您使用(仅)xml,则可能会遗漏UserService实现的<bean>
定义。
如果您使用注释并且实现注释正确,请检查扫描实现所在的包(检查<context:component-scan base-package=
-value)。
添加这个给你applicationContext:
<bean id="userService" class="com.example.my.services.user.UserServiceImpl ">
将注释@Repository添加到userDao类的头部。如果userDao是一个接口,则将此注释添加到接口的实现中。
由于使用AOP,我在testingconfiguration中遇到类似的麻烦。 我在spring-config.xml中添加了这行代码
<aop:config proxy-target-class="true"/>
它的工作原理!
我有一个类似的问题,但我错过了(@Service或@Component)的实现com.example.my.services.myUser.MyUserServiceImpl
如果这只发生在部署上,请确保您具有在.war中引用的包的依赖关系。 例如,这在我的机器上本地工作,debuggingconfiguration工作正常,但是在部署到Amazon的Elastic Beanstalk之后,我收到了这个错误,并注意到.war包中没有捆绑其中一个依赖关系。
在我的情况下,这是CrudRepository错误的依赖。 我的IDE还添加了以下内容:
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-commons</artifactId> <version>1.11.2.RELEASE</version> </dependency>
但我只需要:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>RELEASE</version> </dependency>
我删除了第一个,一切都很好。
我有同样的问题,但在我的情况下,由于自动assembly失败,实施的class级意外地变得“抽象”。
- 用Hibernate + Spring进行caching – 一些问题!
- 为什么不使用Spring的OpenEntityManagerInViewFilter
- 没有Hibernate Session绑定到线程,configuration不允许在这里创build非事务性的
- Hibernate 4.1Final替代Hibernate.STRING
- 我如何从jackson的一个自定义反序列化器中调用默认的反序列化器
- org.hibernate.HibernateException:当“hibernate.dialect”未设置时,对DialectResolutionInfo的访问不能为null
- Hibernate的两个configuration文件的目的是什么?
- JPA @OneToMany – >父 – 子参考(外键)
- 如何将Spring与Hibernate会话和事务pipe理集成?