Skip to content
Advertisement

When running a project “Unsatisfied dependency expressed through constructor parameter 0”

When running a project, an error occurs:

UnsatisfiedDependencyException: Error creating bean with name ‘sqlServerQueryDaoImpl’ defined in file SqlServerQueryDaoImpl.class : Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘javax.persistence.EntityManagerFactory’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

Code

JavaScript

Dao

JavaScript

DaoImpl

JavaScript

my pom.xml

JavaScript

stacktrace

JavaScript

Advertisement

Answer

Well, there are multiple problems found:

First, of all. You’ve mixed plain Spring and Spring Boot application/configuration.

For example, this dependency:

JavaScript

Already contains these two artifacts:

JavaScript

There’s no need to duplicate them (unless you need specific version of it), since you’re using spring-boot-starter packages.

Then your @SpringBootApplication class should be in the root directory of your app, in order to scan all the packages recursively, like this (note package):

JavaScript

Then, coming to a problem with two data sources.

PostgresqlConfiguration

Change

JavaScript

To

JavaScript

Similarly do the same for SqlserverConfiguration.java class.

In SqlServerQueryDaoImpl.java do the following changes:

JavaScript

In order to disable Spring auto configuration for data source (HikariCP will scan for it), add the following to application.properties:

JavaScript

No other changes needed.

Then you have a problem with @Cache in your entities. You did not specify nor you’ve added cache provider to class path, so it can’t find a provider and fails on the startup. Just remove @Cache annotation from DayVisits.java and IntervalVisits.java.

Then add @Transient to this setter:

JavaScript

Then add setter to week_num field. JPA will scan for it.

JavaScript

And the last one, you need to add JSONB bean implementation to the classpath:

JavaScript

After all this changes were done, I was able to start the app on embedded Tomcat.

I’d also suggest to get rid of all *.xml configuration files. Move everything to application.properties and read it from there.

In general, try to minimize customization of Spring app, unless you’re totally understanding what you’re doing. Spring Boot provides tons of ways how to bootstrap your project with minimum effort. For example, this article shows an example of Spring Boot app with 2 data sources: https://medium.com/@joeclever/using-multiple-datasources-with-spring-boot-and-spring-data-6430b00c02e7

Overall, in order to get the project running some moderate refactoring is really needed.

Good luck with it.

Advertisement