Beancreationexception+NosuchBeandefinition exception

自闭症网瘾萝莉.ら 提交于 2019-12-19 09:46:41

问题


I am working on Spring 4 application with SpringBoot.

In com.test.tm package,
Application class:

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

In com.test.tm.entities package,
User Class:

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

In 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 );
 }  

There is a GenericRepositoryImpl.java as well where logic for above methods are implemented.

On running Application.java, I am getting following error:

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. There is no such variable called userService which I defined, but still it shows error.
  2. Any way I can fix the above issue.

回答1:


Ad. 1. Spring creates userService bean with default name, as indicated in documentation.

Ad. 2. I'm not sue, sure, but perhaps GenericRepositoryImpl is outside of com.test.tm package? If yes, then specify additional @ComponentScan annotation with proper package declaration ie.@ComponentScan("com.test.utils"), or - if You use Boot 1.3+ - modify @SpringBootApplication(scanBasePackages={"com.test.utils","com.test.tm"})



来源:https://stackoverflow.com/questions/33494346/beancreationexceptionnosuchbeandefinition-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!