[英]@Autowired and UnsatisfiedDependencyException in Spring integration test
[英]UnsatisfiedDependencyException autowired on constructor and set not working
我創建了一個自定義存儲庫 在這個存儲庫中,我需要訪問標准存儲庫(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:在文件[/ home / alpha / Development / project / UserAgenda / build / classes / main / com / alpha / userAgenda / service /中定義的名稱為'usersServiceImpl'的bean創建時出錯UsersServiceImpl.class]:通過構造函數參數0表示的不滿意的依賴項; 嵌套異常是org.springframework.beans.factory.BeanCurrentlyInCreationException:創建名為'usersRepositoryImpl'的bean時出錯:名稱為'usersRepositoryImpl'的Bean已作為循環引用的一部分注入其原始版本的其他bean [usersRepository]中,但是最終被包裹了。 這意味着所述其他bean不使用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配置
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"]
}
我認為你最好的機會就是使用它
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評論中給出的建議(單獨留下存儲庫並處理特殊情況下的服務
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.