0

edit note : during debug, autowired config object in the main class always returned null

I am trying to build a simple spring application with a Configuration class and a main class which basically gets values from an external application.properties file to be used in the main class. As I am new to spring, probably I am missing something basic so I appreciate any help. Many thanks in advance.

Here is application.properties (which is in the same folder as Config.java file)

kinesis.consumer.transaction.streamName = sample-stream-name
kinesis.consumer.transaction.applicationName = sample-application-name

And the Config.java file

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;


@Configuration
@PropertySource("classpath:application.properties")
public class Config {

    @Value("${kinesis.consumer.transaction.streamName}")
    private
    String transactionStreamName;

    @Value("${kinesis.consumer.transaction.applicationName}")
    private
    String transactionApplicationName;


    public String getTransactionStreamName() {
        return transactionStreamName;
    }

    public void setTransactionStreamName(String transactionStreamName)
    {
        this.transactionStreamName = transactionStreamName;
    }

    public String getTransactionApplicationName()
    {
        return transactionApplicationName;
    }

    public void setTransactionApplicationName(String transactionApplicationName)
    {
        this.transactionApplicationName = transactionApplicationName;
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

and finally the main class

public class TransactionProcessor {

    @Autowired
    private static Config config;

    public static void main (String args[]) {
       System.out.println(config.getTransactionApplicationName()+" " + config.getTransactionStreamName());
    }
}

below is the project structure

structure

Cœur
  • 37,241
  • 25
  • 195
  • 267
emrahozkan
  • 193
  • 1
  • 3
  • 15
  • 1
    Can you share your detailed project structure? I believe it is happening because application.properties might not be on the classpath – koder23 Jun 09 '16 at 10:15
  • Is this a Spring Boot application or are you not using Spring Boot? The file `application.properties` is what's used by Spring Boot, but if you're not using Spring Boot this will not be picked up automatically. You'll need to do what @koder23 shows below to let Spring pick it up. – Jesper Jun 09 '16 at 10:58

4 Answers4

1

Note that by default the below folders are available on the classpath:

/src/main/java/
/src/main/resources/
/src/main/test/

Now the way I have done it is that my /src/main/resources/ has my application.properties file and my /src/main/java has a package com.mypackage.config which has my Config.java file. The way I have referenced it is as below:

@PropertySource("classpath:application.properties")

This works for me.

koder23
  • 307
  • 2
  • 11
0

Put application.properties in src/main/resources

Sangram Jadhav
  • 2,438
  • 16
  • 17
0

I'm guessing that Config.java is in a package. Say: "com.kinesis.app". If the config file is in that same directory then you can load it via:

@PropertySource("classpath:com/kinesis/app/application.properties")

However, the other post is right. Most java tooling will make use of files in

src/main/resources

And copy them to the right place in your created jar. How are you building your application?

Robert Moskal
  • 21,737
  • 8
  • 62
  • 86
  • Hello, I added the project structure tree to the question, tried your suggestion as well yet didn't succeed – emrahozkan Jun 09 '16 at 11:33
0

Try by loading Environment in which the current application is running. Models two key aspects of the application environment: profiles and properties.

@Configuration
@PropertySource({ "classpath:application.properties" })
public class PersistenceJPAConfig {

@Autowired
private Environment env;

public void properties() {
    Preconditions.checkNotNull(env
            .getProperty("kinesis.consumer.transaction.streamName")));
    Preconditions.checkNotNull(env
            .getProperty("kinesis.consumer.transaction.applicationName")));

    }
}
biology.info
  • 3,500
  • 2
  • 28
  • 39