Category Archives: Java

Use CDN Service in Spring MVC

A few days ago, I created TestZilla on Aliyun Elastic Compute Service. However, with the increment of PV, I decided to use CDN to cache static files(images, CSS, and javascript). But there's no information on how to use CDN with Spring MVC, so I asked a question on StackOverflow. Setup Spring Configuration First of all, you need to use PropertyPlaceholderConfigurer in Spring Configuration(such as dispatcher-servlet.xml) Of course, you need to add XML Namespaces: Create Properties File As I was declared in the Spring configuration, there is a file named testzilla.properties in the classpath.You need to create the file and add the following lines to it:[plain]cdn.url=//cdn.testzilla.org/[/plain] Of course, you can add extra properties if needed: Use CDN Property in JSP Now, we…

Continue Reading

Sending Email with Spring MVC Using Template

This tutorial provides a sample spring MVC application that allows user sending an e-mail message. In this tutorial, you are supposed to familiar with Java EE development as well as developing Spring MVC-based applications. Spring Framework’s Support for E-mail Based on JavaMail, Spring framework provides high-level abstraction API which greatly simplifies e-mail sending process. Let’s take a brief look at this API in the following class diagram: To send e-mail messages, we can use an implementation of interface MailSender – the JavaMailSenderImpl class which is built upon on JavaMail. It’s convenient to configure this implementation as a bean in Spring’s context: Dependencies(pom.xml) The Mail Template File Name: verifyEmail.vm File Location: classpath: /mails/verifyEmail.vm (You can change this in configuration file) The…

Continue Reading

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.) All collections have an iterator that goes through all of the elements in the collection. Additionally, Collection is a generic.…

Continue Reading

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. Aspect-Oriented Programming Overview Most of the enterprise applications have some common crosscutting concerns that is applicable for different types of Objects and modules. Some of the common crosscutting concerns are logging, transaction management, data validation, etc. In Object-Oriented Programming, modularity of application is achieved by Classes whereas in Aspect-Oriented Programming application modularity is achieved by Aspects and they are configured to cut across different classes. AOP takes out the direct dependency of crosscutting tasks from classes that we can’t achieve through a normal object-oriented programming…

Continue Reading

The String Constant Pool

String Constant Pool is possible only because String is immutable in Java and its implementation of the String interning concept. The string pool is also an example of a Flyweight design pattern. String Pool Examples Instructions So as you can see, the reference of s1, s2, s3, and s6 are the same. Actually, the object of s4 and s5 are stored in the Heap of JVM, and String Constants are stored in the Method Area of JVM. Something about intern() When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool,…

Continue Reading

Difference between wait() and sleep(), yield() in Java

Differences between wait() and sleep() or sleep() and yield() method in Java Thread is one of the very old questions asked in Java interviews. Though both wait and sleep put threads to the waiting state, they are completely different in terms of behavior and use cases. JavaDoc Definition Thread Class public static void sleep(long millis[, int nanos]) throws InterruptedException Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors. Parameters:millis - the length of time to sleep in millisecondsnanos - 0-999999 additional nanoseconds to sleep Throws:IllegalArgumentException - if the value of millis…

Continue Reading
Contact Us
  • Nanyang Technological University, Singapore
  • root [at] haozhexie [dot] com