我正在使用 Spring Boot 开发 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 中
用户服务.类:
@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) }
- 没有我定义的名为 userService 的变量,但它仍然显示错误。
- 无论如何我都可以解决上述问题。