Beancreationexception + NosuchBeandefinitionexception

我正在使用SpringBoot处理Spring 4应用程序。

在com.test.tm包中,
申请类:

@SpringBootApplication @EnableJpaRepositories( repositoryFactoryBeanClass = GenericRepositoryFactoryBean.class ) @Import( { HikariDataSourceConfig.class } ) public class Application { public static void main( String[] args ) { SpringApplication.run(Application.class, args); } } 

在com.test.tm.entities包中,
用户类:

 @Table( name = "test.user" ) @Entity public class User implements Serializable { @Id @GeneratedValue( strategy = GenerationType.AUTO ) private Integer id; private String message; .... } 

在com.test.tm.user中
UserService.class:

 @Service @Transactional( rollbackFor = Exception.class ) public class UserService { @Autowired private GenericRepository gr; public User saveEntity( User usr ) { return gr.save(usr); } public String getUser() { //Get User logic } } 

GenericRepository.java:

 @NoRepositoryBean public interface GenericRepository extends PagingAndSortingRepository { public List findByNamedQuery( String name ); public List findByNamedQueryAndParams( String name, Map params ); } 

还有一个GenericRepositoryImpl.java ,其中实现了上述方法的逻辑。

在运行Application.java时,我收到以下错误:

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.utils.spring.repo.GenericRepository com.test.tm.user.UserService.gr; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type[com.test.utils.spring.repo.GenericRepository] 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) } 
  1. 没有我定义的名为userService的变量,但它仍然显示错误。
  2. 我可以用任何方式解决上述问题。

广告。 1. Spring创建了具有默认名称的userService bean,如文档中所示 。

广告。 2.我当然没有起诉,但也许GenericRepositoryImpl不在com.test.tm包中? 如果是,则使用适当的包声明指定其他@ComponentScan注释,即。 @ComponentScan("com.test.utils") ,或 – 如果使用Boot 1.3+ – 修改@SpringBootApplication(scanBasePackages={"com.test.utils","com.test.tm"})