Stack Exchange
Spring Boot / Postgres / Docker "UnsatisfiedDependencyException" and "NoSuchBeanDefinitionException:" Ignored because not a concrete top-level class:
I want to share a problem I had when trying to upload a simple application with docker connected to postgres also in docker.
looks like it's having trouble finding the BeersRepository interface.
I've tried annotations like @ComponentScan, @EnableJpaRepository etc...
EDIT: https://github.com/teodorolucasac/beerstore
This is the error log:
`
20:48:51.989 [main] INFO com.hibicode.beerstore.BeerstoreApplication - Starting BeerstoreApplication using Java 18-ea on c97aaa0419fe with PID 1 (/app/app.jar started by root in /)
20:48:51.991 [main] DEBUG com.hibicode.beerstore.BeerstoreApplication - Running with Spring Boot, Spring
20:48:51.994 [main] INFO com.hibicode.beerstore.BeerstoreApplication - No active profile set, falling back to 1 default profile: "default"
20:48:51.996 [main] DEBUG org.springframework.boot.SpringApplication - Loading source class com.hibicode.beerstore.BeerstoreApplication
20:48:52.062 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1a052a00
20:48:52.072 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
20:48:52.129 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Ignored because not a concrete top-level class: URL [jar:file:/app/app.jar!/com/hibicode/beerstore/repository/BeersRepository.class]
20:48:52.159 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/app/app.jar!/com/hibicode/beerstore/resource/BeerResource.class]
20:48:52.170 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/app/app.jar!/com/hibicode/beerstore/service/BeerService.class]
20:48:52.327 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
20:48:52.329 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
20:48:52.332 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
20:48:52.334 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
20:48:52.337 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor'
20:48:52.342 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'beerstoreApplication'
20:48:52.349 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'beerResource'
20:48:52.367 [main] WARN org.springframework.context.annotation.AnnotationConfigApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'beerResource': Unsatisfied dependency expressed through field 'beersRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.hibicode.beerstore.repository.BeersRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
20:48:52.375 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'beerResource': Unsatisfied dependency expressed through field 'beersRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.hibicode.beerstore.repository.BeersRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:659)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295)
        at com.hibicode.beerstore.BeerstoreApplication.main(BeerstoreApplication.java:17)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.hibicode.beerstore.repository.BeersRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1801)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1357)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:656)
        ... 19 common frames omitted

`
Application
`
package com.hibicode.beerstore;

@SpringBootApplication
public class BeerstoreApplication {

   public static void main(String[] args) {
      SpringApplication.run(BeerstoreApplication.class, args);
   }
}

`
Entity
`
package com.hibicode.beerstore.model;

@Entity
@Data
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class Beer {

`
Repository
`
package com.hibicode.beerstore.repository;

@Repository
@EnableJpaRepositories
public interface BeersRepository extends JpaRepository<Beer, Long> {

    Optional<Beer> findByNameAndType(String name, BeerType type);
}

`
Controller
`
package com.hibicode.beerstore.resource;

@RestController
@RequestMapping("/beers")
public class BeerResource {

`
Service
`
package com.hibicode.beerstore.service;

import java.util.Optional;

@Service
@Component
public class BeerService {

`
application.properties
the port is 5433 because my Docker is configured like this: 0.0.0.0:5433->5432/tcp, because I was having problem in docker with windows, and the only solution I found was to change the port of my machine
`
spring.datasource.url=jdbc:postgresql://localhost:5433/beerstore
spring.datasource.username=beerstore
spring.datasource.password=beerstore

spring.jpa.hibernate.temp.use_jdbc_metadata_defaults=false
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect

spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true

`
Dockerfile
PS: I was creating my project .jar through Intellij > File > Project Structure > Project Settings > Artifacts
Then Build > Build artifacts
`
FROM openjdk:18-jdk-alpine3.14

ENV LANG C.UTF-8

RUN apk add --update bash

ADD out/artifacts/beerstore_jar/*.jar /app/app.jar

CMD java -jar /app/app.jar $APP_OPTIONS

`
docker image build
docker build -t teodorolucasac/beerstore:0.3 .
docker run
docker run -p 8080:8080 --rm --network beernet -e APP_OPTIONS='--spring.datasource.url=jdbc:postgresql://beerdb:5433/beerstore' teodorolucasac/beerstore:0.3
ps: postgres is already running on docker and connected to the beernet network
Someone save me please. If any information is missing, you can ask me to post it immediately.
I tried annotations like @ComponentScan, @EnableJpaRepository etc... but it didn't work
EDIT I will post the new error message after some changes as @EnableJpaRepository.
It looks like there is some problem with entityManagerFactory.
PS: the application runs perfectly on my machine, this problem only happens when I go to docker.
new error:
`
15:07:41.192 [main] INFO com.hibicode.beerstore.BeerstoreApplication - Starting BeerstoreApplication using Java 18-ea on 755d25c6462c with PID 1 (/app/app.jar started by root in /)
15:07:41.194 [main] DEBUG com.hibicode.beerstore.BeerstoreApplication - Running with Spring Boot, Spring
15:07:41.197 [main] INFO com.hibicode.beerstore.BeerstoreApplication - No active profile set, falling back to 1 default profile: "default"
15:07:41.198 [main] DEBUG org.springframework.boot.SpringApplication - Loading source class com.hibicode.beerstore.BeerstoreApplication
15:07:41.263 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationConte
xt@1a052a00
15:07:41.271 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.inte
rnalConfigurationAnnotationProcessor'
15:07:41.343 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/app/app.jar!/com/hibicode/beersto
re/config/ApiErrorConfig.class]
15:07:41.354 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/app/app.jar!/com/hibicode/beersto
re/error/ApiExceptionHandler.class]
15:07:41.359 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/app/app.jar!/com/hibicode/beersto
re/error/GeneralExceptionHandler.class]
15:07:41.368 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Ignored because not a concrete top-level class: URL [jar:file:/app/app.jar!/com/hibico
de/beerstore/repository/BeersRepository.class]
15:07:41.388 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/app/app.jar!/com/hibicode/beersto
re/resource/BeerResource.class]
15:07:41.391 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/app/app.jar!/com/hibicode/beersto
re/service/BeerService.class]
15:07:41.509 [main] INFO org.springframework.data.repository.config.RepositoryConfigurationDelegate - Bootstrapping Spring Data JPA repositories in DEFAULT mode.
15:07:41.520 [main] DEBUG org.springframework.data.repository.config.RepositoryConfigurationDelegate - Scanning for JPA repositories in packages com.hibicode.beerstore.repository.
15:07:41.533 [main] DEBUG org.springframework.data.repository.config.RepositoryComponentProvider - Identified candidate component class: URL [jar:file:/app/app.jar!/com/hibicode/beerst
ore/repository/BeersRepository.class]
15:07:41.582 [main] INFO org.springframework.data.repository.config.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 61 ms. Found 1 JPA repository interfac
es.
15:07:41.666 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'emBeanDefinitionRegistrarPostProcessor'
15:07:41.669 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalE
ventListenerProcessor'
15:07:41.672 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalE
ventListenerFactory'
15:07:41.675 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.inte
rnalAutowiredAnnotationProcessor'
15:07:41.677 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.inte
rnalCommonAnnotationProcessor'
15:07:41.682 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.inte
rnalPersistenceAnnotationProcessor'
15:07:41.691 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'beerstoreApplication'
15:07:41.698 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'apiErrorConfig'
15:07:41.700 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'apiExceptionHandler'
15:07:41.733 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'apiErrorMessageSource'
15:07:41.753 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Autowiring by type from bean name 'apiExceptionHandler' via constructor to bean named '
apiErrorMessageSource'
15:07:41.754 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'generalExceptionHandler'
15:07:41.763 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'beerResource'
15:07:41.776 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'beersRepository'
15:07:41.838 [main] WARN org.springframework.context.annotation.AnnotationConfigApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: o
rg.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'beerResource': Unsatisfied dependency expressed through field 'beersRepository'; nested 
exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'beersRepository' defined in com.hibicode.beerstore.repository.BeersRepository defin
ed in @EnableJpaRepositories declared on BeerstoreApplication: Cannot create inner bean '(inner bean)#53f0a4cb' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while s
etting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#53f0a4cb': Cannot resolve
 reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entity
ManagerFactory' available
15:07:41.846 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'beerResource': Unsatisfied dependency expressed through field 'beersRepository'; nested
 exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'beersRepository' defined in com.hibicode.beerstore.repository.BeersRepository defi
ned in @EnableJpaRepositories declared on BeerstoreApplication: Cannot create inner bean '(inner bean)#53f0a4cb' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while 
setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#53f0a4cb': Cannot resolv
e reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entit
yManagerFactory' available
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:659)     
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295)
        at com.hibicode.beerstore.BeerstoreApplication.main(BeerstoreApplication.java:12)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'beersRepository' defined in com.hibicode.beerstore.repository.BeersRepository defined
 in @EnableJpaRepositories declared on BeerstoreApplication: Cannot create inner bean '(inner bean)#53f0a4cb' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while set
        at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1344)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:309)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
        at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:330)
        ... 40 common frames omitted

`

@EnableJpaRepositories goes on a configuration class, not a repository, and @Repository isn't needed. Show your POM: Do you have spring-boot-starter-data-jpa?

I see, I didn't create a configuration class. About this @EnableJpaRepositories annotation, it was just a test I did wrong and I forgot to remove it.
Well, I better pass the project's GitHub, right.
Beerstore project GitHub

you need @EnableJpaRepositories(basePackages = "YOUR_PACKAGE")
also make sure to add this in pom.xml
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.31</version>
</dependency>

or this
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.5.0</version>
</dependency>


hello friend, thanks for the help, but I'm still having problems.
I've updated GitHub with some changes in case you want to see the details of the project better:
https://github.com/teodorolucasac/beerstore
About your recommendations, I added @EnableJpaRepositories but postgres dependency was already present in pom.xml
Also, the error message exposed another issue about the entityManagerFactory. So I added the dependency below but to no avail.
<groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> 

don't forget to add the basePackages like com.hibicode.beerstore.repository, add enableJpaRepositories under @SpringBootApplication. I downloaded the source file from github but it has a lot of errors. try to edit your codes and if things don't work, create a pull request and I will try to fix it for you.

Friend, I managed to upload the application in Docker. Despite having incremented the annotations in Application, I think what made it work was instead of getting the .jar file from the folder: out > artifacts > beerstore_jar > beerstore.jar (generated in: Build > Build Artifacts) I got the . jar from: targer > beerstore-0.0.1-SNAPSHOT.jar (generated in terminal with command: mvn clean install). In addition, I also changed the port of the command: (docker run (...) -e APP_OPTIONS='--spring.datasource.url=jdbc:postgresql://beerdb:5433/beerstore' (...)) from 5433 to port 5432, where postgres is.

Good, yes you need to get it from target.
