Beancreationexception + NosuchBeandefinition异常

时间:2015-11-03 08:21:58

标签: java spring spring-boot spring-4

我正在使用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<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 个答案:

答案 0 :(得分:1)

广告。 1。 Spring使用默认名称创建image folder not created bean,如documentation中所示。

广告。我当然没有起诉,但也许userService不在 GenericRepositoryImpl包裹?如果是,则使用适当的包声明指定其他com.test.tm注释,例如。@ComponentScan,或者 - 如果使用Boot 1.3+ - 修改@ComponentScan("com.test.utils")