Category «Java»

How to read properties file in Spring.

If you need to read properties file in your Spring application all you need is to configure a PropertyPlaceholderConfigurer bean in your application context. Following example shows how to read property values from a properties file named config.properties. This file needs to be in your classpath so Spring can find it. Let’s begin by creating …

How to configure multiple handlers in a Spring MVC web application

In Spring MVC, DispatcherServlet relies on handler mapping to determine which controller the request should be sent to. All handler mapping classes in Spring implement org.springframework.web.servlet.HandlerMapping interface. Spring distribution contains following four implementation of HandlerMapping interface. BeanNameUrlHandlerMapping SimpleUrlHandlerMapping ControllerClassNameHandlerMappign CommonsPathMapHandlerMapping BeanNameUrlHandlerMapping is the simplest of all and DispatcherServlet looks for this mapping by default. You …

Extract substring from end of a Java String

Today I’ll show you how to extract last few characters (a substring) from a string in Java. Here is the code to do this. public class SubStringEx{ /** Method to get the substring of length n from the end of a string. @param inputString – String from which substring to be extracted. @param subStringLength- int …

How to add external jar files to Maven project POM

Maven has made compiling Java projects almost an effortless job. You no longer need to worry about placing all the required class files in your classpath. All you have to do is to include dependencies in your POM file and Maven takes care of the rest. It automatically downloads the jar file that your project …

Import static elements for more readable Java code

One of the welcome additions to Java language in Java 5 release is the static import declaration. static import works in the same way as traditional import declaration but it imports only the static members of a class. Traditional import declaration looks like this import java.util.*; The above statement will import all the classes under …

Test your Java skills.

Even with many years of experience with programming in Java one cannot claim that he or she has mastered the language. What I found out with experience is that unless your job offers you diverse problems to solve, you are more likely to work on similar projects and hence may end up using the exact …

How to read properties file in Java.

Reading properties file in Java is much easier than you might have thought. Following example illustrates one simple way of reading properties from a properties file. Let’s say we need to read from myConfig.properties file. The properties file has following entries. Directory = C:/prodFiles/ NumberOfFiles = 25 Extension = java Here is the java code …