Welcome to my blog. Here I write about technology, software development practice/processes and other stuff.
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. ╣
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.
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.
Before Spring Boot enterprise-level development creating an application with the Spring framework was so difficult. As a developer, you have to create and maintain the dependence injection, web configuration, deployment and etc. This also increase the learning curve for the developers.
Mike Youngstrom opened Spring JIRA # SPR-9888 feature JIRA on 17th Oct 2012. The idea was to reduce the learning curve of any new developer who will be coming in to create application using Spring's Web Application.
What is the Spring Boot?
Spring framework provides a pre-setup of Spring and some third party library using which developer could develop an application without any/much of configurations result in faster development.Non-functional features are readily available such as an embedded server, health check, security, metrics, and externalised configuration. If we want to differ from Spring-provided default configurations we could simply do so.
Spring Boot application could be bundled into a jar or a traditional war, as a result, it is easy to deploy. It also provides a command-line interface (CLI) to run the Spring script.
Why anyone uses Spring Boot?
Do prototyping.
Creating a minimum viable product for the actual market.
Create Microservice
How to use/implement Spring Boot?
Spring Boot requirements
Java 8 onwards supported
Maven 3.3+ version build tool supported
Gradle 6.3+ version build tool supported (5.6.x is also supported but is deprecated form)
You can also deploy Spring Boot applications to any Servlet 3.1+ compatible container.
Spring Boot is available in Groovy or Kotlin orJava language for application development.
To minimise the developer’s effort Spring provide Spring Initializr (https://start.spring.io/) to generate the basic structure of your project with all the dependency in it. The Spring Boot Command Line Interface (CLI) is a command-line tool that you can use to quickly prototype an application. As it uses Groovy script for it, which means there is no boilerplate code that still has flour of Java.
Creating a simple application
Open Spring Initializr (https://start.spring.io/) to generate the basic structure of your project with all the dependency in it.
Note all the official starters follow the naming pattern spring-boot-starter-*
It has aspring-boot-starter-parent parent. We included only Spring Web and Spring Initializr add the test for unit testing. We will see how to test Spring Boot in another blog.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Created a controller to make REST call RestContoller annotation let the Spring know this is a controller class RequestMapping maps HTTP requests to handler methods of MVC and REST controllers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
As a Java developer, we use POJO every day in our coding. We have to write or generate codes of accessor methods and other methods like 'equals', 'toString' and 'hashCode' or use a library like Lombok.
To make it more simplified Java introducing 'record'. Using which developer need not have to generate or write these code.
Some of the features of these 'record' class.
implicitly they are final so it cannot be extended or abstracted but can implement an interface.
java.lang.Record is the superclass of record
A record cannot define the native method.
A record field(s) is/are implicitly final
Let's see with an example.
A simple record with no boilerplate code. It provides field accessor method, 'toString', 'equals' and 'hashCode' readily available.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A record with multiple constructors and instance method.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A record with constructor level validation, static method and override 'equals' method.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In this class, we are accessing the field with the internal method and other methods like 'toString', 'equals' and 'hashCode'.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
As a developer if you want to control who can extend your class or interface.
In this enhancement of Java programming language, new keywords are introduced sealed, non-sealed and permit at the class level.
A sealed class or interface can be extended or implemented only by those classes and interfaces permitted to do so. If you try to implement or extend to other class which is not permitted then the code will not compile. For example,
public sealed class Point permits Line
A permit keyword defines which are the subclasses or interfaces can be extended or implemented from this class. For example,
public sealed class Point permits Line
Point class only permits Line class to extend directly.
A non-sealed class or interface will allow this class to be extended.
For example,
public non-sealed class Line
Permitted class Line is open for any unknown class extension.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A permitted subclass must define whether it could be extended to a specific class (sealed) or open to any unknown class(non-sealed) or not open for any extension (final).
This is an enhancement of Java language with pattern matching for the instanceof operator. Currently, this feature is in the second preview in JDK 15.
So currently when we need to cast an object, we check it using instanceof operation then only we cast the object to save our self from class cast exception.
In the above code snippet, we have to explicitly cast it to an object.
In this enhancement, we no more have to do it.
To run the below code you need to install JDK 14 or above and have to enable the preview feature of javac & java.
In order to enable that preview feature.
javac
--enable-preview
-release <version>
<Source file>
java
--enable-preview
<Compiled class>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A data type is an attribute in a programming language which define the domain of values and operations on that domain.
Define a certain domain of value.
Define operations allowed on those values.
Example
int type
Take only integer values
Operations supported: addition, subtraction, multiplication, bitwise operation etc.
float type
Take only floating values
Operations supported: addition, subtraction, multiplication, etc. (it does not support operations like bitwise and modulus).
Now, what is the user-defined data type?
A user data type is NOT an attribute in a programming language but defines by the user which define the domain of values.
In below example, we define the user-defined data type using class. Here Person class is the type which is consist of bunch of String, LocalDate and Address fields. Person class have a user define a data type of Address.
Example
public class Person { private String firstName; private String lastName; private LocalDate dateOfBirth; private Address address; ..... } public class Address { private String firstLine; private String lastLine, private State state; .... }
Abstract Data Types (ADT)
Abstract Data Types (ADT) are like user define types which define values and operations on that user define type without specifying how the operation will happen.
Example
In this example, we will define an ADT where will create a data type named MyString class and define operations like addition, subtraction, multiplication and division.
User Define Data Type
MyString is a wrapper object of string.
Let's define the operations of MyString data type.
User Define Operations
The addition will concatenation two MyString. Example: "Hello" + " world" will result in "Hello world".
The subtraction will substring the difference of length. If the difference of length is zero or negative then it will have an empty string. Example:
"Hello" - "world" will result in an empty string
"Hello" - "hi" will result in "Hel"
"hi" - "Hello" will also result in an empty string.
The division will remove common character(s) from MyString. Example: "Hello" / "world" will result in "He".
The multiplication will concatenate characters alternatively. Example: "Hello" * "world" will result in "Hweolrllod".
Actual Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Simple code to generate One Time Password.
We are using java.util.SplittableRandom which was introduced in Java 8.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters