All posts by mkyong.
gradle-logo

Gradle Application Plugin – APP_HOME in applicationDefaultJvmArgs

In Gardle, the application plugin, you can pass the system properties via applicationDefaultJvmArgs : gradle.build apply plugin:’application’ mainClassName = "com.mkyong.analyzer.engine.hydra.entryPointForJar" applicationName = ‘analyzer’   distZip { archiveName ‘analyzer-‘ + version + ‘.zip’ }   applicationDefaultJvmArgs = ["-Dlogback.configurationFile=logback.xml"] The problem is how to get the APP_HOME for logback.xml? gradle.build applicationDefaultJvmArgs =Continue Reading

spring-logo

Spring MVC – Beans loaded twice

A Spring MVC web application, noticed all the Spring’s beans are loaded twice!? package com.mkyong.config.db;   @Configuration public class MongoDevConfig {   private final static Logger logger = LoggerFactory.getLogger(MongoDevConfig.class);   @Bean MongoDbFactory mongoDbFactory() throws Exception {   logger.debug("Init…… MongoDbFactory() in production mode!"); //… return new new SimpleMongoDbFactory(mongo, "db");;   }Continue Reading

spring-logo

Spring Data MongoDB – Select fields to return

In MongoDB console, you can use field:1 to select the fields to return from a query : > db.hosting.find({},{domain:1, count:1}); In Spring Data for MongoDB, you use query.fields().include : HostingDaoImpl.java package com.mkyong.core.hosting.dao;   import java.util.List; import org.springframework.data.domain.Sort; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; import org.springframework.stereotype.Repository; import com.hostingcompass.core.db.dao.MongoDaoImpl;   @Repository publicContinue Reading

gradle-logo

Gradle War Plugin – Change output WAR filename

In Gradle, the WAR plugin will generate the final WAR file with the following pattern: ${baseName}-${appendix}-${version}-${classifier}.${extension}   Example : hello.1.0.war To change the default WAR filename, update war.archiveName. For example : build.gradle project(':web’) {   apply plugin: ‘war’   war { archiveName ‘hello-gradle.war’ }   dependencies { compile project(':core’) providedCompileContinue Reading

default thumbnail

How to pass System Properties in web.xml

In Java standalone application, you can use -D option to pass in the system properties : $ java -Dgeoip.file="/home/mkyong/geoip/test.db" test.jar In Java web application, you can pass the system properties via context-param in web.xml : web.xml <web-app> <context-param> <param-name>geoip.file</param-name> <param-value>/home/mkyong/geoip/test.db</param-value> </context-param> </web-app>

spring-logo

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-logo

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-logo

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’] ?: ‘127.0.0.1’}") private String mongodbUrl;   @Value("#{aBean.ageContinue Reading

linux-logo

Multiple SSH private keys Examples

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

mongoDB-logo

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 – 192.168.161.100 Public IP – 45.56.65.100 MongoDB 2.6.3, port 27017 IpTables Firewall 2. Application Server (Same LAN network) Private IP – 192.168.161.200 PublicContinue Reading

logback-logo

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-logo

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_logo_100

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

eclipse-logo

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-logo

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

Page 1 of 10012345...102030...Last »