Showing posts with label Externalisation. Show all posts
Showing posts with label Externalisation. Show all posts

Sunday, March 28, 2021

Externalisation of Reloadable Properties Spring Application

org.springframework.context.support.ReloadableResourceBundleMessageSource 
.setBasename(String Full_Filepath_of_Properties_Files_Without_Prefix)

SetBaseName/SetBaseNames- Sets Filepath without extension
You can add Multiple Filepaths as well

getMessage(String key, PlaceHolder Args, Locale)
--------------------------------------------------------------
Load resource from the application root folder
"file:data.txt"

Load resource from classpath
classpath:data.txt"

Load resource from the filesystem
file:///c:/temp/filesystemdata.txt"

Load resource from URL
https://testsite.com/data.txt"

-----------------------------------------------------

https://dzone.com/articles/working-with-resources-in-spring

https://en.wikipedia.org/wiki/File_URI_scheme


A file URI takes the form of

file://host/path

Unix[edit]

Here are two Unix examples pointing to the same /etc/fstab file:

file://localhost/etc/fstab
file:///etc/fstab

Windows[edit]

Here are some examples which may be accepted by some applications on Windows systems, referring to the same, local file c:\WINDOWS\clock.avi

file://localhost/c$/WINDOWS/clock.avi
file:///c:/WINDOWS/clock.avi

where host is the fully qualified domain name of the system on which the path is accessible, and path is a hierarchical directory path of the form directory/directory/.../name. If host is omitted, it is taken to be "localhost", the machine from which the URL is being interpreted. Note that when omitting host, the slash is not omitted (while "file:///foo.txt" is valid, "file://foo.txt" is not, although some interpreters manage to handle the latter).

RFC 3986 includes additional information about the treatment of ".." and "." segments in URIs.

-----------------------------------------------------

@Bean @Scope(BeanDefinition.SCOPE_SINGLETON)

public ReloadableResourceBundleMessageSourceWrapper reloadableResourceBundleMessageSource(String externalFilePath) { ReloadableResourceBundleMessageSource reloadableResourceBundleMessageSource = new ReloadableResourceBundleMessageSource reloadableResourceBundleMsgSource.setBasename(externalFilePath);//Full Filepath of Properties/XML , file: or classpath: or relativePath reloadableResourceBundleMsgSource.setCacheSeconds(60);//After 60 Seconds, It tries to validate Cache again - Tries to read file again return reloadableResourceBundleMsgSource; }

--------------------------------------------------------------------

org.springframework.context.support.ReloadableResourceBundleMessageSource

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/support/ReloadableResourceBundleMessageSource.html

public final String getMessage(String code, 
                               @Nullable Object[] args,
                               Locale locale)
                        throws NoSuchMessageException


Specified by: getMessage(...) in MessageSource

Parameters:code the message code to look up, e.g. 'calculator.noRateSet'.

MessageSource users are encouraged to base message names on qualified classor package names, avoiding potential conflicts and ensuring maximum clarity.

args an array of arguments that will be filled in for params withinthe message (params look like "{0}", "{1,date}", "{2,time}" within a message),or null if nonelocale the locale in which to do the lookupReturns:the resolved message (never null)

Throws:NoSuchMessageException - if no corresponding message was found

--------------------------------------------------------------------

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/support/AbstractResourceBasedMessageSource.html

void setBasename(String basename)

Set a single basename, following the basic ResourceBundle convention of not specifying file extension or language codes.

void setBasename(String basename);

Set an array of basenames, each following the basic ResourceBundle convention of not specifying file extension or language codes.


About message resource keep note:
A plain path will be relative to the current application context.
A "classpath:" URL will be treated as classpath resource.
A "file:" URL will load from an absolute file system path.
Any other URL, such as "http:", is possible too.

--------------------------------------------------------------------

//Set the number of milliseconds to cache loaded properties files.
void	setCacheMillis(long cacheMillis)

//Set the number of seconds to cache loaded properties files.
void	setCacheSeconds(int cacheSeconds)


Tuesday, January 19, 2021

Externalisation of Configuration - Spring Boot --spring.config.location --spring.config.additional-location

https://www.baeldung.com/spring-properties-file-outside-jar

https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html

----------------------------------------------------------------------------

By default, Spring Boot looks for "application.properties", "application.yml" inside
locations defined by "spring.config.location" Application Java Arguments

Note :
-Dfoo.bar=fubar (JVM Arg) (-D)
--foo.bar=fubar (Application Arg) (--)

If If you prefer to add additional locations, rather than replacing them, you can use "spring.config.additional-location"

Spring Boot - will look for Files in these config Locations apart from Usual Config Directories inside the Project and read them and Will be used to fill Values from @Value Annotations at Startup 

@Configuration
public class SystemPropertiesFilepaths {

	@Value("${applicationProperties}")
	private String applicationProperties;

	@Value("${processDetails}")
	private String processDetails;

	@Value("${databaseProperties}")
	private String databaseProperties;
}
  //Getters or Field Level Accessors used to fetch Values 

----------------------------------------------------------------------------

export SPRING_CONFIG_NAME=application,jdbc export SPRING_CONFIG_LOCATION=file:///Users/home/config

java -jar app.jar --spring.config.name=application,jdbc --spring.config.location=file:///Users/home/config

----------------------------------------------------------------------------

Using the Default Location

By convention, Spring Boot looks for an externalized configuration file – application.properties or application.yml – in 4 predetermined locations in the following order of precedence:

  • /config subdirectory of the current directory

Using the Command Line

We can also pass a folder location where the application will search for the file:

java -jar app.jar --spring.config.name=application,jdbc --spring.config.location=file:///Users/home/config


Using Environment Variables

Or, let's say that we can't alter the start-up command. What's great is Spring Boot will also read the environment variables SPRING_CONFIG_NAME and SPRING_CONFIG_LOCATION

export SPRING_CONFIG_NAME=application,jdbc
export SPRING_CONFIG_LOCATION=file:///Users/home/config
java -jar app.jar


2.3. External Application Properties

Spring Boot will automatically find and load application.properties and application.yaml files from the following locations when your application starts:

  1. The classpath root

  2. The classpath /config package

  3. The current directory

  4. The /config subdirectory in the current directory

  5. Immediate child directories of the /config subdirectory

The list is ordered by precedence (with values from lower items overriding earlier ones). Documents from the loaded files are added as PropertySources to the Spring Environment.

If you do not like application as the configuration file name, you can switch to another file name by specifying a spring.config.name environment property. You can also refer to an explicit location by using the spring.config.location environment property (which is a comma-separated list of directory locations or file paths). The following example shows how to specify a different file name:

$ java -jar myproject.jar --spring.config.name=myproject

The following example shows how to specify two locations:

$ java -jar myproject.jar --spring.config.location=optional:classpath:/default.properties,optional:classpath:/override.properties
Use the prefix optional: if the locations are optional and you don’t mind if they don’t exist.
spring.config.name and spring.config.location are used very early to determine which files have to be loaded. They must be defined as an environment property (typically an OS environment variable, a system property, or a command-line argument).

If spring.config.location contains directories (as opposed to files), they should end in / (at runtime they will be appended with the names generated from spring.config.name before being loaded). Files specified in spring.config.location are used as-is. Whether specified directly or contained in a directory, configuration files must include a file extension in their name. Typical extensions that are supported out-of-the-box are .properties.yaml, and .yml.

When multiple locations are specified, the later ones can override the values of earlier ones.

Locations configured by using spring.config.location replace the default locations. For example, if spring.config.location is configured with the value optional:classpath:/custom-config/,optional:file:./custom-config/, the complete set of locations considered is:

  1. optional:classpath:custom-config/

  2. optional:file:./custom-config/

If you prefer to add additional locations, rather than replacing them, you can use spring.config.additional-location. Properties loaded from additional locations can override those in the default locations. For example, if spring.config.additional-location is configured with the value optional:classpath:/custom-config/,optional:file:./custom-config/, the complete set of locations considered is:

  1. optional:classpath:/

  2. optional:classpath:/config/

  3. optional:file:./

  4. optional:file:./config/

  5. optional:file:./config/*/

  6. optional:classpath:custom-config/

  7. optional:file:./custom-config/

This search ordering lets you specify default values in one configuration file and then selectively override those values in another. You can provide default values for your application in application.properties (or whatever other basename you choose with spring.config.name) in one of the default locations. These default values can then be overridden at runtime with a different file located in one of the custom locations.

Azure - Pipeline - Add Approver for Stage

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/approvals?view=azure-devops&tabs=check-pass