Home > Enterprise >  Hey, I have a question about dependency injection in Spring and bean instantiation
Hey, I have a question about dependency injection in Spring and bean instantiation

Time:11-30

@Service
@AllArgsConstructor
@RequiredArgsConstructor
//@NoArgsConstructor
public class CurrencyExchange_Logic implements LogicInterface {


    private final Currency_Interface currency_interface;
    private final Rates_Interface rates_interface;
    private final OldRates_Interface Oldrates_interface;
    String start, end;

// methods
}

Stack trace:

C:\Users\mtsge\.jdks\azul-13.0.6\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.3.4\lib\idea_rt.jar=60750:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.3.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\mtsge\OneDrive\Pulpit\Syf\Currency Exchange2\currence_exchange\target\classes;C:\Users\mtsge\.m2\repository\org\springframework\boot\spring-boot-starter-data-jdbc\2.6.0\spring-boot-starter-data-jdbc-2.6.0.jar;C:\Users\mtsge\.m2\repository\org\springframework\boot\spring-boot-starter-jdbc\2.6.0\spring-boot-starter-jdbc-2.6.0.jar;C:\Users\mtsge\.m2\repository\com\zaxxer\HikariCP\4.0.3\HikariCP-4.0.3.jar;C:\Users\mtsge\.m2\repository\org\springframework\spring-jdbc\5.3.13\spring-jdbc-5.3.13.jar;C:\Users\mtsge\.m2\repository\org\springframework\data\spring-data-jdbc\2.3.0\spring-data-jdbc-2.3.0.jar;C:\Users\mtsge\.m2\repository\org\springframework\data\spring-data-relational\2.3.0\spring-data-relational-2.3.0.jar;C:\Users\mtsge\.m2\repository\org\springframework\data\spring-data-commons\2.6.0\spring-data-commons-2.6.0.jar;C:\Users\mtsge\.m2\repository\org\springframework\spring-tx\5.3.13\spring-tx-5.3.13.jar;C:\Users\mtsge\.m2\repository\org\springframework\boot\spring-boot-starter-data-jpa\2.6.0\spring-boot-starter-data-jpa-2.6.0.jar;C:\Users\mtsge\.m2\repository\jakarta\transaction\jakarta.transaction-api\1.3.3\jakarta.transaction-api-1.3.3.jar;C:\Users\mtsge\.m2\repository\jakarta\persistence\jakarta.persistence-api\2.2.3\jakarta.persistence-api-2.2.3.jar;C:\Users\mtsge\.m2\repository\org\hibernate\hibernate-core\5.6.1.Final\hibernate-core-5.6.1.Final.jar;C:\Users\mtsge\.m2\repository\org\jboss\logging\jboss-logging\3.4.2.Final\jboss-logging-3.4.2.Final.jar;C:\Users\mtsge\.m2\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;C:\Users\mtsge\.m2\repository\org\jboss\jandex\2.2.3.Final\jandex-2.2.3.Final.jar;C:\Users\mtsge\.m2\repository\com\fasterxml\classmate\1.5.1\classmate-1.5.1.jar;C:\Users\mtsge\.m2\repository\org\hibernate\common\hibernate-commons-annotations\5.1.2.Final\hibernate-commons-annotations-5.1.2.Final.jar;C:\Users\mtsge\.m2\repository\org\glassfish\jaxb\jaxb-runtime\2.3.5\jaxb-runtime-2.3.5.jar;C:\Users\mtsge\.m2\repository\org\glassfish\jaxb\txw2\2.3.5\txw2-2.3.5.jar;C:\Users\mtsge\.m2\repository\com\sun\istack\istack-commons-runtime\3.0.12\istack-commons-runtime-3.0.12.jar;C:\Users\mtsge\.m2\repository\com\sun\activation\jakarta.activation\1.2.2\jakarta.activation-1.2.2.jar;C:\Users\mtsge\.m2\repository\org\springframework\data\spring-data-jpa\2.6.0\spring-data-jpa-2.6.0.jar;C:\Users\mtsge\.m2\repository\org\springframework\spring-orm\5.3.13\spring-orm-5.3.13.jar;C:\Users\mtsge\.m2\repository\org\springframework\spring-aspects\5.3.13\spring-aspects-5.3.13.jar;C:\Users\mtsge\.m2\repository\org\springframework\boot\spring-boot-starter-thymeleaf\2.6.0\spring-boot-starter-thymeleaf-2.6.0.jar;C:\Users\mtsge\.m2\repository\org\springframework\boot\spring-boot-starter\2.6.0\spring-boot-starter-2.6.0.jar;C:\Users\mtsge\.m2\repository\org\springframework\boot\spring-boot\2.6.0\spring-boot-2.6.0.jar;C:\Users\mtsge\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.6.0\spring-boot-autoconfigure-2.6.0.jar;C:\Users\mtsge\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.6.0\spring-boot-starter-logging-2.6.0.jar;C:\Users\mtsge\.m2\repository\ch\qos\logback\logback-classic\1.2.7\logback-classic-1.2.7.jar;C:\Users\mtsge\.m2\repository\ch\qos\logback\logback-core\1.2.7\logback-core-1.2.7.jar;C:\Users\mtsge\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.14.1\log4j-to-slf4j-2.14.1.jar;C:\Users\mtsge\.m2\repository\org\apache\logging\log4j\log4j-api\2.14.1\log4j-api-2.14.1.jar;C:\Users\mtsge\.m2\repository\org\slf4j\jul-to-slf4j\1.7.32\jul-to-slf4j-1.7.32.jar;C:\Users\mtsge\.m2\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;C:\Users\mtsge\.m2\repository\org\yaml\snakeyaml\1.29\snakeyaml-1.29.jar;C:\Users\mtsge\.m2\repository\org\thymeleaf\extras\thymeleaf-extras-java8time\3.0.4.RELEASE\thymeleaf-extras-java8time-3.0.4.RELEASE.jar;C:\Users\mtsge\.m2\repository\org\springframework\boot\spring-boot-starter-validation\2.6.0\spring-boot-starter-validation-2.6.0.jar;C:\Users\mtsge\.m2\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.55\tomcat-embed-el-9.0.55.jar;C:\Users\mtsge\.m2\repository\org\hibernate\validator\hibernate-validator\6.2.0.Final\hibernate-validator-6.2.0.Final.jar;C:\Users\mtsge\.m2\repository\jakarta\validation\jakarta.validation-api\2.0.2\jakarta.validation-api-2.0.2.jar;C:\Users\mtsge\.m2\repository\org\springframework\boot\spring-boot-starter-web\2.6.0\spring-boot-starter-web-2.6.0.jar;C:\Users\mtsge\.m2\repository\org\springframework\boot\spring-boot-starter-json\2.6.0\spring-boot-starter-json-2.6.0.jar;C:\Users\mtsge\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.0\jackson-datatype-jdk8-2.13.0.jar;C:\Users\mtsge\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.0\jackson-datatype-jsr310-2.13.0.jar;C:\Users\mtsge\.m2\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.0\jackson-module-parameter-names-2.13.0.jar;C:\Users\mtsge\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\2.6.0\spring-boot-starter-tomcat-2.6.0.jar;C:\Users\mtsge\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.55\tomcat-embed-core-9.0.55.jar;C:\Users\mtsge\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.55\tomcat-embed-websocket-9.0.55.jar;C:\Users\mtsge\.m2\repository\org\springframework\spring-web\5.3.13\spring-web-5.3.13.jar;C:\Users\mtsge\.m2\repository\mysql\mysql-connector-java\8.0.27\mysql-connector-java-8.0.27.jar;C:\Users\mtsge\.m2\repository\javax\money\money-api\1.0.1\money-api-1.0.1.jar;C:\Users\mtsge\.m2\repository\org\javamoney\moneta\1.0\moneta-1.0.jar;C:\Users\mtsge\.m2\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;C:\Users\mtsge\.m2\repository\org\thymeleaf\thymeleaf\3.0.11.RELEASE\thymeleaf-3.0.11.RELEASE.jar;C:\Users\mtsge\.m2\repository\ognl\ognl\3.1.12\ognl-3.1.12.jar;C:\Users\mtsge\.m2\repository\org\javassist\javassist\3.20.0-GA\javassist-3.20.0-GA.jar;C:\Users\mtsge\.m2\repository\org\attoparser\attoparser\2.0.5.RELEASE\attoparser-2.0.5.RELEASE.jar;C:\Users\mtsge\.m2\repository\org\unbescape\unbescape\1.1.6.RELEASE\unbescape-1.1.6.RELEASE.jar;C:\Users\mtsge\.m2\repository\org\slf4j\slf4j-api\1.7.32\slf4j-api-1.7.32.jar;C:\Users\mtsge\.m2\repository\org\springframework\spring-webmvc\5.3.7\spring-webmvc-5.3.7.jar;C:\Users\mtsge\.m2\repository\org\springframework\spring-aop\5.3.13\spring-aop-5.3.13.jar;C:\Users\mtsge\.m2\repository\org\springframework\spring-beans\5.3.13\spring-beans-5.3.13.jar;C:\Users\mtsge\.m2\repository\org\springframework\spring-context\5.3.13\spring-context-5.3.13.jar;C:\Users\mtsge\.m2\repository\org\springframework\spring-core\5.3.13\spring-core-5.3.13.jar;C:\Users\mtsge\.m2\repository\org\springframework\spring-jcl\5.3.13\spring-jcl-5.3.13.jar;C:\Users\mtsge\.m2\repository\org\springframework\spring-expression\5.3.13\spring-expression-5.3.13.jar;C:\Users\mtsge\.m2\repository\org\springframework\boot\spring-boot-starter-aop\2.5.2\spring-boot-starter-aop-2.5.2.jar;C:\Users\mtsge\.m2\repository\org\aspectj\aspectjweaver\1.9.7\aspectjweaver-1.9.7.jar;C:\Users\mtsge\.m2\repository\org\thymeleaf\thymeleaf-spring5\3.0.11.RELEASE\thymeleaf-spring5-3.0.11.RELEASE.jar;C:\Users\mtsge\.m2\repository\com\google\code\gson\gson\2.8.5\gson-2.8.5.jar;C:\Users\mtsge\.m2\repository\com\microsoft\sqlserver\mssql-jdbc\9.4.0.jre8\mssql-jdbc-9.4.0.jre8.jar;C:\Users\mtsge\.m2\repository\org\projectlombok\lombok\1.18.22\lombok-1.18.22.jar;C:\Users\mtsge\.m2\repository\jakarta\xml\bind\jakarta.xml.bind-api\2.3.3\jakarta.xml.bind-api-2.3.3.jar;C:\Users\mtsge\.m2\repository\jakarta\activation\jakarta.activation-api\1.2.2\jakarta.activation-api-1.2.2.jar;C:\Users\mtsge\.m2\repository\net\bytebuddy\byte-buddy\1.11.22\byte-buddy-1.11.22.jar;C:\Users\mtsge\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.11.3\jackson-databind-2.11.3.jar;C:\Users\mtsge\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.13.0\jackson-annotations-2.13.0.jar;C:\Users\mtsge\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.13.0\jackson-core-2.13.0.jar" com.example.currence_exchange.CurrenceExchangeApplication

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.0)

2021-11-28 23:09:44.805  INFO 6976 --- [           main] c.e.c.CurrenceExchangeApplication        : Starting CurrenceExchangeApplication using Java 13.0.6 on DESKTOP-61G00PJ with PID 6976 (C:\Users\mtsge\OneDrive\Pulpit\Syf\Currency Exchange2\currence_exchange\target\classes started by mtsge in C:\Users\mtsge\OneDrive\Pulpit\Syf\Currency Exchange2)
2021-11-28 23:09:44.810  INFO 6976 --- [           main] c.e.c.CurrenceExchangeApplication        : No active profile set, falling back to default profiles: default
2021-11-28 23:09:46.741  INFO 6976 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-11-28 23:09:46.741  INFO 6976 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-11-28 23:09:46.855  INFO 6976 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 93 ms. Found 3 JPA repository interfaces.
2021-11-28 23:09:47.146  INFO 6976 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2021-11-28 23:09:47.147  INFO 6976 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JDBC repositories in DEFAULT mode.
2021-11-28 23:09:47.162  INFO 6976 --- [           main] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.example.currence_exchange.Interfaces.Currency_Interface. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.
2021-11-28 23:09:47.163  INFO 6976 --- [           main] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.example.currence_exchange.Interfaces.OldRates_Interface. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.
2021-11-28 23:09:47.165  INFO 6976 --- [           main] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.example.currence_exchange.Interfaces.Rates_Interface. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.
2021-11-28 23:09:47.166  INFO 6976 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 17 ms. Found 0 JDBC repository interfaces.
2021-11-28 23:09:48.606  INFO 6976 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-11-28 23:09:48.624  INFO 6976 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-11-28 23:09:48.624  INFO 6976 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.55]
2021-11-28 23:09:48.844  INFO 6976 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-11-28 23:09:48.845  INFO 6976 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3880 ms
2021-11-28 23:09:49.138  INFO 6976 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-11-28 23:09:49.228  INFO 6976 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.6.1.Final
2021-11-28 23:09:49.507  INFO 6976 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-11-28 23:09:49.704  INFO 6976 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-11-28 23:09:50.381  INFO 6976 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-11-28 23:09:50.429  INFO 6976 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2021-11-28 23:09:51.771  INFO 6976 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-11-28 23:09:51.789  INFO 6976 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-11-28 23:09:51.810  WARN 6976 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'viewControllers' defined in file [C:\Users\mtsge\OneDrive\Pulpit\Syf\Currency Exchange2\currence_exchange\target\classes\com\example\currence_exchange\Controllers\ViewControllers.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'currencyExchange_Logic' defined in file [C:\Users\mtsge\OneDrive\Pulpit\Syf\Currency Exchange2\currence_exchange\target\classes\com\example\currence_exchange\Service\CurrencyExchange_Logic.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.currence_exchange.Service.CurrencyExchange_Logic]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.example.currence_exchange.Service.CurrencyExchange_Logic.<init>()
2021-11-28 23:09:51.811  INFO 6976 --- [           main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2021-11-28 23:09:51.815  INFO 6976 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2021-11-28 23:09:51.841  INFO 6976 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2021-11-28 23:09:51.843  INFO 6976 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2021-11-28 23:09:51.860  INFO 6976 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-11-28 23:09:51.893 ERROR 6976 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'viewControllers' defined in file [C:\Users\mtsge\OneDrive\Pulpit\Syf\Currency Exchange2\currence_exchange\target\classes\com\example\currence_exchange\Controllers\ViewControllers.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'currencyExchange_Logic' defined in file [C:\Users\mtsge\OneDrive\Pulpit\Syf\Currency Exchange2\currence_exchange\target\classes\com\example\currence_exchange\Service\CurrencyExchange_Logic.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.currence_exchange.Service.CurrencyExchange_Logic]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.example.currence_exchange.Service.CurrencyExchange_Logic.<init>()
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.13.jar:5.3.13]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.13.jar:5.3.13]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.0.jar:2.6.0]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) ~[spring-boot-2.6.0.jar:2.6.0]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) ~[spring-boot-2.6.0.jar:2.6.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) ~[spring-boot-2.6.0.jar:2.6.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[spring-boot-2.6.0.jar:2.6.0]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1290) ~[spring-boot-2.6.0.jar:2.6.0]
    at com.example.currence_exchange.CurrenceExchangeApplication.main(CurrenceExchangeApplication.java:14) ~[classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'currencyExchange_Logic' defined in file [C:\Users\mtsge\OneDrive\Pulpit\Syf\Currency Exchange2\currence_exchange\target\classes\com\example\currence_exchange\Service\CurrencyExchange_Logic.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.currence_exchange.Service.CurrencyExchange_Logic]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.example.currence_exchange.Service.CurrencyExchange_Logic.<init>()
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1334) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1232) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791) ~[spring-beans-5.3.13.jar:5.3.13]
    ... 19 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.currence_exchange.Service.CurrencyExchange_Logic]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.example.currence_exchange.Service.CurrencyExchange_Logic.<init>()
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1326) ~[spring-beans-5.3.13.jar:5.3.13]
    ... 31 common frames omitted
Caused by: java.lang.NoSuchMethodException: com.example.currence_exchange.Service.CurrencyExchange_Logic.<init>()
    at java.base/java.lang.Class.getConstructor0(Class.java:3350) ~[na:na]
    at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2554) ~[na:na]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78) ~[spring-beans-5.3.13.jar:5.3.13]
    ... 32 common frames omitted


Process finished with exit code 1

Controller class

@Controller
@RequiredArgsConstructor
public class ViewControllers {

    private final CurrencyExchange_Logic currencyExchange_logic;
    private final Rates_Interface rates_interface;
    private final OldRates_Interface Oldrates_interface;


    @PostMapping("specificRate")
    public String getSpecificRate(Model model, @ModelAttribute("specific") @Valid RatesViewModel ratesViewModel) {
        Set set = new HashSet<>();
        set.add(rates_interface.getByCode(ratesViewModel.getCode()));
        set.add(Oldrates_interface.getByCode(ratesViewModel.getCode()));

        model.addAttribute("spec", set);
        return "formPage";
    }


    @GetMapping("all")
    public String all(Model model, @RequestParam(value = "page", defaultValue = "1") Integer page,
                      @RequestParam(value = "size", defaultValue = "5") Integer size) {

        Page<CurrencyEntity> ratePage = currencyExchange_logic.pagination(PageRequest.of(page - 1, size));
        model.addAttribute("all", ratePage);

        int totalPages = ratePage.getTotalPages();
        if (totalPages > 0) {
            List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages)
                    .boxed()
                    .collect(Collectors.toList());
            model.addAttribute("pageNumbers", pageNumbers);
        }
        return "allPage";
    }


    @GetMapping("AllRates")
    public String allRates(Model model) {
        model.addAttribute("ratesOf", new DatesViewModel());
        model.addAttribute("allRates", rates_interface.findAll());
        return "allRates";
    }


    @GetMapping("/")
    public String mainPaige(Model model) {
        model.addAttribute("currencyAndAmount", new CurrencyViewModel());
        return "formPage";
    }


    @PostMapping("RatesOfPeriod")
    public String RatesOfPeriod(@ModelAttribute("ratesOf") @Valid DatesViewModel datesViewModel,
                                BindingResult bindingResult, Model model) {
        if (!bindingResult.hasErrors()) {
            String start = Objects.toString(datesViewModel.getDatesFrom(), "");
            String end = Objects.toString(datesViewModel.getDatesTo(), "");
            try {
                currencyExchange_logic.getRatesOfDates(start, end);
                model.addAttribute("allRates", Oldrates_interface.findAll());
                Oldrates_interface.deleteAll();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
        return "allRates";
    }


    @PostMapping("Calculate")
    public String Calculate(@ModelAttribute("currencyAndAmount") @Valid CurrencyViewModel currencyViewModel,
                            BindingResult bindingResult, Model model) {
        if (!bindingResult.hasErrors()) {
            var entity = currencyExchange_logic.CurrencyViewModelToEntity(currencyViewModel);
            String start = Objects.toString(entity.getDateFrom(), "");
            String end = Objects.toString(entity.getDateTo(), "");
            try {
                MonetaryAmountJson currencyJson = currencyExchange_logic.currencyJson(start, end);
                var calVal = currencyExchange_logic.calculateMoney(currencyJson, entity);
                model.addAttribute("endValue", calVal);
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
            return "end";
        } else {
            return "formPage";
        }
    }

// TODO: 28.11.2021 napisac testy i ZREFAKTOROWAC
}

interfaces

Repository
public interface Rates_Interface extends JpaRepository<RatesEntity, Long> {

    @Query("select r from RatesEntity r where r.code = :code")
    List<RatesEntity> getByCode(@Param("code") CurrencyEnum code);


}



@Repository
public interface OldRates_Interface extends JpaRepository<OldRatesEntity, Long> {

    @Query("select r from OldRatesEntity r where r.code = :code")
    List<OldRatesEntity> getByCode(@Param("code") CurrencyEnum code);
}

Question i have is that stack informs that a bean Currency_Interface_Logic cannot be created beacuse the class above does not have a default constructor and therefore cannot inject dependencies but annotated this class with @AllArgsConstructor, @RequiredArgsConstructor (can' add @NoArgsConstructor) so everything should be fine. Why do I get this error and how to fix it? All was good until I added a new @PostMapping method (getSpecificRate()) and new query methods in above interfaces

CodePudding user response:

Your CurrencyExchange_Logic class has two constructors: the required args constructor, which has parameters corresponding to the 3 final fields, and the all args constructor, with parameters corresponding to those 3 fields as well as start and end.

When you only have one constructor defined, Spring knows how to implicitly choose it for injection. However, when you have more than one, you have to tell it which one you want it to use, using the @Autowired or @Inject annotation.

I would guess you want Spring to use the required args constructor, as I doubt Spring has any way of knowing how to resolve the start or end fields. This can be done in lombok (@RequiredArgsConstructor(onConstructor_ = @Autowired)), or by just explicitly writing the constructor yourself and annotating it. Note you could also get rid of the all args constructor, if you don't need it, and Spring's implicit constructor injection should "just work."

  • Related