how to Appoint yml file path - spring

I have use springboot to creat a project.
when I make yml file at src/main/resources/config or src/main/resources , I cant set :
spring:
profiles:
include:
- 'scheduling'
- 'yml'
in application.yml to appoint file file path,
and use #Value("${importance_info.message.notice}") to get the appointed value from
src/main/resources/config/application-scheduling.yml
but if I create yml not in the src/main/resources/config or src/main/resources, I can't get appointted value from it.
when I use #PropertySource(value = {"classpath:config/sub/sub-scheduling.yml"}), still can't get appointted.
Please tell me , how to make config or code to get appointed value from yml file in that file.

Related

Understanding the spring profile

I have basic idea how spring profile works. But here in this file this - i am not able to get it. And current Application.yml file mentioning the three profile which one will get active and when that i need to know as well. Below is the Application.yml file content.
spring:
application:
name:
profiles:
active:
-default
-local
-swaggerinfo
Note: i have three config files present in my resources. Also if i want to look another config file
then spring use the naming convention like Application-<Name>.extension . so - is already get added for the new config file then why we explicitly need
to put another one in our application.yml file under spring.profile.active.
Below are the names of the three config files present under the resources folder.
application.yml
application-local.yml
bootstrap-default.yml
But here in this file this - i am not able to get it. spring use the
naming convention like Application-.extension . so - is already
get added for the new config file then why we explicitly need to put
another one in our application.yml file under spring.profile.active
spring:
application:
name:
profiles:
active:
-default
-local
-swaggerinfo
The declaration of profiles are incorrect. You must either put space or should not use (-) at all.
spring:
profiles:
active:
- default
- local
- swaggerinfo
The Spring also supports the following way of declarations.
spring:
profiles:
active: default,local,swaggerinfo
or
spring:
profiles:
active:
default
local
swaggerinfo
Here default refers to application.properties file not bootstrap-default.properties. Also, You don't need to specify the default profile. Spring automatically use application.properties as default one. So, in your case it's appropriate to go with local and swaggerinfo.
current Aplication.yml file mentioning the three profile which one
will get active and when that i need to know as well.
Let's talk about the following declaration.
spring:
profiles:
active:
- local
- swaggerinfo
Both local and swaggerinfo profiles will be active for props loading. So,which means that all the three files (application.yml by default) will be consumed by spring.
Let's talk about the order.
The order in the above case would be
application -> application-local -> application-swaggerinfo
Note:
Assume that you've mentioned the same prop in all the three files then in that case the precedence will be given as per the order highlighted above i.e prop mentioned in the application-swaggerinfo will override the ones available in the other twos.

override command line Spring properties with bootstrap.yml

i have a tomcat box , which has spring active profile set already, as a command line argument every time a spring app is deployed through catalina.
i am using spring cloud config server , so in config client i specify active profile in bootstrap.yml , but as i mention earlier it is overriden by tomcat command line argument .
how to override the command line argument passed through tomcat , with my boostrap.yml at the time of bootstrap context loading so that i can pass active profile from my bootstrap.yml to config server.
Tomcat set environment command (which i cannot change as i dont have access)
JAVA_OPTS="$JAVA_OPTS -Djava.library.path=/path -Dspring.profiles.active=e2"
bootstrap.yml
spring:
profiles:
active: e2,cron
cloud:
config:
uri: http://localhost:8888
application:
name: heartbeat_monitor.
Command line argument(-Dspring.profiles.active=e2) will always override your properties file, no matter how many hardcoded profiles you specify in your yaml file. I would suggest you to add additional profile to be set programatically at the runtime and keep two property files with -profilename before the .yml extension.
This could be done as follows:
ApplicationMain.java
public static void main(String[] args) {
SpringApplication app = new SpringApplication(DemoApplication.class);
app.setAdditionalProfiles("cron");
app.run(args);
}
bootstrap-e2.yml
// Keep all the properties which is specific to e2 profile.
bootstrap-cron.yml
// Keep all the properties which is specific to cron profile.
In this way, you can keep both the profiles in use .But, if a property is common in both the bootstrap files, then program will pick the property from that bootstrap file whose profile matches with the runtime args : -Dspring.profiles.active

Springboot: how to set external location for profile file only?

I have an internal application.yml that specifies common properties and the profile:
spring.profiles.active: dev
spring.config.location: 'C:\externalConf\'
spring.application.name: myAppName
myproperty: aprop
Then I put my application-dev.yml in C:\externalConf\ but it seems it is not read.
Where is the problem?
You should pass config folder location like this:
spring.config.location=file:///C:externalConf/

Spring Application.Properties and Application-dev.properties files parameter conflict

profile logic in my application.properties file.My problem is when I use -dev ,it reads value from application-dev.properties.But I have same key which does not exist in my application-dev.properties but exist on application.properties,spring continue to read values from application.properties
application.properties
myfirstkey=x
mysecondkey=x
application-dev.properties
mysecondkey=dev
-Dspring.profiles.active=dev (// I pass profile value and see result by the way)
Output mysecondkey dev ,it is okay no problem
Output myfirstkey is x (my expectation is empty),but it doesnt exist in application-dev.properties?Is there anyway to prevent application.properties read
Well, application.properties are basically your 'default' .properties and you can't just (on a case-per-case basis) disable them, so the fix would be to do this in your -dev.properties.
myfirstkey=

How can/do I configure my Spring app to check a specific tag of configs with custom directory?

Suppose I have the following dir:
root.properties
dev-ci
common.properties
app_dir
app.properties
prod
stage
test
test/stage/prod all follow dev-ci's dir/path structure.
How can I setup my bootstrap.yml file so that when I start my app, it'll load the following:
root.properties
dev-ci/common.properties
dev-ci/app_dir/app.properties
Is there a way to set up my yml so that it takes some parameter from commandline? Or will I have to map out all the possible 'paths' then pass in some label/name?
Lastly, where does the tag name come into play?

Resources