Posts

Send Email Using Spring Boot

Image
Hello everyone, today we will learn how to send an email via SMTP in Spring Boot. Technologies used:  Spring Boot 2.1.1.RELEASE  Java Mail 1.6.2 Maven 3  Java 8 Project Structure: Dependency Managemen(pom.xml): spring-boot-starter-mail will pull the JavaMail dependencies. <?xml version = "1.0" encoding = "UTF-8" ?> <project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0  https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0.0 </modelVersion> <parent> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-starter-parent </artifactId> <version> 2.1.1.RELEASE </version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId&

Java - static keyword in Java with example

Java static keyword is used to create a Class level variable in java. static variables and methods are part of the class, not the instances of the class. The static keyword in Java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks, and nested classes. static is a non-access modifier in Java that is applicable for the following: blocks variables methods nested classes Interface static method(Java 8 onwards) Java static variable We can use static keyword with a class-level variable. A static variable is a class variable and doesn’t belong to the Object/instance of the class. Since static variables are shared across all the instances of Object, they do not thread-safe. Usually, static variables are used with the final keyword for common resources or constants that can be used by all the objects. public class KnowledgeFactoryStaticDemo { // static variable example private static int count; public stat

Java -Stream API Introduction with Example

Java 8 introduced the concept of stream, Stream represents a sequence of objects from a source, which supports aggregate operations.   Following are the characteristics of a Stream − It takes input from the Collections, Arrays, or I/O channels. It never stores the elements. Stream supports intermediate operations like filter, map, limit, reduce, find, match, and so on. Streams only provide the result as per the pipelined methods. Stream operations do the iterations internally over the source elements provided. Different Operations On Streams-   forEach The stream has provided a new method ‘forEach’ to iterate each element of the stream   List < Integer > obj = new ArrayList <>(); obj.add( 7 ); obj.add( 12 ); obj.add( 3 ); obj.add( 5 ); obj.forEach((o) -> System.out.println( "Item : " + o)); map The map method is used to map the items in the collection to other objects according to the Function passed as the argument. List < Str

Configuring Tomcat Connection pool and Hikari Connection pool in Spring Boot Application

Image
One key component of spring boot starter dependencies is spring-boot-starter-data-JPA. This allows us to use JPA and work with production databases by using some popular JDBC connection pooling implementations, such as HikariCP or Tomcat JDBC Connection Pool. Tomcat Connection Pooling Spring Boot will look for HikariCP on the classpath and use it by default when present. To configure a Tomcat JDBC connection pool instead of the default HikariCP, we'll exclude HikariCP from the spring-boot-starter-data-JPA dependency and add the tomcat-JDBC Maven dependency. Maven (pom.xml) <dependency> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-starter-data-jpa </artifactId> <exclusions> <exclusion> <groupId> com.zaxxer </groupId> <artifactId> HikariCP </artifactId> </exclusion> </exclusions> </dependency> <dependency> &