Monday, February 1, 2021

Spring Boot - ConfigurationProperties

Works in SpringBoot Only

Check Following properties file 

#Simple properties
mail.hostname=host@mail.com
mail.port=9000
mail.from=mailer@mail.com

Properties file directly mapped to Class Fields


Use @ConfigurationProperties 

@ConfigurationProperties(prefix = "mail") 
public class ConfigProperties { 

    private String hostName; 
    private int port; 
    private String from; 

    // standard getters and setters 
}

org.springframework.boot.context.properties.ConfigurationProperties;

How to get hold of Application Context when from Non-Spring resource



Make a static variable as appContext in the helper class
Make the helper Class Spring Managed by @Component/@Configuration
Make it autowired on setter method
and make static getter method

Why Should I Use Constructor Injection?

Linux Command Line Bash Shortcuts

https://www.tecmint.com/linux-command-line-bash-shortcut-keys/

In this article, we will share a number of Bash command-line shortcuts useful for any Linux user. These shortcuts allow you to easily and in a fast manner, perform certain activities such as accessing and running previously executed commands, opening an editor, editing/deleting/changing text on the command line, moving the cursor, controlling processes etc. on the command line.
Although this article will mostly benefit Linux beginners getting their way around with command line basics, those with intermediate skills and advanced users might also find it practically helpful. We will group the bash keyboard shortcuts according to categories as follows.

Launch an Editor

Open a terminal and press Ctrl+X and Ctrl+E to open an editor (nano editor) with an empty buffer. Bash will try to launch the editor defined by the $EDITOR environment variable.

Nano Editor

Controlling The Screen

These shortcuts are used to control terminal screen output:
  • Ctrl+L – clears the screen (same effect as the “clear” command).
  • Ctrl+S – pause all command output to the screen. If you have executed a command that produces verbose, long output, use this to pause the output scrolling down the screen.
  • Ctrl+Q – resume output to the screen after pausing it with Ctrl+S.

Move Cursor on The Command Line

The next shortcuts are used for moving the cursor within the command-line:
  • Ctrl+A or Home – moves the cursor to the start of a line.
  • Ctrl+E or End – moves the cursor to the end of the line.
  • Ctrl+B or Left Arrow – moves the cursor back one character at a time.
  • Ctrl+F or Right Arrow – moves the cursor forward one character at a time.
  • Ctrl + Left Arrow or Alt+B or Esc and then B – moves the cursor back one word at a time.
  • Ctrl + Right Arrow or Alt+C or Esc and then F – moves the cursor forward one word at a time.

Search Through Bash History

The following shortcuts are used for searching for commands in the bash history:
  • Up arrow key – retrieves the previous command. If you press it constantly, it takes you through multiple commands in history, so you can find the one you want. Use the Down arrow to move in the reverse direction through the history.
  • Ctrl+P and Ctrl+N – alternatives for the Up and Down arrow keys, respectively.
  • Ctrl+R – starts a reverse search, through the bash history, simply type characters that should be unique to the command you want to find in the history.
  • Ctrl+S – launches a forward search, through the bash history.
  • Ctrl+G – quits reverse or forward search, through the bash history.

Delete Text on the Command Line

The following shortcuts are used for deleting text on the command line:
  • Ctrl+D or Delete – remove or deletes the character under the cursor.
  • Ctrl+K – removes all text from the cursor to the end of the line.
  • Ctrl+X and then Backspace – removes all the text from the cursor to the beginning of the line.

Transpose Text or Change Case on the Command Line

These shortcuts will transpose or change the case of letters or words on the command line:
  • Ctrl+T – transposes the character before the cursor with the character under the cursor.
  • Esc and then T – transposes the two words immediately before (or under) the cursor.
  • Esc and then U – transforms the text from the cursor to the end of the word to uppercase.
  • Esc and then L – transforms the text from the cursor to the end of the word to lowercase.
  • Esc and then C – changes the letter under the cursor (or the first letter of the next word) to uppercase, leaving the rest of the word unchanged.

Working With Processes in Linux

The following shortcuts help you to control running Linux processes.
  • Ctrl+Z – suspend the current foreground process. This sends the SIGTSTP signal to the process. You can get the process back to the foreground later using the fg process_name (or %bgprocess_number like %1%2 and so on) command.
  • Ctrl+C – interrupt the current foreground process, by sending the SIGINT signal to it. The default behavior is to terminate a process gracefully, but the process can either honor or ignore it.
  • Ctrl+D – exit the bash shell (same as running the exit command).

Bash Bang (!) Commands

In the final part of this article, we will explain some useful ! (bang) operations:
  • !! – execute last command.
  • !top – execute the most recent command that starts with ‘top’ (e.g. !).
  • !top:p – displays the command that !top would run (also adds it as the latest command in the command history).
  • !$ – execute the last word of the previous command (same as Alt +., e.g. if last command is ‘cat tecmint.txt’, then !$ would try to run ‘tecmint.txt’).
  • !$:p – displays the word that !$ would execute.
  • !* – displays the last word of the previous command.
  • !*:p – displays the last word that !* would substitute.
For more information, see the bash man page:
$ man bash 
That’s all for now! In this article, we shared some common and useful Bash command-line shortcuts and operations. Use the comment form below to make any additions or ask questions.

Tags

Scroll back to top

@Value Properties in Spring - How to use and configure ?

First We need to tell Spring where are the property files located
We can use xml-based or annotation based approach

Xml-Based Approach -  <context:property-placeholder>
 
1
2
3
<context:property-placeholder location="classpath:foo.properties"/>
<context:property-placeholder location="file:bar.properties"/>
<context:property-placeholder location="${databaseProperties}.properties"/>

"classpath:foo.properties" ---> Searches in Classpath
"file:bar.properties"      ---> Searches in filepath
This is Spring format having "file:" as prefix

If you want to make Filepath Dynamic - We can save the path and pass it to JVM Arg Value, For Example
 
-DdatabaseProperties="file:C:/Users/Karan/Desktop/ExternalConfig/database"

We can use these properties by using
@Value + @Component /@Configuration - which can make class Bean Managed
-------------------------------------------------------------------------------------
https://www.baeldung.com/spring-value-annotation
https://www.baeldung.com/properties-with-spring
https://www.baeldung.com/spring-autowire
-------------------------------------------------------------------------------------
Using Annotation
@PropertySource("classpath:foo.properties")
@PropertySource("classpath:bar.properties")
public class PropertiesWithJavaConfig {
    //...
}

Default values can be provided for properties that might not be defined. Here, the value some default will be injected:

@Value("${unknown.param:some default}")
private String someDefault;

When we use the @Value annotation, we're not limited to a field injection. We can also use it together with constructor injection.

Let's see this in practice:

@Component
@PropertySource("classpath:values.properties")
public class PriorityProvider {

    private String priority;

    @Autowired
    public PriorityProvider(@Value("${priority:normal}") String priority) {
        this.priority = priority;
    }

    // standard getter
}

Sunday, January 31, 2021

Log4j2 Properties YAML XML - How to tell Application where to find log4j2 Configuration ?

 http://logging.apache.org/log4j/2.x/manual/configuration.html

https://stackoverflow.com/questions/28572783/no-log4j2-configuration-file-found-using-default-configuration-logging-only-er/38326259


-Dlog4j.configurationFile="file:///C:/Users/Karan/Desktop/ExternalConfig/log4j2.xml"


Log4j has the ability to automatically configure itself during initialization. When Log4j starts it will locate all the ConfigurationFactory plugins and arrange them in weighted order from highest to lowest. As delivered, Log4j contains four ConfigurationFactory implementations: one for JSON, one for YAML, one for properties, and one for XML.


Log4j will inspect the "log4j.configurationFile" system property and, if set, will attempt to load the configuration using the ConfigurationFactory that matches the file extension. Note that this is not restricted to a location on the local file system and may contain a URL.

If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath.

If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.

If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.

If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.

If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.

If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath.

If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.

If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.

If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.

Saturday, January 30, 2021

Types of Spring @Autowired

 We can implement dependency injection with:

https://www.baeldung.com/properties-with-spring

https://reflectoring.io/constructor-injection/

  • constructor-based injection
@Component
class Sandwich {

  private Topping toppings;
  private Bread breadType;

  @Autowired
  Sandwich(Topping toppings, Bread breadType) {
    this.toppings = toppings;
    this.breadType = breadType;
  }

}

  • setter-based injection
@Component
class Cookie {

  private Topping toppings;

  @Autowired
  void setTopping(Topping toppings) {
    this.toppings = toppings;
  }
}

  • field-based injection.
@Component
class Pizza {

  @Autowired
  private Topping toppings;
}

Azure - Pipeline - Add Approver for Stage

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