In the post “Setting Filter at Handler level”, I showed how to set filter at handler level. In this post I will explain how to set filter at logger level. An use case of setting filter at logger level, is restricting logging of messages based on the user who logged it. Custom Filter 1 package…… Continue reading Setting Filter at Logger level
Setting Filter at Handler level
Java logging provides log levels at logger and handler level, to filter the messages. To make sure only required messages are logged. We can extend this functionality and provide our own filter, which will filter messages based on message’s metadata in addition to filtering by log levels. We can add this filter at logger level…… Continue reading Setting Filter at Handler level
Supplier Functional Interface Example
Java 8 added a new functional interface named “Supplier”. The purpose of this interface is to supply instances. I will be explaining using the below example. In the example I have want of list of employees. I will separate the logic behind employee instance creation and adding to list. The employee instance will be created…… Continue reading Supplier Functional Interface Example
Consumer Functional Interface Example
Java 8 added a new functional interface named “Consumer”. The purpose of this interface is to consume/process the information received, it doesn’t return anything. I will be explaining using the below example. In the example I have list of employees whom I want to terminate. I will pass the list of employees to an implementation…… Continue reading Consumer Functional Interface Example
BiPredicate Simple Example
This post explains BiPredicate functional interface. BiPredicate is similar to Predicate functional interface except BiPredicate takes two arguments instead of one. In the below example we will filter list of Person entities based on first and last name. The predicate class name will be “NamePredicate”. NamePredicate package Function; import java.util.function.BiPredicate; public class NamePredicate implements BiPredicate…… Continue reading BiPredicate Simple Example
Different ways to convert collection to an array
This post explains two ways through which we can convert a collection to an array. The collection can be a list or set. First approach is simply calling “toArray” on collection instance. This method returns an Object array, When converting to array it doesn’t consider the data type of the entries in the collection. Below…… Continue reading Different ways to convert collection to an array
Different ways to convert list to array
This post explains two ways through which we can convert a list to array. First approach is simply calling toArrayList on list instance. This method returns an Object array, When converting to array it doesn’t consider the data type of the entries in the list. Below is the code snippet list.toArray(); The second approach involves…… Continue reading Different ways to convert list to array
Creating a custom log message format
This post explains how to create a custom log message format. Out of the box java provides two log formats SimpleFormatter and XMLFormatter. Both extends the abstract super class Formatter. We can create our own custom format. Below is the code of the custom format CustomFormatter 1 package Logging; 2 3 import java.util.logging.Formatter; 4 import…… Continue reading Creating a custom log message format
Stubbing final methods
This post explains how to stub a final method. The class to be tested is as shown below Class1 package package5; public class Class1 { public final int method1(int value) { return value * value; } } The test class is as shown below Test Class 1 package package5; 2 3 import org.junit.Before; 4 import…… Continue reading Stubbing final methods
Spring depends-on attribute
When Spring loads the application context, it creates the beans following its own particular order. Irrespective of the order in which it creates the beans, it makes sure that a bean’s dependency (which are other beans) are set before the beans is ready of use. Below code will give you an example of what I am…… Continue reading Spring depends-on attribute