Properties file not getting read by Spring

Viewed 416

I've spent hours on this. I can't get my variables interpolated correctly with my properties file. I have a properties file and cannot get the values interpolated correctly. I've tried specifying the file explicitly, using an external config and the config inside the jar to no avail.

enter image description here

Usage

@Component
public class TeradataConnector {
    @Value("${jdbcurl}")
    private String teradataConnection;

    @Value("${jdbctable}")
    private String teradataTable;

Error

Jan 05, 2021 6:10:15 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Jan 05, 2021 6:10:15 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Tomcat]
Jan 05, 2021 6:10:15 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.34]
Jan 05, 2021 6:10:15 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring embedded WebApplicationContext
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teradataConnector': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'jdbcurl' in value "${jdbcurl}"
Jan 05, 2021 6:10:16 PM org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service [Tomcat]
WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance.
Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teradataConnector': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'jdbcurl' in value "${jdbcurl}"
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:405) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:895) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at com.company.nap.cyclic.App.main(App.java:11) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'jdbcurl' in value "${jdbcurl}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:178) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:175) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:912) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1247) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1226) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[cyclic-feature-cyclic_migration-494a9e9-dirty.jar:?]
    ... 17 more

application.properties

#### Personalized Batch Configuration
batch.max_records=1
batch.max_wait_seconds=15
jdbcurl=wefwef
jdbcuser=wefwef
jdbcpass=fweijfwef
teradata.table=tester
2 Answers
  1. Find the package containing your main Spring Boot Application file. It contains the static main method and that has the @SpringBootApplication annotation. It should look something like this:
@SpringBootApplication
public class MyApplication {
  public static void main(String[] args) {
  SpringApplication.run(MyApplication.class, args);   
}
  1. Make sure your TeradataConnector class resides in a package beneath the location of your Spring Application class found in step 1. So if your Spring Application class is in: com.companyxyz.invoice
    then you TeradataConnector class must be: com.companyxyz.invoice.someotherpackage

Note: It can be further down. In other words, it does not have to be immediately below, just underneath somewhere.

Reason: a Spring Boot Application will at startup by default will only scan and autowire components in the packages underneath the main Spring Application package. Note: You could also add a @ComponentScan annotation and tell Spring where to find the components if they are not underneath you directory. However, I would recommend just moving them underneath unless there is a strong compelling reason.

Make sure you are imported class for @Value annotation is import org.springframework.beans.factory.annotation.Value

application.properties file should inside src\main\resources folder.

You must add key & value for jdbctable in application.properties file which is missing.

Related