Spring boot application properties are all read as null - spring-boot

In my application class I have
#SpringBootApplication(scanBasePackages = {"com.binance.bot.timeandsales", <some other packages>})
#Configuration
#EnableScheduling
public class BinanceTimeAndSalesApplication implements CommandLineRunner {
#Autowired
AggTradeEventSubscriptionListener aggTradeEventSubscriptionListener;
And in the bean class which is a non-public class in the same file I have
#Component
class AggTradeEventSubscriptionListener implements SubscriptionListener<AggregateTradeEvent> {
#Value("${binance.api}")
private String apiUrl;
which is coming as null.
In my application.properties I have
binance.api=https://fapi.binance.com
The application.properties in src/main/resources directory which is also appearing as the resources directory in the Intellij module configuration.
In my pom.xml I have
<properties>
<start-class>com.binance.bot.timeandsales.BinanceTimeAndSalesApplication</start-class>
</properties>
What am I doing wrong that the application properties are read as null?
Another weirdity is that in some other class in a different package, the properties are getting read. But not in the newer classes that I'm writing in the com.binance.bot.timeandsales package, which is specified as one of the packages to scan in the Configuration.

Try adding #Configuration to AggTradeEventSubscriptionListener.

Related

java jar -D option passing multiple external config files

I have two configuration files that have to be read using spring boot application, however, only the first configuration property file is resolved (external_application.properties) , later one (queries_config.properties) is not getting detected any issue in passing in the command line.
java -Dexternal.app.properties=file:external_application.properties -Dqueries.config.properties=file:queries_config.properties -jar testsnapshot.jar
If you are using spring boot so you can use
java
-Dspring.config.location=file:external_application.properties,file:queries_config.properties
source
I did like below which resolved over all problem..
java -Dspring.config.location=classpath:file:///C:\Users\configfiles\ -jar testsnapshot.jar
secondly to lookup for external config
#Component
#PropertySource(value = "${spring.config.location}queries_config-${spring.profiles.active}.properties")
#ConfigurationProperties("query")
public class QueriesConfig {
}
#Component
#PropertySource(value = "${spring.config.location}external_application-${spring.profiles.active}.properties")
#ConfigurationProperties("query")
public class ExternalConfig {
}

spring-boot application.properties are not overwriten

I have one class:
package com.example.propertyorder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
#SpringBootApplication
#PropertySource(value="file:C:\\TEMP\\property-order.properties", ignoreResourceNotFound = true)
public class PropertyOrderApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(PropertyOrderApplication.class, args);
Environment env = (Environment) run.getBean("environment");
System.out.println(env.getProperty("my.value"));
System.out.println(env.getProperty("my.value2"));
}
}
and I have one application.properties file:
my.value=application.properties
and I have my external properties file C:\\TEMP\\property-order.properties:
my.value=property-order.properties
my.value2=gotcha
but the result is always:
application.properties
gotcha
instead of:
property-order.properties
gotcha
So it looks like, that spring-boot's application.properties overrules all.
Is there a way to fix it.
The only solution that i found is to not use the application.properties, but a my-app.properties instead and place that one before my external file in the ProperySource-tree:
#PropertySource(value="classpath:my-app.properties")
#PropertySource(value="file:C:\\TEMP\\property-order.properties", ignoreResourceNotFound = true)
Is there a better way, so that I can stay with the application.properties ?
edit:
added missing value2 to the property file
The actual behavior is conform to the Spring Boot documentation :
Spring Boot uses a very particular PropertySource order that is
designed to allow sensible overriding of values. Properties are
considered in the following order:
....
14.Application properties outside of your packaged jar (application.properties and YAML variants).
15.Application properties packaged inside your jar (application.properties and YAML variants).
16.#PropertySource annotations on your #Configuration classes.
The application.properties (inside or outside the jar) have a higher priority (14 and 15 respectively) than any #PropertySource annotations added in #Configuration class (16).
Is there a better way, so that I can stay with the
application.properties ?
Of course you can use any of the 13 higher priority ways :
1.Devtools global settings properties on your home directory
(~/.spring-boot-devtools.properties when devtools is active).
2.#TestPropertySource annotations on your tests.
3.#SpringBootTest#properties annotation attribute on your tests. Command
line arguments.
4.Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an
environment variable or system property).
5.ServletConfig init parameters.
6.ServletContext init parameters.
7.JNDI attributes from java:comp/env.
8.Java System properties (System.getProperties()).
9.OS environment variables.
10.A RandomValuePropertySource that has properties only in random.*.
11.Profile-specific application properties outside of your packaged jar
(application-{profile}.properties and YAML variants).
12.Profile-specific application properties packaged inside your jar
(application-{profile}.properties and YAML variants).
13.Application properties outside of your packaged jar
(application.properties and YAML variants).
For example renaming property-order.properties to application-order.properties to make it a Spring Boot profile properties and run your application with order as active profile would give it a higher priority and so it should be enough :
#SpringBootApplication
public class PropertyOrderApplication {...}
If you want to override the property defined int the application.properties, you can use the below approach.
#PropertySources({
#PropertySource(value = "classpath:application.properties"),
#PropertySource(value = "file:/user/home/external.properties", ignoreResourceNotFound = true)
})
public class Application {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
}
}

Load multiple external configuration file in spring boot

How to load multiple external configuration properties file in spring boot. Please find the below command to load external properties file when run the jar file.
"java -jar -Dspring.config.location= myBootProject.jar"
Like we add one or two configuration path, but when we add more than two configuration then how would we configure?
In spring you can do like below:
#Configuration
#PropertySource({
"classpath:app-config.properties",
"classpath:dtabase.properties"
})
public class AppConfig {
#Autowired
Environment env;
}
If you are using spring4 and Java8 or Greater:
#Configuration
#PropertySources({
#PropertySource("classpath:app-config.properties"),
#PropertySource("classpath:database.properties")
})
public class AppConfig {
//configuration classes
}
If a property key is duplicated, the last declared file will ‘win’ and override.
Read this for more information and complete samples
https://www.mkyong.com/spring/spring-propertysources-example/
Hope it helps!

Spring boot Auto Configuration trying to load beans from dependent project

Spring Boot is loading Beans from external dependent jars .
How can we stop Autoconfigration of loading Bean from dependent jar .
Below is the annotation used .
#Configuration
#SpringBootApplication(excludeName = {"com.test.core"})
#ComponentScan(basePackages = {"com.test.myhazelcast"})
#EnableJpaRepositories
public class BelHazelcastApplication { ...
I want Spring boot not to configure any bean com.test.core.* which is a dependent module .
But Take beans that are presenst in com.test.myhazelcast.* package .
The excludeName attribute of SpringBootApplication is used to exclude an autoconfiguration class by its name, not to exclude a package from component scanning.
Because you SpringBootApplication with the default for the packages to component scan, it will component scan from whatever package BelHazelcastApplication is in.
If this package is "com.test" then that will include components in "com.test.core".
I'd remove this line:
#ComponentScan(basePackages = {"com.test.myhazelcast"})
and change this:
#SpringBootApplication(excludeName = {"com.test.core"})
to be:
#SpringBootApplication(basePackages = {"com.test.myhazelcast"})

Change search location in Spring boot 1.3.0

I would like externalize the configuration of my aplication, I use Spring Boot 1.3.0, and I kwnow that the class ConfigFileApplicationListener has a default value DEFAULT_SEARCH_LOCATIONS. How can I change the source of my configuration, before this class load the default properties source?
You can use #PropertySource annotation on your configuration class (which is annotated #SpringBootApplication or #Configuration).
Like this:
#SpringBootApplication
#PropertySource(value = { "classpath:other.properties", "file:./other-external.properties" }, ignoreResourceNotFound = true)
public class MyApplication {
It will search first other.properties in your classpath and then looks the external file other-external.properties right outside of your jar or war file. Latest files overwrites values of other files (if they exists)
See https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html for more details.

Resources