Spring @Value – Import a list from properties file

In this tutorial, we will show you how to import a “List” from a properties file, via Spring EL @Value Tested with : Spring 4.0.6 JDK 1.7 Spring @Value and List In Spring @Value, you can use the split() method to inject the ‘List” in one line. config.properties server.name=hydra,zeus server.id=100,102,103Continue Reading


Spring @PropertySource example

In Spring, you can use @PropertySource annotation to externalize your configuration to a properties file. In this tutorial, we will show you how to use @PropertySource to read a properties file and display the values with @Value and Environment. P.S @PropertySource has been available since Spring 3.1 1. @PropertySource andContinue Reading


Spring @Value default value

In this tutorial, we will show you how to set a default value for @Value 1. @Value Examples To set a default value in Spring expression, use Elvis operator : #{expression?:default value} Few examples : @Value("#{systemProperties[‘mongodb.port’] ?: 27017}") private String mongodbPort;   @Value("#{config[‘mongodb.url’] ?: ‘’}") private String mongodbUrl;   @Value("#{aBean.ageContinue Reading


Multiple SSH private keys Examples

To allow multiple private keys connect to different servers, edit ~/.ssh/config : ~/.ssh/config Host IdentityFile ~/.ssh/linode_rsa   Host IdentityFile ~/.ssh/id_rsa If you SSH to, private key ~/.ssh/linode_rsa will be used. If you SSH to, private key ~/.ssh/id_rsa will be used. 1. Single Private Key for MultipleContinue Reading


MongoDB – Allow remote access

In this tutorial, we will show you how to enable remote access to a MongoDB server. Here is the tested environment : 1. MongoDB Server Private IP – Public IP – MongoDB 2.6.3, port 27017 IpTables Firewall 2. Application Server (Same LAN network) Private IP – PublicContinue Reading


Logback – different log file for each thread

In this tutorial, we will show you how to use Logback Mapped Diagnostic Context (MDC) and SiftingAppender to create a separate log file for each thread. P.S Tested with Logback 1.1.2, should work in earlier version. Note More info, refer to this Logback MDC documentation 1. logback.xml example A logback.xmlContinue Reading


Spring – ${} is not working in @Value

A simple Spring @PropertySource example to read a properties file. db.properties db.driver=oracle.jdbc.driver.OracleDriver AppConfig.java @Configuration @PropertySource("classpath:db.properties") public class AppConfig {   @Value("${db.driver}") private String driver; But the property placeholder ${} is unable to resolve in @Value, if print out the driver variable, it will display string ${db.driver} directly, instead of “oracle.jdbc.driver.OracleDriver”.Continue Reading


Java – Get number of available processors

A code snippet to show you how to get the number of available processors / cores / CPUs in your environment. int processors = Runtime.getRuntime().availableProcessors(); System.out.println(processors); Output 8 P.S Tested with Intel(R) Core(TM) i7-4770 CPU @3.40GHz


How to change Eclipse theme

In this tutorial, we will show you how to change the Eclipse Theme. Tools used : Eclipse 4.4 Luna, works on earlier version. Eclipse Color Theme Plugin Figure : This is how your final Eclipse IDE looks like 1. Install Eclipse Color Theme Plugin Install the theme plugin and restartContinue Reading


Spring MVC Abstract Controller example

For self-reference, this article shows you how to create a Abstract class for Spring Controller, or a template method design pattern. 1. Abstract Controller In Abstract class, the @Controller annotation is optional, your implemented class will apply it. AbstractResultController.java package com.mkyong.web.controller;   import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.PathVariable;Continue Reading


logback.xml Example

Here are a few logback.xml examples that are using in some of my projects, just for sharing. 1. Send logs to Console All logging will be redirected to console. logback.xml <?xml version="1.0" encoding="UTF-8"?> <configuration>   <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern> %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} – %msg%n </Pattern> </layout>Continue Reading


Jsoup – Check Redirect URL

In this article, we will show you how to use Jsoup to check if an URL is going to redirect. 1. URL Redirection Normally, a redirect URL will return an HTTP code of 301 or 307, and the target URL will be existed in the response header “location” field. ReviewContinue Reading


MongoDB – How to remove a field from document

This MongoDB article shows you how to remove a field from document and array. 1. Remove a field from Documents Sample of document, and you want to remove the field “affLink”. domain.json { "_id" : 1, "domain" : "mkyong.com", "affLink" : "abc" } To remove a field from all documents,Continue Reading


Javascript – How to call function inside jQuery code

Review a Javascript code snippet to call a function which declared inside jQuery code : <script> //javascript function submitSearchForm() {   updateErrorMessage("Please enter a website url");   }   //jquery jQuery(document).ready(function($) {   function updateErrorMessage(msg) { $(’#error’).html(msg).hide().fadeIn(500); }   } ); </script> But, browser console shows the updateErrorMessage function isContinue Reading


Spring Profiles example

Spring @Profile allow developers to register beans by condition. For example, register beans based on what operating system (Windows, *nix) your application is running, or load a database properties file based on the application running in development, test, staging or production environment. In this tutorial, we will show you aContinue Reading

Page 1 of 10112345...102030...Last »