<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Java on Infinite Script</title><link>https://www.infinitescript.com/tags/java/</link><description>Recent content in Java on Infinite Script</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Sat, 24 Feb 2018 07:10:14 +0000</lastBuildDate><atom:link href="https://www.infinitescript.com/tags/java/index.xml" rel="self" type="application/rss+xml"/><item><title>Getting Started with JUnit 5: Test your Spring MVC Application with it</title><link>https://www.infinitescript.com/2018/02/getting-started-with-junit-5-test-your-spring-mvc-application-with-it/</link><pubDate>Sat, 24 Feb 2018 07:10:14 +0000</pubDate><guid>https://www.infinitescript.com/2018/02/getting-started-with-junit-5-test-your-spring-mvc-application-with-it/</guid><description>&lt;p&gt;In this post, we describe how to use JUnit 5 to test your Spring MVC application with Maven. Before diving into the &lt;code&gt;pom.xml&lt;/code&gt;, it helps to know that JUnit 5 is not a single library but three sub-projects: the &lt;strong&gt;Platform&lt;/strong&gt; (the foundation that discovers and launches tests), &lt;strong&gt;Jupiter&lt;/strong&gt; (the new programming and extension model you actually write tests against), and &lt;strong&gt;Vintage&lt;/strong&gt; (a backward-compatible engine that still runs your old JUnit 3/4 tests). This split is what makes the Maven coordinates below, and the &lt;code&gt;SpringExtension&lt;/code&gt; we plug in later, much less mysterious.&lt;/p&gt;</description></item><item><title>Single Sign-On with Apereo CAS</title><link>https://www.infinitescript.com/2017/02/configuring-apereo-cas/</link><pubDate>Tue, 14 Feb 2017 14:04:16 +0000</pubDate><guid>https://www.infinitescript.com/2017/02/configuring-apereo-cas/</guid><description>&lt;p&gt;This tutorial walks a newcomer through standing up an &lt;a href="https://www.apereo.org/projects/cas"&gt;Apereo CAS&lt;/a&gt; server for single sign-on and wiring a Java web application to it as a client. The complete CAS 7.1 server is available as a companion WAR overlay on &lt;a href="https://gitlab.com/hzxie/apereo-cas-webapp"&gt;GitLab&lt;/a&gt;.&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This post has been rewritten for Apereo CAS 7.x. It was originally written for CAS 4.x, which you set up by copying the server source and hand-editing a pile of XML files (&lt;code&gt;deployerConfigContext.xml&lt;/code&gt;, &lt;code&gt;ticketGrantingTicketCookieGenerator.xml&lt;/code&gt;, …). CAS has changed completely since: the &lt;code&gt;org.jasig.cas&lt;/code&gt; packages became &lt;code&gt;org.apereo.cas&lt;/code&gt;, and the server is now built as a &lt;strong&gt;Spring Boot WAR overlay&lt;/strong&gt; configured through a single &lt;code&gt;cas.properties&lt;/code&gt; file.&lt;/p&gt;</description></item><item><title>Introduction to Java Virtual Machine</title><link>https://www.infinitescript.com/2015/08/introduction-to-java-virtual-machine-jvm/</link><pubDate>Fri, 07 Aug 2015 07:27:00 +0000</pubDate><guid>https://www.infinitescript.com/2015/08/introduction-to-java-virtual-machine-jvm/</guid><description>&lt;blockquote&gt;&#10;&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This article has been &lt;strong&gt;updated for Java 8 and later&lt;/strong&gt;. The original 2015 version described the &lt;em&gt;Permanent Generation&lt;/em&gt; (PermGen), which Java 8 removed and replaced with &lt;em&gt;Metaspace&lt;/em&gt; (&lt;a href="https://openjdk.org/jeps/122"&gt;JEP 122&lt;/a&gt;); the garbage-collection section has also been rewritten to cover the modern HotSpot collectors (G1, ZGC, Shenandoah). The behaviors described here are those of HotSpot; other JVM implementations may differ.&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;h2 id="what-is-jvm"&gt;What is JVM?&lt;/h2&gt;&#10;&lt;p&gt;The Java Virtual Machine is the cornerstone of the Java platform. It is the component responsible for Java&amp;rsquo;s hardware and operating-system independence, the small size of its compiled code, and its ability to protect users from malicious programs.&lt;/p&gt;</description></item><item><title>Use Server-Sent Events in Spring MVC</title><link>https://www.infinitescript.com/2015/06/use-server-sent-events-in-spring-mvc/</link><pubDate>Tue, 30 Jun 2015 02:55:32 +0000</pubDate><guid>https://www.infinitescript.com/2015/06/use-server-sent-events-in-spring-mvc/</guid><description>&lt;p&gt;Server-Sent Events (SSE) let a server push a continuous stream of updates to the browser over a single long-lived HTTP connection. Spring has supported them since version 4.2 through the &lt;code&gt;SseEmitter&lt;/code&gt; class, and the API has been stable ever since. This post is a practical how-to: we start with a bare &lt;code&gt;SseEmitter&lt;/code&gt;, then build the setup I actually use in production, streaming events from a message queue to the browser.&lt;/p&gt;</description></item><item><title>Use WebSockets with Spring, SockJS and Stomp</title><link>https://www.infinitescript.com/2015/05/use-websockets-with-spring-sockjs-and-stomp/</link><pubDate>Wed, 20 May 2015 14:42:45 +0000</pubDate><guid>https://www.infinitescript.com/2015/05/use-websockets-with-spring-sockjs-and-stomp/</guid><description>&lt;p&gt;This guide walks you through building a &amp;ldquo;hello world&amp;rdquo; application that sends messages back and forth between a browser and the server. WebSocket is a very thin, lightweight layer on top of TCP, which makes it a natural place to run a &lt;em&gt;subprotocol&lt;/em&gt; that gives the raw bytes some structure. Here we use &lt;a href="https://en.wikipedia.org/wiki/Streaming_Text_Oriented_Messaging_Protocol"&gt;STOMP&lt;/a&gt; messaging with Spring to create an interactive web application.&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This post has been &lt;strong&gt;updated for Spring 6/7&lt;/strong&gt;. The WebSocket setup now uses &lt;code&gt;@EnableWebSocketMessageBroker&lt;/code&gt; Java configuration and the modern &lt;code&gt;@stomp/stompjs&lt;/code&gt; client. The original 2015 version targeted Spring 4.1 with XML &lt;code&gt;&amp;lt;websocket:message-broker&amp;gt;&lt;/code&gt; config and the legacy &lt;code&gt;Stomp.over()&lt;/code&gt; API; both are obsolete and have been replaced throughout.&lt;/p&gt;</description></item><item><title>Spring MVC MyBatis Integration Tutorial</title><link>https://www.infinitescript.com/2015/05/spring-mvc-mybatis-integration-tutorial/</link><pubDate>Sun, 03 May 2015 03:20:08 +0000</pubDate><guid>https://www.infinitescript.com/2015/05/spring-mvc-mybatis-integration-tutorial/</guid><description>&lt;p&gt;A while back I built a project on Spring MVC and Hibernate, then wanted to see how MyBatis compares. This article shows how to wire MyBatis into Spring MVC, and how to let Spring manage MyBatis transactions for you.&lt;/p&gt;&#10;&lt;p&gt;A database transaction is an all-or-nothing unit of work: either every statement in it takes effect, or none of them does. We lean on that property at the very end, where a single bad row makes an entire batch insert roll back.&lt;/p&gt;</description></item><item><title>Verwandlung Online Judge</title><link>https://www.infinitescript.com/project/verwandlung-online-judge/</link><pubDate>Thu, 30 Apr 2015 18:45:00 +0000</pubDate><guid>https://www.infinitescript.com/project/verwandlung-online-judge/</guid><description>&lt;h2 id="introduction"&gt;Introduction&lt;/h2&gt;&#10;&lt;p&gt;An &lt;strong&gt;Online Judge (OJ)&lt;/strong&gt; is a web-based system used in competitive programming. It presents algorithmic problems, accepts code submissions in various languages, automatically compiles and runs the code against hidden test cases, and immediately tells the user whether their solution is correct, all without any human involvement in the grading process. Platforms like LeetCode and Codeforces are well-known examples.&lt;/p&gt;&#10;&lt;p&gt;Verwandlung Online Judge is a self-hostable, open-source OJ built for running your own contests or practice environment. Its main distinguishing feature at the time of release was cross-platform support: most open-source OJs were Linux-only due to their reliance on Linux-specific sandboxing APIs, whereas Verwandlung runs natively on both Windows and Linux.&lt;/p&gt;</description></item><item><title>Use CDN Service in Spring MVC</title><link>https://www.infinitescript.com/2015/04/use-cdn-service-in-spring-mvc/</link><pubDate>Thu, 02 Apr 2015 15:53:52 +0000</pubDate><guid>https://www.infinitescript.com/2015/04/use-cdn-service-in-spring-mvc/</guid><description>&lt;p&gt;When I deployed &lt;a href="https://www.infinitescript.com/project/testzilla/"&gt;TestZilla&lt;/a&gt; on &lt;a href="https://www.aliyun.com"&gt;Aliyun&lt;/a&gt; Elastic Compute Service, growing traffic made it worth offloading static files (images, CSS, and JavaScript) to a CDN. At the time there was little guidance on wiring a CDN into Spring MVC, so I &lt;a href="https://stackoverflow.com/questions/28734196/how-to-use-cdn-in-spring-mvc"&gt;asked on Stack Overflow&lt;/a&gt; and ended up with the setup below. The idea is simple: keep the CDN host in a single property and inject it wherever your JSP views reference static assets.&lt;/p&gt;</description></item><item><title>Sending Email with Spring MVC Using Template</title><link>https://www.infinitescript.com/2015/01/sending-email-with-spring-mvc/</link><pubDate>Sat, 10 Jan 2015 04:25:34 +0000</pubDate><guid>https://www.infinitescript.com/2015/01/sending-email-with-spring-mvc/</guid><description>&lt;p&gt;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.&lt;/p&gt;&#10;&lt;p&gt;It assumes you are comfortable with Java EE development and with building Spring MVC applications.&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This post has been &lt;strong&gt;updated for Spring Framework 7 and Thymeleaf 3.1&lt;/strong&gt;. 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 &lt;code&gt;javax.mail&lt;/code&gt; to Jakarta Mail (&lt;code&gt;jakarta.mail&lt;/code&gt;).&lt;/p&gt;</description></item><item><title>Java Collections Framework</title><link>https://www.infinitescript.com/2014/10/java-collections-framework/</link><pubDate>Thu, 30 Oct 2014 13:40:38 +0000</pubDate><guid>https://www.infinitescript.com/2014/10/java-collections-framework/</guid><description>&lt;p&gt;Almost all collections in Java are derived from the &lt;strong&gt;&lt;a href="http://docs.oracle.com/javase/8/docs/api/java/util/Collection.html"&gt;java.util.Collection&lt;/a&gt;&lt;/strong&gt; 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 &lt;strong&gt;&lt;a href="http://docs.oracle.com/javase/8/docs/api/java/lang/Iterable.html"&gt;java.lang.Iterable&lt;/a&gt;&lt;/strong&gt;, 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 &lt;code&gt;Collection&amp;lt;String&amp;gt;&lt;/code&gt; can hold strings, and the elements can be used as strings without any casting required.&lt;/p&gt;</description></item><item><title>Spring AOP Example Using Annotation</title><link>https://www.infinitescript.com/2014/10/spring-aop-example-using-annotation/</link><pubDate>Fri, 24 Oct 2014 15:05:50 +0000</pubDate><guid>https://www.infinitescript.com/2014/10/spring-aop-example-using-annotation/</guid><description>&lt;p&gt;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.&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This post has been &lt;strong&gt;updated for Spring Framework 7.0&lt;/strong&gt;. The original 2014 version targeted Spring 4.1 with AspectJ 1.8 and the &lt;code&gt;aspectjrt&lt;/code&gt; + &lt;code&gt;aspectjtools&lt;/code&gt; dependencies; it now uses &lt;code&gt;aspectjweaver&lt;/code&gt; 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 &lt;code&gt;jakarta.servlet.*&lt;/code&gt; rather than &lt;code&gt;javax.servlet.*&lt;/code&gt;. The AOP concepts and &lt;code&gt;@AspectJ&lt;/code&gt; annotations themselves are unchanged.&lt;/p&gt;</description></item><item><title>The String Constant Pool</title><link>https://www.infinitescript.com/2014/10/the-string-constant-pool/</link><pubDate>Mon, 06 Oct 2014 08:47:31 +0000</pubDate><guid>https://www.infinitescript.com/2014/10/the-string-constant-pool/</guid><description>&lt;p&gt;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 &lt;code&gt;String&lt;/code&gt; object instead of allocating a new one each time. This is possible only because &lt;code&gt;String&lt;/code&gt; is immutable in Java, combined with the String interning mechanism. In other words, the pool is an application of the Flyweight design pattern.&lt;/p&gt;&#10;&lt;h2 id="string-pool-examples"&gt;String Pool Examples&lt;/h2&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-java" data-lang="java"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#34;Hello&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#34;Hello&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;s3&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#34;Hel&amp;#34;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#34;lo&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;s4&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;#34;Hel&amp;#34;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;lo&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;s5&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;Hello&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;s6&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;s5&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;intern&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// true&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;s3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// true&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;s4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// false&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s4&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;s5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// false&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;s6&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="c1"&gt;// true&lt;/span&gt;&lt;span class="w"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="explanation"&gt;Explanation&lt;/h2&gt;&#10;&lt;p&gt;The references of &lt;code&gt;s1&lt;/code&gt;, &lt;code&gt;s2&lt;/code&gt;, &lt;code&gt;s3&lt;/code&gt;, and &lt;code&gt;s6&lt;/code&gt; all point to the same pooled object, while &lt;code&gt;s4&lt;/code&gt; and &lt;code&gt;s5&lt;/code&gt; do not. Here is why:&lt;/p&gt;</description></item><item><title>Difference between wait() and sleep(), yield() in Java</title><link>https://www.infinitescript.com/2014/09/difference-between-wait-and-sleep-yield-in-java/</link><pubDate>Tue, 23 Sep 2014 14:13:41 +0000</pubDate><guid>https://www.infinitescript.com/2014/09/difference-between-wait-and-sleep-yield-in-java/</guid><description>&lt;p&gt;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.&lt;/p&gt;&#10;&lt;h2 id="tldr"&gt;TL;DR&lt;/h2&gt;&#10;&lt;table&gt;&#10;&#9;&lt;thead&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;&lt;code&gt;sleep()&lt;/code&gt;&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;&lt;code&gt;yield()&lt;/code&gt;&lt;/th&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;th&gt;&lt;code&gt;wait()&lt;/code&gt;&lt;/th&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/thead&gt;&#10;&#9;&lt;tbody&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Defined in&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;Thread&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;Thread&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;Object&lt;/code&gt;&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Static method?&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Yes&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Yes&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;No&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Releases the lock/monitor?&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;No&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;No&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Yes&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Must hold the monitor (be in &lt;code&gt;synchronized&lt;/code&gt;)?&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;No&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;No&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Yes&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Wakes up when&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;timeout expires or interrupted&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;the scheduler decides (maybe immediately)&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;&lt;code&gt;notify()&lt;/code&gt;/&lt;code&gt;notifyAll()&lt;/code&gt;, timeout, or interrupt&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&#9;&#9;&lt;tr&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;Typical use&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;pause for a fixed time&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;hint to give up the CPU&lt;/td&gt;&#10;&#9;&#9;&#9;&#9;&#9;&lt;td&gt;coordination between threads&lt;/td&gt;&#10;&#9;&#9;&#9;&lt;/tr&gt;&#10;&#9;&lt;/tbody&gt;&#10;&lt;/table&gt;&#10;&lt;h2 id="javadoc-definition"&gt;JavaDoc Definition&lt;/h2&gt;&#10;&lt;h3 id="thread-class"&gt;Thread Class&lt;/h3&gt;&#10;&lt;p&gt;&lt;strong&gt;public static void sleep(long millis[, int nanos]) throws InterruptedException&lt;/strong&gt;&lt;/p&gt;</description></item><item><title>Object Initialization Order in Java</title><link>https://www.infinitescript.com/2014/09/object-initialization-order-in-java/</link><pubDate>Mon, 22 Sep 2014 08:51:05 +0000</pubDate><guid>https://www.infinitescript.com/2014/09/object-initialization-order-in-java/</guid><description>&lt;p&gt;An object is a chunk of memory bundled with the code that manipulates memory. In the memory, the object maintains its &lt;em&gt;state&lt;/em&gt; (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.&lt;/p&gt;</description></item><item><title>HashMap Internal Implementation Analysis in Java</title><link>https://www.infinitescript.com/2014/09/hashmap-internal-implementation-analysis-in-java/</link><pubDate>Tue, 09 Sep 2014 03:38:29 +0000</pubDate><guid>https://www.infinitescript.com/2014/09/hashmap-internal-implementation-analysis-in-java/</guid><description>&lt;p&gt;&lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html"&gt;HashMap&lt;/a&gt; is one of the most common topics in Java interviews, and &amp;ldquo;How does HashMap work?&amp;rdquo; has a deceptively simple one-line answer: &amp;ldquo;On the principle of hashing.&amp;rdquo; To say anything more, you need to know how hashing works and how HashMap applies it internally.&lt;/p&gt;&#10;&lt;p&gt;This post assumes you already know the basics of using a HashMap; if not, start with the official &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html"&gt;Java docs&lt;/a&gt;. The walkthrough below uses the pre-Java-8 source as its baseline; wherever Java 8+ changed the implementation, an inline &lt;em&gt;Note&lt;/em&gt; flags it.&lt;/p&gt;</description></item><item><title>Integrate Spring MVC with Log4j</title><link>https://www.infinitescript.com/2014/06/integrate-spring-mvc-with-log4j/</link><pubDate>Tue, 10 Jun 2014 04:46:09 +0000</pubDate><guid>https://www.infinitescript.com/2014/06/integrate-spring-mvc-with-log4j/</guid><description>&lt;p&gt;Integrating Log4j into a Spring MVC application is quite straightforward: include the Log4j library in your project dependencies, then create a &lt;code&gt;log4j.properties&lt;/code&gt; file to define Log4j&amp;rsquo;s appenders and put it on the project classpath. This tutorial walks through that with the Log4j 1.x logging framework.&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This post targets &lt;strong&gt;Log4j 1.x&lt;/strong&gt;, 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 &lt;strong&gt;SLF4J + Logback&lt;/strong&gt; (the default in Spring Boot) or &lt;strong&gt;Log4j 2.x&lt;/strong&gt;. Their configuration and API differ entirely from what&amp;rsquo;s shown here, and the official link below now redirects to Log4j 2.&lt;/p&gt;</description></item><item><title>Unit Testing for Spring MVC Controllers</title><link>https://www.infinitescript.com/2014/05/unit-testing-of-spring-mvc-controllers/</link><pubDate>Sat, 17 May 2014 02:09:38 +0000</pubDate><guid>https://www.infinitescript.com/2014/05/unit-testing-of-spring-mvc-controllers/</guid><description>&lt;p&gt;Once the services, DAO, and supporting classes are tested, it&amp;rsquo;s time for the controller. Controllers are often the hardest layer to test, so many developers (based on observation) reach for Selenium or, worse, test by hand. That works, but it makes exercising individual logic branches awkward and slow, and no one wants to wait for a browser to spin up before checking in code. The Spring MVC Test framework solves this by driving full controller logic through fast unit tests, and it has lived in Spring&amp;rsquo;s core ever since Spring 4.&lt;/p&gt;</description></item><item><title>Spring 4 MVC Hello World Tutorial Using Maven</title><link>https://www.infinitescript.com/2014/04/spring-4-mvc-hello-world-tutorial-using-maven/</link><pubDate>Sun, 20 Apr 2014 02:10:48 +0000</pubDate><guid>https://www.infinitescript.com/2014/04/spring-4-mvc-hello-world-tutorial-using-maven/</guid><description>&lt;p&gt;In this tutorial we build the smallest useful Spring MVC application: a controller that reads a request parameter and renders it in a JSP view. It is the classic &amp;ldquo;Hello World&amp;rdquo;, but the wiring it shows (a &lt;code&gt;DispatcherServlet&lt;/code&gt;, a bean configuration, and a view resolver) is the same wiring every larger Spring MVC application is built on.&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This tutorial has been &lt;strong&gt;updated for Spring Framework 7&lt;/strong&gt;. It was originally written in 2014 for Spring 4 and the &lt;code&gt;javax.*&lt;/code&gt; servlet API; the configuration below now uses the &lt;code&gt;jakarta.*&lt;/code&gt; namespace, drops the Eclipse-specific setup for a plain Maven command, and runs on Jakarta EE 11 (Tomcat 11+). We keep XML configuration and &lt;code&gt;web.xml&lt;/code&gt; so every piece of the wiring stays visible. The same beans map one-to-one onto a &lt;code&gt;@Configuration&lt;/code&gt; class if you prefer Java config.&lt;/p&gt;</description></item><item><title>How To Convert Byte[] Array To String in Java</title><link>https://www.infinitescript.com/2013/11/how-to-convert-byte-array-to-string-in-java/</link><pubDate>Sun, 03 Nov 2013 08:00:51 +0000</pubDate><guid>https://www.infinitescript.com/2013/11/how-to-convert-byte-array-to-string-in-java/</guid><description>&lt;p&gt;It is common to convert a &lt;code&gt;String&lt;/code&gt; into a &lt;code&gt;byte[]&lt;/code&gt; array and back again. The catch is that &lt;em&gt;how&lt;/em&gt; you convert depends on what the bytes actually are: human-readable text in some encoding, or arbitrary binary data such as the output of a cipher. Using the wrong approach silently corrupts your data.&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This post has been &lt;strong&gt;updated to always specify a charset explicitly&lt;/strong&gt; and to cover the binary-data case. The original advice — &lt;code&gt;new String(bytes)&lt;/code&gt; — relies on the platform&amp;rsquo;s default charset, which makes the result differ from machine to machine, and it cannot round-trip binary data at all. Both pitfalls are addressed below.&lt;/p&gt;</description></item></channel></rss>