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