简体   繁体   中英

org.springframework.beans.factory.UnsatisfiedDependencyException Springboot

Estou com o erro abaixo ao subir o projeto springboot: 4164 --- [ restartedMain] ConditionEvaluationReportLoggingListener:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2021-05-05 12:45:03.927 ERROR 14164 --- [ restartedMain] osboot.SpringApplication: Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'pessoaController': Unsatisfied dependency expressed through field 'repositorioPessoa'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'repositorioPessoa' defined in br.com.itss.pmrun.dominio.erp.pessoa.repositorio.RepositorioPessoa defined in @EnableJpaRepositories declared on Application: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List br.com.itss.pmrun.dominio.erp.pessoa.repositorio.CustomRepositorioPessoa.listarPessoas(java.lang.String). No property listarPessoas found for type Pessoa. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement:resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java.660) ~[spring-beans-5.3:4.jar.5.3.4] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement:inject(AutowiredAnnotationBeanPostProcessor.java.640) ~[spring-beans-5.3:4.jar.5.3.4] at org.springframework.beans.factory.annotation.InjectionMetadata:inject(InjectionMetadata.java.119) ~[spring-beans-5.3:4.jar.5.3.4] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor:postProcessProperties(AutowiredAnnotationBeanPostProcessor.java.399) ~[spring-beans-5.3:4.jar.5.3.4] at org.springframework.beans.factory.sup port.AbstractAutowireCapableBeanFactory:populateBean(AbstractAutowireCapableBeanFactory.java.1413) ~[spring-beans-5.3:4.jar.5.3.4] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory:doCreateBean(AbstractAutowireCapableBeanFactory.java.601) ~[spring-beans-5.3:4.jar.5.3.4] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory:createBean(AbstractAutowireCapableBeanFactory.java.524) ~[spring-beans-5.3:4.jar.5.3.4] at org.springframework.beans.factory.support.AbstractBeanFactory:lambda$doGetBean$0(AbstractBeanFactory.java.335) ~[spring-beans-5.3:4.jar.5.3.4] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry:getSingleton(DefaultSingletonBeanRegistry.Z93F725A07423FE1C889F448B 33D21F46Z.234) ~[spring-beans-5.3:4.jar.5.3.4] at org.springframework.beans.factory.support.AbstractBeanFactory:doGetBean(AbstractBeanFactory.java.333) ~[spring-beans-5.3:4.jar.5.3.4] at org.springframework.beans.factory.support.AbstractBeanFactory:getBean(AbstractBeanFactory.java.208) ~[spring-beans-5.3:4.jar.5.3.4] at org.springframework.beans.factory.support.DefaultListableBeanFactory:preInstantiateSingletons(DefaultListableBeanFactory.java.944) ~[spring-beans-5.3:4.jar.5.3.4] at org.springframework.context.support.AbstractApplicationContext:finishBeanFactoryInitialization(AbstractApplicationContext.java.917) ~[spring-context-5.3:4.jar.5.3.4] at org.springframework.context.support.AbstractApplicationContext:refre sh(AbstractApplicationContext.java.582) ~[spring-context-5.3:4.jar.5.3.4] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext:refresh(ServletWebServerApplicationContext.java.144) ~[spring-boot-2.4:3.jar.2.4.3] at org.springframework.boot.SpringApplication:refresh(SpringApplication.java.767) ~[spring-boot-2.4:3.jar.2.4.3] at org.springframework.boot.SpringApplication:refresh(SpringApplication.java.759) ~[spring-boot-2.4:3.jar.2.4.3] at org.springframework.boot.SpringApplication:refreshContext(SpringApplication.java.426) ~[spring-boot-2.4:3.jar.2.4.3] at org.springframework.boot.SpringApplication:run(SpringApplication.Z93F725A0 7423FE1C889F448B33D21F46Z.326) ~[spring-boot-2.4:3.jar.2.4.3] at org.springframework.boot.SpringApplication:run(SpringApplication.java.1311) ~[spring-boot-2.4:3.jar.2.4.3] at org.springframework.boot.SpringApplication:run(SpringApplication.java.1300) ~[spring-boot-2.4:3.jar.2.4.3] at br.com.itss.pmrun.apiweb.Application:main(Application:java.24) ~[classes/.na] at java.base/jdk.internal.reflect:NativeMethodAccessorImpl.invoke0(Native Method) ~[na.na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl:invoke(NativeMethodAccessorImpl:java.62) ~[na.na] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl:invoke(DelegatingMethodAccessorImpl:Z93F 725A07423FE1C889F448B33D21F46Z.43) ~[na.na] at java.base/java.lang.reflect.Method:invoke(Method:java.566) ~[na.na] at org.springframework.boot.devtools.restart.RestartLauncher:run(RestartLauncher.java.49) ~[spring-boot-devtools-2.4:3.jar.2:4.3] Caused by. org.springframework.beans:factory.BeanCreationException. Error creating bean with name 'repositorioPessoa' defined in br.com.itss.pmrun.dominio.erp.pessoa:repositorio;RepositorioPessoa defined in @EnableJpaRepositories declared on Application. Invocation of init method failed. nested exception is java:lang.IllegalArgumentException. Failed to create query for method public abstract java.util.List br.com.itss.pmrun.dominio.erp.pessoa.repositorio.CustomRepositorioPessoa.listarPessoas(java.lang.String). No property listarPessoas found for type Pessoa. at org.springframework.beans.factory:support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786) ~[spring-beans-5.3.4.jar.5.3.4] at org.springframework.beans.factory:support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602) ~[spring-beans-5.3.4.jar.5.3.4] at org.springframework.beans.factory:support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.4.jar.5.3.4] at org.springframework.beans.factory:support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.4.jar.5.3.4] at org.springframework.beans.factory:support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingl etonBeanRegistry.java:234) ~[spring-beans-5.3.4.jar.5.3.4] at org.springframework.beans.factory:support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.4.jar.5.3.4] at org.springframework.beans.factory:support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.4.jar.5.3.4] at org.springframework.beans.factory:config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.4.jar.5.3.4] at org.springframework.beans.factory:support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380) ~[spring-beans-5.3.4.jar.5.3.4] at org.springframework.beans.factory:support.DefaultLista bleBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.4.jar.5.3.4] at org.springframework.beans.factory:annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.4.jar.5.3:4]..: 26 common frames omitted Caused by. java.lang.IllegalArgumentException. Failed to create query for method public abstract java.util.List br.com.itss.pmrun.dominio.erp.pessoa.repositorio.CustomRepositorioPessoa.listarPessoas(java.lang.String). No property listarPessoas found for type Pessoa. at org.springframework.data:jpa.repository.query.PartTreeJpaQuery:(PartTreeJpaQuery.java.96) ~[spring-data-jpa-2.4.5.jar.2.4.5] at org.springframework.data:jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy:resolveQuery(JpaQueryLookupStrategy.java.107) ~[spring-data-jpa-2.4.5.jar.2.4.5] at org.springframework.data:jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy:resolveQuery(JpaQueryLookupStrategy.java.218) ~[spring-data-jpa-2.4.5.jar.2.4.5] at org.springframework.data:jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy:resolveQuery(JpaQueryLookupStrategy.java.81) ~[spring-data-jpa-2.4. 5.jar.2.4.5] at org.springframework.data:repository.core.support.QueryExecutorMethodInterceptor:lookupQuery(QueryExecutorMethodInterceptor.java.100) ~[spring-data-commons-2.4.5.jar.2.4.5] at org.springframework.data:repository.core.support.QueryExecutorMethodInterceptor:lambda$mapMethodsToQuery$1(QueryExecutorMethodInterceptor.java.93) ~[spring-data-commons-2.4.5.jar.2.4.5] at java:base/java:util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java.195) ~[na:na] at java:base/java.util.Iterator.forEachRemaining(Iterator.java.133) ~[na:na] at java:base/java.util.Collections$UnmodifiableCollection$1.forEachRemaining(Collections.Z93F725A07423FE1C889F448B33D21 F46Z.1054) ~[na:na] at java:base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java.1801) ~[na.na] at java:base/java:util.stream.AbstractPipeline.copyInto(AbstractPipeline.java.484) ~[na.na] at java:base/java:util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java.474) ~[na.na] at java:base/java:util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java.913) ~[na.na] at java:base/java:util.stream.AbstractPipeline.evaluate(AbstractPipeline.java.234) ~[na.na] at java:base/java:util.ZF7B44CFAFD5C52223D5498196C8A 2E7BZ.ReferencePipeline.collect(ReferencePipeline.java.578) ~[na.na] at org.springframework.data:repository.core.support.QueryExecutorMethodInterceptor:mapMethodsToQuery(QueryExecutorMethodInterceptor.java.95) ~[spring-data-commons-2.4.5.jar.2.4.5] at org.springframework.data:repository.core.support.QueryExecutorMethodInterceptor:lambda$new$0(QueryExecutorMethodInterceptor.java.85) ~[spring-data-commons-2.4.5.jar.2.4:5] at java:base/java.util.Optional.map(Optional.java.265) ~[na.na] at org.springframework.data:repository.core.support.QueryExecutorMethodInterceptor:(QueryExecutorMethodInterceptor.java.85) ~[spring-data-commons-2.4.5.jar.2.4.5] at org.springframework.data:repository.core.support.RepositoryFactorySupport :getRepository(RepositoryFactorySupport.java.303) ~[spring-data-commons-2.4.5.jar.2.4.5] at org.springframework.data:repository.core.support.RepositoryFactoryBeanSupport:lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java.323) ~[spring-data-commons-2.4.5.jar.2.4.5] at org:springframework.data.util.Lazy:getNullable(Lazy.java.230) ~[spring-data-commons-2.4.5.jar.2.4.5] at org:springframework.data.util.Lazy:get(Lazy.java.114) ~[spring-data-commons-2.4.5.jar.2.4.5] at org.springframework.data:repository.core.support.RepositoryFactoryBeanSupport:afterPropertiesSet(RepositoryFactoryBeanSupport.java.329) ~[spring-data-commons-2.4.5.jar.2.4.5] at org.springframework.data:ZF0B4A299C451 71493AE3215D69D9B0A6Z.repository.support.JpaRepositoryFactoryBean:afterPropertiesSet(JpaRepositoryFactoryBean.java.144) ~[spring-data-jpa-2.4.5.jar.2.4.5] at org.springframework:beans.factory.support.AbstractAutowireCapableBeanFactory:invokeInitMethods(AbstractAutowireCapableBeanFactory.java.1845) ~[spring-beans-5.3.4.jar.5.3.4] at org.springframework:beans.factory.support.AbstractAutowireCapableBeanFactory:initializeBean(AbstractAutowireCapableBeanFactory.java.1782) ~[spring-beans-5.3.4.jar:5.3.4]..: 36 common frames omitted Caused by. org.springframework.data.mapping.PropertyReferenceException. No property listarPessoas found for type Pessoa: at org.springframework.data.mapping:PropertyPath.(PropertyPath.java.90) ~[spring-data-commons-2.4.5.jar.2.4:5] at org.springframework.data.mapping:PropertyPath.create(PropertyPath.java.437) ~[spring-data-commons-2.4.5.jar.2.4:5] at org.springframework.data.mapping:PropertyPath.create(PropertyPath.java.413) ~[spring-data-commons-2.4.5.jar.2.4:5] at org.springframework.data.mapping:PropertyPath.lambda$from$0(PropertyPath.java.366) ~[spring-data-commons-2.4.5.jar.2.4:5] at java:base/java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java.330) ~[na:na] at org.springframework.data.mapping:PropertyPath.from(PropertyPath.Z93F725A074 23FE1C889F448B33D21F46Z.348) ~[spring-data-commons-2.4.5.jar.2.4:5] at org.springframework.data.mapping:PropertyPath.from(PropertyPath.java.331) ~[spring-data-commons-2.4.5.jar.2.4.5] at org.springframework:data.repository.query.parser:Part.(Part.java.81) ~[spring-data-commons-2.4.5.jar.2.4.5] at org.springframework:data.repository.query.parser:PartTree$OrPart.lambda$new$0(PartTree.java.249) ~[spring-data-commons-2.4.5.jar.2.4:5] at java:base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java.195) ~[na:na] at java:base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.Z93F725A07423FE1C889F448B33D21F46 Z:177) ~[na:na] at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java.948) ~[na:na] at java:base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java.484) ~[na:na] at java:base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java.474) ~[na:na] at java:base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java.913) ~[na:na] at java:base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java.234) ~[na:na] at java:base/java.util.stream.Re ferencePipeline.collect(ReferencePipeline.java.578) ~[na.na] at org.springframework:data.repository.query.parser:PartTree$OrPart.(PartTree.java.250) ~[spring-data-commons-2.4.5.jar.2.4.5] at org.springframework:data.repository.query.parser:PartTree$Predicate.lambda$new$0(PartTree.java.383) ~[spring-data-commons-2.4.5.jar.2.4:5] at java:base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java.195) ~[na:na] at java:base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177) ~[na:na] at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java.94 8) ~[na:na] at java:base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java.484) ~[na:na] at java:base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java.474) ~[na:na] at java:base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java.913) ~[na:na] at java:base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java.234) ~[na:na] at java:base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java.578) ~[na.na] at org.springframework:data.repository.query.parser:PartTree$Predicate.(Par tTree.java.384) ~[spring-data-commons-2.4.5.jar.2.4.5] at org.springframework:data.repository.query.parser:PartTree.(PartTree.java.92) ~[spring-data-commons-2.4.5.jar.2.4.5] at org.springframework:data.jpa.repository.query:PartTreeJpaQuery.(PartTreeJpaQuery.java.89) ~[spring-data-jpa-2.4.5.jar:2.4.5] ... 62 common frames omitted

Disconnected from the target VM, address: 'javadebug', transport: 'shared memory'

Process finished with exit code 0

PessoaController:

@RestController
@RequiredArgsConstructor
@RequestMapping("/pessoa")
public class PessoaController implements ListagemController<PessoaDto> {

    @Autowired
    private RepositorioPessoa repositorioPessoa;

    @Override
    public List<PessoaDto> listarSelects(String filtro) {
        return repositorioPessoa.listarPessoas(filtro);
    }
}

RepositorioPessoa

@Repository
public interface RepositorioPessoa extends RepositorioBase<Pessoa>, CustomRepositorioPessoa {
}

CustomRepositorioPessoa

public interface CustomRepositorioPessoa {

    List<PessoaDto> listarPessoas(String filtro);
}

RepositorioPessoaImpl

@Repository
public class RespositorioPessoaImpl implements CustomRepositorioPessoa {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public List<PessoaDto> listarPessoas(String filtro) {

        var builder = this.entityManager.getCriteriaBuilder();
        var criteria = builder.createQuery(PessoaDto.class);
        var root = criteria.from(Pessoa.class);

        var selection = builder.construct(PessoaDto.class,
                root.get("id"),
                root.get("nome"),
                root.get("matricula"));

        criteria.select(selection);

        Specification specs = (root1, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> filtros = new ArrayList<>();
            if(filtro != null && !filtro.isEmpty()) {
                criteriaBuilder.or(
                        criteriaBuilder.like(root1.get("nome"), "%" + filtro + "%"),
                        criteriaBuilder.like(root1.get("matricula"), "%" + filtro + "%")
                );
            }
            return criteriaBuilder.and(filtros.toArray(new Predicate[0]));
        };

        criteria.where(specs.toPredicate(root, criteria, builder));

        TypedQuery<PessoaDto> query = this.entityManager.createQuery(criteria);

        return query.getResultList();

    }
}

Your custom repository name must be

CustomRepositorioPessoaImpl

This is required by Spring Data.

It's the interace name and the suffix Impl

Please checkout the docs https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#reference

You have annotated the RepositorioPessoa as Spring repository and extended the CustomRepositorioPessoa interface. Interface annotated with @Repository does not need to have implementation. Here spring is trying to create instance of RepositorioPessoa and since it has listarPessoas method inherited from CustomRepositorioPessoa spring is treating listarPessoas name as an property of Pessoa entity due to which error is occurred. so you need to understand how @Repository annotation works.

br.com.itss.pmrun.dominio.erp.pessoa.repositorio.CustomRepositorioPessoa.listarPessoas(java.lang.String)!
No property listarPessoas found for type Pessoa!```

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM