How to get access and customize Freemarker's Configuration object in spring-boot 2.x? - spring-boot

How to get access and customize Freemarker's Configuration object in spring-boot 2.x?
It allows to set square bracket syntax as default option with something like:
Configuration#setTagSyntax(Configuration.SQUARE_BRACKET_TAG_SYNTAX)
which is not possible with any of spring.freemarker.* configuration property.
Also, it should be possible to introduce default imports having access to direct configuration of this object.

It's possible to set any FreeMarker configuration settings with spring.freemarker.settings.<settingName>, like spring.freemarker.settings.tagSyntax = square_bracket. See the JavaDoc of Configuration.setSetting(String, String) for more (https://freemarker.apache.org/docs/api/freemarker/core/Configurable.html#setSetting-java.lang.String-java.lang.String-). This is the method to which Spring delegates the assignments under spring.freemarker.settings; Spring itself doesn't know what "settings" exist or how to parse them.

Related

Observation on the priority order of application properties in Spring Boot

When using Spring Cloud config server, I have observed the below behavior. Please let me know, if my hypothesis is correct regarding the behavior.
If the application-${env}.yaml/properties have the server.port property set, I cannot override the value, even by passing -Dserver.port
If I do not inherit the property defined in the spring cloud config, then I will be able to provide the value inside the application.yaml/property of the application
If the property is defined inside the application's application.property/yaml, I can override the value from the command line by passing the -Dserver.port option.
Is my assumptions right based on the above behavior.
Yes, spring cloud config value can't overwrite by default . We can change to override with property pring.cloud.config.allowOverride=true
https://cloud.spring.io/spring-cloud-static/spring-cloud.html#overriding-bootstrap-properties

Can property name be given alias

I'm using a 3rd party Spring Boot module that requires a "validation.url" property to be set
This same value exists in our centralized config service as "url.for.validation", which we retrieve via a PropertyPlaceholderConfigurer
Outside of intercepting the Properties from the PropertyPlaceholderConfigurer and adding a new one for "validation.url", what is the recommended way to achieve this?
Is there any alias functionality for properties?

Using Expressions in Spring application.properties file

Can expressions be used as a right-hand-side value in a Spring application.properties file?
For example, something like this:
logging.level.com.acme=#{'${MY_RUN_ENV}'=='PROD'?'WARN':'DEBUG'}
That, specifically, does not work. But, I'm wondering if I can do something similar to what's intended there
No you can not use SpEL within properties files.
Finally, while you can write a SpEL expression in #Value, such
expressions are not processed from Application property files.
You can however use placeholders within properties files, eg:
app.name=MyApp
app.description=${app.name} is a Spring Boot application
For your use case, you should look at the profile-specific configuration mechanism.
Which allows you to load different config based on an environment profile.
No this is not possible, From spring boot reference:
Feature #ConfigurationProperties
SpEL evaluation No
Instead you can have an application-default.properties in production and in it define loglevel=WARN.
And in your application.properties:
loglevel=DEBUG
logging.level.com.acme=${loglevel}
The profile-specific properties file(-default by default) should override the properties from application.properties, more info here.
Use profile based properties file.
In application-dev.properties :
logging.level.com.acme=WARN
and in application-prod.properties :
logging.level.com.acme=DEBUG
FYI when spring boot doesn't find a propertie in a profile based file it use the value in the default one . So you can set properties in application.properties and override them in a profile based file when their value changed.

Set/override Spring / Spring Boot properties at runtime

At the project with Spring Boot we use application.properties but need to configure some of these properties (like port number of logging level) based on an external configuration. We access the configuration via API so it is known only at runtime.
Is there a way to override or set some Spring properties at runtime (for example using a bean) and if yes how can this be achieved?
You could do this with Spring Cloud Config
Just for the purpose of illustration, here's a relatively quick way to see dynamic property overrides at runtime:
First, for your bean to be able to pick up changed properties, you need to annotate it with
#RefreshScope
Add the spring cloud dependency to your spring boot app, eg for gradle
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter', version: '1.1.1.RELEASE'
( NB You also need the spring boot actuator dependency.)
With the app running, you can view your current config at eg
http://localhost:8080/env
eg if you have a property 'my.property' in application.properties, you'll see something like:
"applicationConfig: [classpath:/application.properties]": {
"my.property": "value1",
etc
To change the value, POST my.property=value2 to /env as application/x-www-form-urlencoded
eg
curl -X POST http://localhost:8080 -d my.property=value2
GET /env again and you'll see the new value appears under the "manager" section
To apply the changed properties, do an empty POST to /refresh. Now your bean will have the new value.
Could you use system properties to pass in the variable? If you configure the PropertyPlaceholderConfigurer you can set the precedence of system properties vs file properties.
For example, something like:
#Bean public PropertyPlaceholderConfigurer placeHolderConfigurer() {
PropertyPlaceholderConfigurer props = new PropertyPlaceholderConfigurer()
props.setSystemPropertiesMode( PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE )
props.setLocations(new
PathMatchingResourcePatternResolver().getResources("classpath:/**.properties"));
props
}
The above would load your .properties file, but we set the priority to be system variables first, so if you set a system variable that will override the same variable in the config.
Alternatively, looking at the docs, Spring recommends defining a search order in your Environment:
[PropertyPlaceholderConfigurer is still appropriate for use when]
existing configuration makes use of the "systemPropertiesMode" and/or "systemPropertiesModeName" properties. Users are encouraged to
move away from using these settings, and rather configure property
source search order through the container's Environment; however,
exact preservation of functionality may be maintained by continuing to
use PropertyPlaceholderConfigurer.
Hopefully one of the above should sort out what you need?

Extended Properties for Spring Framework

Looking for a solution that will provide us more functionality within Spring properties such as:
nested structures
maps/lists
properties referencing other properties. Example:
city.name=Toronto
city.address=#{city.name}, 123 Ave SW
I tried EProperties (Google) and Commons Configurations (Apache) but doesn't seem to integrate very well with the Spring Framework.
Also, we're using Velocity to access properties using #springMessage("city.address"), so it needs to work for that.
Does anyone know how I can achieve the above by extending the default Properties capability?
With newest versions of Spring you can use the PropertySource mechanism. You register all your PropertySource and the order in which they are searched and then you don't have to do anything, except perhaps add this to your XML:
<context:property-placeholder />
As long as you declare only one of these without specifying local property files (the "old way"), you will be able to reference property A as the value of property B, even if they are not in the same property source.
For nested structures this may help if you don't like the properties readability:
https://stackoverflow.com/a/13470704/82609
For parsing problems you can easily handle lists and other stuff like that manually very easily:
Reading a List from properties file and load with spring annotation #Value

Resources