Java
Sending Email with Spring MVC Using Template
This tutorial walks through a small Spring MVC application that sends a templated e-mail, using Thymeleaf to render the message body and Jakarta Mail to deliver it. It assumes you are comfortable with Java EE development and with building Spring MVC applications. Note: This post has been updated for Spring Framework 7 and Thymeleaf 3.1. The original version rendered the message with Apache Velocity, but Spring deprecated its Velocity support in 4.3 and removed it entirely in 5.0; templating is now handled by Thymeleaf, and the mail dependency has moved from javax.mail to Jakarta Mail (jakarta.mail).
Java Collections Framework
Almost all collections in Java are derived from the java.util.Collection interface. Collection defines the basic parts of all collections. The interface states the add() and remove() methods for adding to and removing from a collection respectively. Also required is the toArray() method, which converts the collection into a simple array of all the elements in the collection. Finally, the contains() method checks if a specified element is in the collection. The Collection interface is a subinterface of java.lang.Iterable, so any Collection may be the target of a for-each statement. (The Iterable interface provides the iterator() method used by for-each statements.) Additionally, Collection is a generic, so any collection can be written to store any class. For example, a Collection<String> can hold strings, and the elements can be used as strings without any casting required.
Spring AOP Example Using Annotation
Spring Framework is developed on two core concepts – Dependency Injection and Aspect-Oriented Programming (AOP). Today we will look into the core concepts of Aspect-Oriented Programming and how we can implement it using Spring Framework. Note: This post has been updated for Spring Framework 7.0. The original 2014 version targeted Spring 4.1 with AspectJ 1.8 and the aspectjrt + aspectjtools dependencies; it now uses aspectjweaver 1.9.25 on the current Spring 7.0 GA line. Because Spring 7 builds on Jakarta EE, the servlet types in the examples now come from jakarta.servlet.* rather than javax.servlet.*. The AOP concepts and @AspectJ annotations themselves are unchanged.
The String Constant Pool
The String Constant Pool is a JVM-wide cache of string literals: when different parts of your code reference a string with the same content, they can share a single String object instead of allocating a new one each time. This is possible only because String is immutable in Java, combined with the String interning mechanism. In other words, the pool is an application of the Flyweight design pattern. String Pool Examples String s1 = "Hello"; String s2 = "Hello"; String s3 = "Hel" + "lo"; String s4 = "Hel" + new String("lo"); String s5 = new String("Hello"); String s6 = s5.intern(); System.out.println(s1 == s2); // true System.out.println(s1 == s3); // true System.out.println(s1 == s4); // false System.out.println(s4 == s5); // false System.out.println(s1 == s6); // true Explanation The references of s1, s2, s3, and s6 all point to the same pooled object, while s4 and s5 do not. Here is why:
Difference between wait() and sleep(), yield() in Java
The difference between wait() and sleep(), or between sleep() and yield(), is one of the oldest questions asked in Java interviews. All three pause the currently running thread, but they differ in who owns them, whether they release the lock, and how the thread wakes up again. TL;DR sleep() yield() wait() Defined in Thread Thread Object Static method? Yes Yes No Releases the lock/monitor? No No Yes Must hold the monitor (be in synchronized)? No No Yes Wakes up when timeout expires or interrupted the scheduler decides (maybe immediately) notify()/notifyAll(), timeout, or interrupt Typical use pause for a fixed time hint to give up the CPU coordination between threads JavaDoc Definition Thread Class public static void sleep(long millis[, int nanos]) throws InterruptedException
Object Initialization Order in Java
An object is a chunk of memory bundled with the code that manipulates memory. In the memory, the object maintains its state (the values of its instance variables), which can change and evolve throughout its lifetime. To get a newly-created object off to a good start, its newly-allocated memory must be initialized to a proper initial state. Here we take an in-depth look at the mechanisms Java uses to manage object initialization.
HashMap Internal Implementation Analysis in Java
HashMap is one of the most common topics in Java interviews, and “How does HashMap work?” has a deceptively simple one-line answer: “On the principle of hashing.” To say anything more, you need to know how hashing works and how HashMap applies it internally. This post assumes you already know the basics of using a HashMap; if not, start with the official Java docs. The walkthrough below uses the pre-Java-8 source as its baseline; wherever Java 8+ changed the implementation, an inline Note flags it.
Integrate Spring MVC with Log4j
Integrating Log4j into a Spring MVC application is quite straightforward: include the Log4j library in your project dependencies, then create a log4j.properties file to define Log4j’s appenders and put it on the project classpath. This tutorial walks through that with the Log4j 1.x logging framework. Note: This post targets Log4j 1.x, which reached end of life in August 2015 and has known vulnerabilities (e.g. CVE-2019-17571, CVE-2021-4104). Treat it as a reference for maintaining legacy projects only. For anything new, prefer SLF4J + Logback (the default in Spring Boot) or Log4j 2.x. Their configuration and API differ entirely from what’s shown here, and the official link below now redirects to Log4j 2.