Notice

╠ This is my personal blog and my posts here have nothing to do with my employers or any other association I may have. It is my personal blog for my personal experience, ideas and notes. ╣

Wednesday, January 19, 2022

Using the headless mode in Spring Boot

What is headless mode in Java?  

When an application does not require display, keyboard or mouse then system could be set as headless mode for the application. It could be done by setting the property System.setProperty("java.awt.headless", "true")

So why anyone would require headless mode / configuration? 

Let's say you are creating an application which convert an image into ACSII characters. This work does not require a mouse, keyboard or monitor. As this application will pick-up image from a predefined location and convert that image into ACSII characters. 
This is done to use the most of the computing power of the system. As image is converted in headless mode system then pass on to the headful system for the rendering of ASCII characters in display. 

In case of Spring Boot image banner printing, it is done using "java.awt.headless" system properties as true to print the image banner. The property is reset once the image banner printing is done. 



Saturday, January 8, 2022

Customise Spring Boot Banner

When we run a Spring Boot application  we see a Spring Boot banner. I wonder if we could able to change this or not and find out that Spring gives us the opportunity to change it. Why anyone should do that? Answer will be just for fun and learning. 


Now we want to change the banner by simply adding a new banner text file with my custom ASCII Art in it and point this banner file to Spring Boot properties. In spring.banner.location have the banner file location information. 
spring.application.name=My Custom Banner
spring.banner.location=classpath:myBanner.txt

myBanner.txt

put it into class path

 

Now let's run the Spring Boot application and see the result. 


Now we will see how to use an image (*.png/*.jpg/*.gif) as a banner. 

To implement image we need to get an image of *.png/*.jpg/*.gif type and put it into the class path and set the path in the Spring Boot property spring.banner.image.location=classpath:myBanner.png here my image file name is myBanner.png. 

  • spring.main.banner-mode where we mention the banner mode console | log | off  and the default value is console. console will print the banner in console and log in log file and off will turn of the banner. 
  • spring.banner.image.location where we put the banner image file location and the default value is classpath:banner.gif.
  • spring.banner.image.height where we put an integer to specify the height of the image and the default value will be based on the image height. 
  • spring.banner.image.width where we put an integer to specify the width of the image in chars and the default value is 76.
  • spring.banner.image.margin where we put an integer to specify the margin from the left hand image margin in chars and the default value is 2.
  •  spring.banner.image.bitdepth is used for ANSI colour and where we use two allowed values 4 (16 colour) or 8 (256 colour).
  • spring.banner.image.invert is set to true then the dark terminal themes will be used for and the default value is false
  • spring.banner.image.pixelmode is to determine the pixel used to render the image, where we can use TEXT | BLOCK and the default value is TEXT. TEXT will use ' ', '.', '*', ':', 'o', '&', '8', '#', '@'  to render image and BLOCK will use ' ', '░', '▒', '▓', '█'  to render image.
Now see a sample 
spring.application.name=My Custom Banner
spring.main.banner-mode=console
spring.banner.location=classpath:myBanner.txt
spring.banner.image.pixelmode=text
spring.banner.image.location=classpath:myBanner.png
spring.banner.image.height=10
spring.banner.image.width=30
spring.banner.image.margin=2
spring.banner.image.bitdepth=8
spring.banner.image.invert=false

I will be using this 32 bit colour myBanner.png

myBanner.png
myBanner.png



Output in console 
Banner as a TEXT



Now lets see what will happen if we put the dark terminal themes by using spring.banner.image.invert=true

Inverted image render


 
Let's render the image in BLOCK by using spring.banner.image.pixelmode=block

Banner as a BLOCK



Now put the banner in the log file by using spring.main.banner-mode=log

Banner in log file



This help us to understand how plug-n-play is Spring Boot.