Spring PropertySource based on environment - spring

I would like to use a dynamic placeholder inside the PropertySource value options.
This is in order to have the ability to have one file for each environment which overrides the default one. Just like application.properties and application-dev.properties.
Current setup:
#PropertySource("classpath:ione.properties")
I would like to have something like
#PropertySource("classpath:ione-{optionalEnvName}.properties")
Thus reading the --spring.profiles.active=dev option.
Thanks!

Run with:
-Dspring.profiles.active=dev
and then:
#PropertySource("classpath:ione-${spring.profiles.active}.properties")
should work

Related

logging.level.root doesn't work in springboot

I want to setting the default logging level to error on springboot
enter image description here
But the console still has the dubug and info output. It seems that logging.level.root=error doesn't work.
Be carefull if you are using Spring Boot Devtools, the properties defined in $HOME/.config/spring-boot folder will override all other properties as specified in Spring Boot documentation : https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html
I found a environment variable named debug,even though its value is a string not true,which caused the problem.Actually,I tried to remove the variable before,but I didn't restart the eclipse.Now,I remove the varibale named DEBUG and restart the eclipse,and it success.

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 to set the Web application context in Thorntail?

I'm looking for a way to customize the Web application name (so to change the Web context accordingly) in Thorntail. I assume that it can be done through the thorntail's maven plugin but I cannot find which is the property to set for it.
Thanks
By default, the context is /, which should be what you want most of the time. To customize it, you can use one of the following options:
1) Pass the thorntail.context.path system property when starting the uberjar: java -jar my-app-thorntail.jar -Dthorntail.context.path=my-context.
2) If you use project-defaults.yml, you can configure it there:
thorntail:
context:
path: my-context
3) Create a file WEB-INF/jboss-web.xml with this content:
<jboss-web>
<context-root>my-context</context-root>
</jboss-web>
(Under the hood, options 1 and 2 are actually transformed to 3, but you don't have to care.)

Liberty server properties file setup

I have properties file in local to which I am reading in code by below method
String pathOfFile = System.getProperties("arg.get.prop");
How to set this system properties to get my property file's path in liberty server.xml
You can specify environment variables in the server.env file placed either in ${wlp.install.dir}/etc/server.env or ${server.config.dir}/server.env. The server will also pick up variables from the current shell environment (server.env files take precedence). Then you can access the variables in the server.xml using the following notation:
${env.<variable name>}
For example, you can have the following in your server.env file:
HTTP_PORT=9001
and then in your server.xml:
<httpEndpoint id="defaultHttpEndpoint"
httpPort="${env.HTTP_PORT}"
httpsPort="9443" />
For more information on customizing the Liberty environment see: https://www.ibm.com/support/knowledgecenter/en/SSAW57_liberty/com.ibm.websphere.wlp.nd.multiplatform.doc/ae/twlp_admin_customvars.html
If you need to define system property the recommended way is to use jvm.options file and put your property there like:
# Set a system property.
-Darg.get.prop=ExampleValue
you may need to create that file in the ${server.config.dir} directory. For some more details check Customizing the Liberty environment
if your property file is in "variable=value" format .. then, you can include in in bootstrap.properties file of your liberty install.
bootstrap.properties can be used to supply variable values to liberty configuration. you can include additional files by specifying
bootstrap.include=

Spring command line JSON config containing array

I am using Grails 3 Elasticsearch plugin with Springs external JSON configuration by setting spring.application.json as system property.
The properties are available in the application but I can't find a way to initialize an array properly.
What I am trying to accomplish is to override the default values of the hosts property specified in my application.yml:
environments:
development:
elasticSearch:
client:
hosts:
- {host: "myhost.com", port: 9300}
- {host: "anotherhost.com", port: 9300}
I am setting the property from the command line as follows:
-Dspring.application.json={"environments":{"development":{"elasticSearch":{"client":{"hosts":[{"host":"override1.com", "port":9000},{"host":"override2.com", "port":9100}]}}}}}
I would expect environments.development.elasticSearch.client.hosts to contain an array like it does when initialized from the application.yml, but in fact environments.development.elasticSearch.client containes host[0] and host[1], where each contains the host and the port. The host array from the yml file is still there.
How can I achieve the same behavior using the command line as with the application.yml file?
I believe you can do this the same way that you would if it was set in a .properties file, using a list:
-Denvironments.developmet.elasticSearch.client.hosts={"host":"override1.com", "port":9000},{"host":"override2.com", "port":9100}
and I believe you can also do it as an environment variable...
set ENVIRONMENTS_DEVELOPMENT_ELASTICSEARCH_CLIENT_HOSTS='{"host":"override1.com", "port":9000},{"host":"override2.com", "port":9100}'
There may need to be some quotes around parts of these depending on the shell you are in, the OS, etc.

Resources