Conditional Beans in spring boot application

Introduction

As per spring-boot documentation, auto-configuration is implemented with standard @Configuration classes. Additional @Conditional annotations are used to constrain when the auto-configuration should apply. Usually, auto-configuration classes use @ConditionalOnClass and @ConditionalOnMissingBean annotations. This ensures that auto-configuration applies only when relevant classes are found and when you have not declared your own @Configuration.

image url
img logo

How to Change the Default Server Port in Spring-boot

Introduction

Spring Boot initial project (which is generated from spring starter) project comes with an embedded server i.e tomcat. The default port of embedded tomcat is 8080. Changing the default port for the embedded server has multiple ways. Let's find out how.

Using property files

The easiest way to customize Spring Boot embedded server port is by overriding the values of the default properties.

img logo

How to disable banner in spring boot

Introduction:

When your spring-boot application starts, you should see something similar to the following output (Banner):


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::   v2.3.2.RELEASE
     

The banner printed on start up can be changed by adding a banner.txt file to application classpath or by setting the spring.banner.location property to the location of such a file. And Banner is a functional interface, that can be customized.

image url
img logo

Why String and wrapper classes are immutable and final in java

Introduction:

Immutable objects are, objects whose state (data) cannot change after construction. String and Wrapper classes in java are immutable. (refer this link for more details)

Immutable objects simplify the program based on below:

  1. They are simple to construct, test and use.
  2. They are automatically thread-safe and won't face synchronization issues.
  3. They don't need a copy constructor or implementation of clone() and no need to copied defensively when used as fields.
img logo

Why do we need to override the equals and hashcode methods in java

Introduction:

When we need to use our object in the hashing based collections, we must override both equals() and hashCode() methods.

We must override hashCode() in every class that overrides equals(). Failure to do so will result in a violation of the general contract for Object.hashCode(), which will prevent your class from functioning properly in conjunction with all hash-based collections, including HashMap, HashSet, and Hashtable.   --from Effective Java, by Joshua Bloch
img logo

Copying object in java

Let us Assume an object- obj1, that contains two objects, containedObj1 and containedObj2.



shallow copying:

shallow copying creates a new instance of the same class and copies all the fields to the new instance and returns it. Object class provides a clone method and provides support for the shallow copying.

Deep copying:

A deep copy occurs when an object is copied along with the objects to which it refers. obj1 after a deep copy has been performed on it. Not only has obj1 been copied, but the objects contained within it have been copied as well. We can use Java Object Serialization to make a deep copy. Unfortunately, this approach has some problems too(detailed examples).

image url
img logo

Differences and Similarities between HashSet TreeSet and LinkedHashSet

Introduction:

TreeSet, LinkedHashSet and HashSet are implementation of Set interface from that, they follows contract of Set interface i.e. they do not allow duplicate elements.
Now in this post we will see the difference between HashSet, TreeSet and LinkedHashSet on different points like ordering elements, allowing null and performance ..etc.
img logo

Configuring two way SSL in Tomcat

Setting up Tomcat to provide self-signed SSL certificates allowing secure client/server communication and relatively easy to set up.  Java provides a handy command-line tool called keytool that you can use to generate keystores.

Generate the Client and Server Keystores : 

By following the instructions below, you will create two keystores:

clientkeystore.jks (for the client to use) and serverkeystore.jks (for the server to use). In order to provide copies of the client’s certificate to the server (and vice versa), you will export public certificates based on the private keys.

img logo

How to re-load properties files without restarting server in java

Re-load properties files without restarting server in Java:

Restart an application-server (servlet-container) means restart all the web-apps that are running under that application-server, with resulting unavailability of services. For this we’ll use the Java class PropertiesConfiguration available in the Apache Common Configuration library. This class allow to bind properties file to a PropertiesConfiguration Java Object through which read the single properties.

img logo

Remove duplicate elements in an Array

package com.test.blogger;

/**
 * @author EducateJava
 */
public class RemoveDuplicatesFromArray {

 public static void main(final String[] args) {

  final RemoveDuplicatesFromArray removeDuplicatesFromArray = new RemoveDuplicatesFromArray();
  final String[] finalArray = removeDuplicatesFromArray
    .removeDup(new String[] { "1", "one", "1", "three", "two", "2", "three", "1" });
  for (final String obj : finalArray) {
   System.out.println(obj);
  }
 }
img logo