0

I wrote this class to retrive a property from application.propreties file. I get the error above on the @value statment. I Tried to set it different ways but nothing helped.

also tried setting it as @componnet.

package com.example.demo;

import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.management.loading.PrivateMLet;

import org.springframework.beans.factory.annotation.*;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.*;


@Configuration
public class ApplicationConfiguration {

    @Value("${spring.datasource.url");
        private String JdbcUrl;

    @Value("${spring.datasource.user:}");
        private String JdbcUser;

    @Value("${spring.datasource.password:}");
        private String JdbcPassword;

    public String getJdbcUrl() {
        return JdbcUrl;
    }
    public String getJdbcUser() {
        return JdbcUser;
    }
    public String getJdbcPassword() {
        return JdbcPassword;
    }

}

the Erorr I get is:

2020-12-05 11:11:30.077  INFO 660 --- [           main] c.e.demo.AccessPostgresApplication       : Starting AccessPostgresApplication on user-PC with PID 660 (C:\Users\user\Documents\mytrips\target\classes started by user in C:\Users\user\Documents\mytrips)
2020-12-05 11:11:30.080  INFO 660 --- [           main] c.e.demo.AccessPostgresApplication       : The following profiles are active: dev
2020-12-05 11:11:31.838  INFO 660 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-12-05 11:11:31.838  INFO 660 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.38]
2020-12-05 11:11:32.008  INFO 660 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded
WebApplicationContext
2020-12-05 11:11:32.124  INFO 660 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-12-05 11:11:32.551  INFO 660 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-12-05 11:11:33.153  INFO 660 --- [         task-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-12-05 11:11:33.223  INFO 660 --- [         task-1] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.21.Final
2020-12-05 11:11:33.418  INFO 660 --- [         task-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-12-05 11:11:33.578  INFO 660 --- [         task-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2020-12-05 11:11:34.580  INFO 660 --- [         task-1] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform
implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Exception in thread "task-2" java.lang.IllegalStateException: EntityManagerFactory is closed
        at org.hibernate.internal.SessionFactoryImpl.validateNotClosed(SessionFactoryImpl.java:509)
2020-12-05 11:11:34.606  INFO 660 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
        at org.hibernate.internal.SessionFactoryImpl.getProperties(SessionFactoryImpl.java:503)
        at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.findDataSource(DataSourceInitializedPublisher.java:105)
        at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.publishEventIfRequired(DataSourceInitializedPublisher.java:97)
        at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.access$100(DataSourceInitializedPublisher.java:50)
        at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher$DataSourceSchemaCreatedPublisher.lambda$postProcessEntityManagerFactory$0(DataSourceInitializedPublisher.java:200)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
        at java.base/java.lang.Thread.run(Thread.java:832)
2020-12-05 11:11:34.614  INFO 660 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2020-12-05 11:11:34.616  INFO 660 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-12-05 11:11:34.641 ERROR 660 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'applicationConfiguration' defined in file [C:\Users\user\Documents\mytrips\target\classes\com\example\demo\ApplicationConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.demo.ApplicationConfiguration$$EnhancerBySpringCGLIB$$5e10e388]: Constructor threw exception; nested exception is java.lang.Error: Unresolved compilation problems:
        Syntax error, insert "enum Identifier" to complete EnumHeaderName
        Syntax error, insert "EnumBody" to complete ClassBodyDeclarations
        Syntax error, insert "enum Identifier" to complete EnumHeaderName
        Syntax error, insert "EnumBody" to complete EnumDeclaration
        Syntax error, insert "enum Identifier" to complete EnumHeaderName
        Syntax error, insert "EnumBody" to complete EnumDeclaration

        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1318) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1213) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) ~[spring-beans-5.2.9.RELEASE.jar:5.2.9.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.9.RELEASE.jar:5.2.9.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.9.RELEASE.jar:5.2.9.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
        at com.example.demo.AccessPostgresApplication.main(AccessPostgresApplication.java:11) ~[classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate 

jsdkfn kljsfn lkflksd ;lskjf ;lskjf ;lkfj s;lkfj ;lskjdf ;lkj ;lskdjf nvm,fnbmdnb ,mfnb ,mdsn

2 Answers2

0

You can not use final with @Value in spring. If you use final with a field in a class then you must initialize the field and after initialization the value can not be changed.

But @Value workes differently, spring will populate the value at runtime, so if there is a final identifier, spring will not be able to populate the value with the value of the property.

In your case, you are using final but you are not initializing the value, that's why you are getting an exception. If you can either remove the final or init the value with something to remove the exception. If you remove the final spring will be able to populate the value. If you init the value with something, then the value will not be changed in you application.

Shawrup
  • 2,478
  • 2
  • 12
  • 21
  • removing the 'final' did not resolve the issue. Constructor threw exception; nested exception is java.lang.Error: Unresolved compilation problems: Syntax error, insert "enum Identifier" to complete EnumHeaderName Syntax error, insert "EnumBody" to complete ClassBodyDeclarations – Itay Lanir Dec 05 '20 at 07:14
  • please add the exception and related code. – Shawrup Dec 05 '20 at 09:02
  • edited the original post with the fukk code and error – Itay Lanir Dec 05 '20 at 09:21
  • https://stackoverflow.com/questions/9986398/syntax-error-insert-enum-identifier-insert-enumbody-inset, please try the solutions mentioned in here. – Shawrup Dec 05 '20 at 11:23
  • 1
    Found the issue. there was a ';' at the end of the @value statment – Itay Lanir Dec 06 '20 at 18:35
0

Found the issue. there was a ';' at the end of the @value statment