简体   繁体   中英

No qualifying bean of type 'javax.persistence.EntityManager' available when import jar dependency

I'm working on two relative projects. One project is used to connect to postgresql and another project import the connector project as dependency to connect to postgresql. In connector project I used EntityManager to access to database.

In Repository class I use entity manager to get entity

    @Transactional
    @Repository
    public class ConfigDetailRepository {
    private EntityManager entityManager;

    @Autowired
    public ConfigDetailRepository(final EntityManager entityManager) {
        this.entityManager = entityManager;
    }
    public void doSomething()

And in Service class I autowire repository

@Configurable
@Service
public class SampleService {
    @Autowired
    private ConfigDetailRepository configDetailRepository;
    public class (){ configDetailRepository.doSomething()}

This connector project is workable and able to get data from database. In my second project I import the connector project and add this dependency in pom.xml

<dependency>
    <groupId>package name</groupId>
    <artifactId>package-id</artifactId>
        <version>package-version</version>
</dependency>

And that's my Application.class and class import SampleService

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@ComponentScan(basePackages = {"package name"})
public class CassandraTestApplication  {
    public static void main(String[] args) {
        SpringApplication.run(CassandraTestApplication.class, args);
    }
@Configurable
@Component
public class TestConnector {
    @Autowired
    SampleService sampleService;

    public void doSomtehing() {
       sampleService.doSomething();
    }
}

When I run the project I got following error:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in package.repository.ConfigDetailRepository required a bean of type 'javax.persistence.EntityManager' that could not be found.

The injection point has the following annotations:
        - @org.springframework.beans.factory.annotation.Autowired(required=true)


java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testConnector': Unsatisfied dependency expressed through field 'sampleService'; nested exception is org.springframework.beans
.factory.UnsatisfiedDependencyException: Error creating bean with name 'sampleService': Unsatisfied dependency expressed through field 'configDetailRepository'; nested exception is org.springframework.beans.factory.UnsatisfiedDepende
ncyException: Error creating bean with name 'configDetailRepository' defined in URL [jar:file:/C:/Users/.m2/repository/com/connector/cassandra-db/0.0.1-SNAPSHOT/cassandra-db-0.0.1-SNAPSHOT.jar!/com/repository/ConfigDetailRepository.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying b
ean of type 'javax.persistence.EntityManager' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

I check the connector project and it can work. Then I tried exclude=HibernateJpaAutoConfiguration.class but that didn't work. Can anyone tell me why the entity manager cannot be created?

The problem is the exclude = {DataSourceAutoConfiguration.class} . With this you are excluding the configuration of the DataSource by Spring Boot. Unless you specify one yourself using an @Bean method this basically prevents you from doing all DB related work.

JPA requires access to your database and without a configured DataSource this will not be possible. Due to the missing DataSource Spring Boot will also not configure JPA. No JPA will lead to this error.

To fix, remove the exclude and provide the configuration for the database.

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