Spring Data MongoDB – Aggregation Grouping Example

In this tutorial, we will show you how to do the data grouping with Spring Data + MongoDB aggregation framework. 1. Test Data domain.json { "_id" : 1, "domainName" : "", "hosting" : "" } { "_id" : 2, "domainName" : "", "hosting" : ""} { "_id" : 3, "domainName"Continue Reading


Gradle Application Plugin – APP_HOME in applicationDefaultJvmArgs

In Gardle, the application plugin, you can pass the system properties via applicationDefaultJvmArgs : 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? applicationDefaultJvmArgs =Continue Reading


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 Data MongoDB – Select fields to return

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


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 @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.,zeus,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.driver=oracle.jdbc.driver.OracleDriver @Configuration @PropertySource("") 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

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