I created a custom repository. In this repository, i need to access some method of standard repository (findAll), so I added
I used spring boot
@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);
...
}
}
In a controller for a
@Controller
public class UsersController {
private final UsersService usersService;
@Autowired
public UsersController( final usersService usersService){
this.usersService=usersService;
}
}
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'usersController': Unsatisfied dependency expressed through field 'usersService';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'usersServiceImpl' defined in file [/home/alpha/Development/project/UserAgenda/build/classes/main/com/alpha/userAgenda/service/UsersServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'usersRepositoryImpl': Bean with name 'usersRepositoryImpl' has been injected into other beans [usersRepository] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
I tried to put a autowired on setUsersRepository in the controller and UsersRepositoryImpl but get the same error
Edit
public abstract class UsersRepositoryCustom extends SimpleJpaRepository<Users, Integer>
public Page<Users> customSearch(UserSearch userSearch, Pageable page) {
...
}
Because i use SimpleJpaRepository, I don't need anymore UserRepository, but i need to add in UersRepositoryCustom this code
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;
}
In my UsersServiceImpl who implement UsersService, i have
@Autowired
public UsersRepositoryCustom usersRepositoryCustom;
I get
Parameter 0 of constructor in .UsersRepositoryImpl required a bean of type 'org.springframework.data.jpa.repository.support.JpaEntityInformation' that could not be found.
Edit 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"]
}
I think your best shot is to use this
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();
}
This way you get the special query with minimal effort. Otherwise I suggest to follow the advice given in the OP comments (leave repository alone and deal with special case in service
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.