Maven Dependency vs dependency management

dependencies

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>

Dependency management

<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8</version>
</dependency>
</dependencies>
</dependencyManagement>

Dependency Management allows to consolidate and centralize the management of dependency versions without adding dependencies which are inherited by all children.

This is especially useful when you have a set of projects (i.e. more than one) that inherits a common parent.

In the parent POM, the main difference between the <dependencies> and <dependencyManagement> is this:

  • Artifacts specified in the <dependencies> section will ALWAYS be included as a dependency of the child module(s).
  • Artifacts specified in the <dependencyManagement> section, will only be included in the child module if they were also specified in the <dependencies> section of the child module itself.

    Why is it good?

  • because you specify the version and/or scope in the parent, and you can leave them out when specifying the dependencies in the child POM. This can help you use unified versions for dependencies for child modules, without specifying the version in each child module
  • <dependencyManagement> allows to easily upgrade/downgrade dependencies based on need, in other scenario this needs to be exercised at every child pom level

    Ref: https://stackoverflow.com/q/2619598

Java SE7 Features

-Binary literals, (using prefix 0b or oB)
-Underscores in numeric literals
-string in switch statements
-type inference for Generic instance creation
Map<String, List<String>> myMap = new HashMap<String, List<String>>();
In Java SE 7, you can substitute the parameterized type of the constructor with an empty set of type parameters (<>):
Map<String, List<String>> myMap = new HashMap<>();
-Caching Multiple exception
-Rethrowing exception with improved type checking
-try with resources statements
try(FileInputStream input = new FileInputStream(“file.txt”)) close automatically input stream, using AutoClosable interface
using multiple resources
try( FileInputStream input = new FileInputStream(“file.txt”);
BufferedInputStream bufferedInput = new BufferedInputStream(input)
) {}

ref:

http://docs.oracle.com/javase/7/docs/technotes/guides/language/enhancements.html#javase7

http://tutorials.jenkov.com/java-exception-handling/try-with-resources.html

 

 

Spring inject value into a static field

using org.springframework.beans.factory.config.MethodInvokingFactoryBean to invoke a static setter.

e.g
public Class ClassName{
private static Object fieldName;
public static void setFieldName(final Object fieldName)
{
ClassName.fieldName = fieldName;
}
}

Spring Injection:

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod" value="package.ClassName.setFieldName" />
<property name="arguments">
<list>
<ref bean="refObject" />
</list>
</property>
</bean>

Ref:

JAVA Performance -Memory,Runtime

Escape Analysis

  • The JVM uses therefore internally escape analysis to check if an object is used only with a thread or method. If the JVM identify this it may decide to create the object on the stack, increasing performance of the Java program.

Garbage collector

  • java -verbose:gc myProgram
  • How to enable Garbage Collection (GC) logs To enable GC logs, the -Xloggc:logFileName option will have to be passed when java command is being executed. Additionally if the detailed log of the GC is required, then an additional -XX:+PrintGCDetails option will have to be passed.Example: java -Xloggc:D:/log/myLogFile.log -XX:+PrintGCDetails myProg