我创建了一个自定义存储库。在这个存储库中,我需要访问标准存储库(findAll)的一些方法,所以我添加了
我用过春季靴子
@EntityScan(basePackageClasses = {UserAgendaApplication.class, Jsr310JpaConverters.class})
@SpringBootApplication
public class UserAgendaApplication {
public static void main(String[] args) {
SpringApplication.run(UserAgendaApplication.class, args);
}
}
public interface UsersRepositoryCustom {
public Page<Users> customSearch(UserSearch UserSearch, Pageable page);
}
public interface UsersRepository extends JpaRepository<Users, Integer>, JpaSpecificationExecutor<Users>, UsersRepositoryCustom {
}
@Repository
public class UsersRepositoryImpl implements UsersRepositoryCustom {
@PersistenceContext
private EntityManager em;
private final UsersRepository usersRepository;
@Autowired
public UsersRepositoryImpl(final UsersRepository usersRepository){
this.usersRepository=usersRepository;
}
@Override
public Page<Users> customSearch(UserSearch UserSearch, Pageable page) {
...
return usersRepository.findAll(specification, page);
}
}
@Service
public class UsersServiceImpl implements UsersService {
@Autowired
public UsersServiceImpl(UsersRepository usersRepository) {
this.usersRepository = usersRepository;
}
private UsersRepository usersRepository;
@Override
public Page<Users> customSearch(UserSearch userSearch, Pageable page) {
usersRepository.customSearch(userSearch, page);
...
}
}
在
的控制器中@Controller
public class UsersController {
private final UsersService usersService;
@Autowired
public UsersController( final usersService usersService){
this.usersService=usersService;
}
}
org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为'usersController'的bean时出错:不满意 通过字段'usersService'表示的依赖;
嵌套异常 是org.springframework.beans.factory.UnsatisfiedDependencyException: 创建在文件中定义名为'usersServiceImpl'的bean时出错 [/home/alpha/Development/project/UserAgenda/build/classes/main/com/alpha/userAgenda/service/UsersServiceImpl.class]: 通过构造函数参数0表示的不满意的依赖性; 嵌套异常是 org.springframework.beans.factory.BeanCurrentlyInCreationException: 创建名为'usersRepositoryImpl'的bean时出错:名称为Bean 'usersRepositoryImpl'已注入其他bean [usersRepository]在其原始版本中作为循环引用的一部分, 但最终被包裹了。这意味着所说的其他豆子 不要使用bean的最终版本。这往往是结果 过度的类型匹配 - 考虑使用'getBeanNamesOfType' 例如,'allowEagerInit'标志已关闭。
我试图在控制器和UsersRepositoryImpl中将setUsersRepository置于自动装配但是得到相同的错误
修改
public abstract class UsersRepositoryCustom extends SimpleJpaRepository<Users, Integer>
public Page<Users> customSearch(UserSearch userSearch, Pageable page) {
...
}
因为我使用SimpleJpaRepository,我不再需要UserRepository了,但是我需要在UersRepositoryCustom中添加这段代码
private final EntityManager entityManager;
private final JpaEntityInformation<Users, Integer> entityInformation;
@Autowired
public UsersRepositoryCustom(final JpaEntityInformation<Users, Integer> entityInformation,
final EntityManager entityManager) {
super(entityInformation, entityManager);
this.entityManager = entityManager;
this.entityInformation = entityInformation;
}
在我实现UsersService的UsersServiceImpl中,我有
@Autowired
public UsersRepositoryCustom usersRepositoryCustom;
我得到了
.UsersRepositoryImpl中构造函数的参数0需要一个无法找到的类型为'org.springframework.data.jpa.repository.support.JpaEntityInformation'的bean。
编辑2
gradle config
buildscript {
ext {
springBootVersion = '2.0.0.M2'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'application'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
mainClassName = 'com.alpha.useragenda.UserAgendaApplication'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.springframework.boot:spring-boot-starter-web')
compile group: 'org.hsqldb', name: 'hsqldb', version: '2.0.0'
compile group: 'org.postgresql', name: 'postgresql', version: '42.1.1'
compile group: 'org.webjars', name: 'bootstrap', version: '3.3.7'
compile group: 'org.webjars.bower', name: 'bootstrap-table', version: '1.11.1'
compile('org.springframework.boot:spring-boot-devtools')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
tasks.withType(JavaCompile) {
options.compilerArgs = ["-Xlint:unchecked", "-Xlint:deprecation", "-parameters"]
}
答案 0 :(得分:0)
我认为你最好的选择就是使用这个
package stack.overflow;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
public interface UsersCrud extends CrudRepository<Users, Long> {
//My other query prototypes here
@Query("select c1 from t1")
public Object customQuery();
}
通过这种方式,您可以轻松获得特殊查询。否则,我建议遵循OP评论中给出的建议(单独留下存储库并处理服务中的特殊情况