我正在使用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) }发布于 2015-11-03 12:05:54
广告。1. Spring创建默认名称的userService bean,如文档中所示。
广告。2.我不起诉,当然,但也许GenericRepositoryImpl不在com.test.tm包之外?如果是,则使用正确的包声明(即.@ComponentScan("com.test.utils"))指定附加的@ComponentScan("com.test.utils")注释,或者--如果使用引导1.3+ --修改@SpringBootApplication(scanBasePackages={"com.test.utils","com.test.tm"})
https://stackoverflow.com/questions/33494346
复制相似问题