Beancreationexception+NosuchBeandefinition异常

Beancreationexception+NosuchBeandefinition exception

我正在使用 SpringBoot 开发 Spring 4 应用程序。

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

@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 包中,
用户 Class:

@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<User, Serializable> gr;

 public User saveEntity( User usr )
 {
    return gr.save(usr);
 }

 public String getUser()
 {
    //Get User logic
 }  
}  

GenericRepository.java:

@NoRepositoryBean
public interface GenericRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID> {
    public List<T> findByNamedQuery( String name );
    public List<T> findByNamedQueryAndParams( String name, Map<String, Object> 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,如 documentation 中所示。

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