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