Sunday, November 13, 2016

ClassLoader

ClassLoader:
Java class loader is a class which used to load classes at runtime. ClassLoader in Java works on three principle:
  • 1.       Delegation principle: Responsibility of loading class is of parent classloader. A classloader will load class only if parent is not able to find or load class.
  • 2.       Visibility principle: Child class loader can see all the classes loaded by parent ClassLoader, but parent class loader can not see classes loaded by child.
  • 3.       Uniqueness principle: A class should be loaded exactly once. This is basically achieved by delegation and ensures that child ClassLoader doesn't reload the class already loaded by parent.


Types of Classloader:
There are three default class loader used in Java:
  • 1.       Bootstrap ClassLoader/Primordial ClassLoader is responsible for loading standard JDK class files from rt.jar and it is parent of all class loaders in Java. Bootstrap class loader don't have any parents, if you call String.class.getClassLoader() it will return null and any code based on that may throw NullPointerException in Java.
  • 2.       Extension ClassLoader delegates class loading request to its parent, Bootstrap and if unsuccessful, loads class form jre/lib/ext directory or any other directory pointed by java.ext.dirs system property. Extension ClassLoader in JVM is implemented by sun.misc.Launcher$ExtClassLoader.
  • 3.       Application class loader is responsible for loading application specific classes from CLASSPATH environment variable, -classpath or -cp command line option, Class-Path attribute of Manifest file inside JAR. Application class loader is a child of Extension ClassLoader and its implemented by sun.misc.Launcher$AppClassLoader class.


Except Bootstrap class loader, which is implemented in native language mostly in C, all Java class loaders are implemented using java.lang.ClassLoader.

Explicitly load a class:
Java provides API to explicitly load a class by
  • 1.       Class.forName(classname)
  • 2.       Class.forName(classname, initialized, classloader)


Uses:
  • 1.       J2EE uses multiple class loaders to load class from different location e.g. classes from WAR file will be loaded by Web-app ClassLoader while classes bundled in EJB-JAR is loaded by another class loader.
  • 2.       Some web server also supports hot deploy functionality which is implemented using ClassLoader.
  • 3.       You can also use ClassLoader to load classes from database or any other persistent store.


String.class.getClassLoader() returns null because this class is loaded by bootstrap classLoader which is not implemented in java , it's either implemented in c or c++ so there is no reference for it that's why it returns null.
Why write a Custom ClassLoader in Java
If you are expecting a class at the runtime or from FTP server or via third party web service at the time of loading the class then you have to extend the existing class loader.

How does Java ClassLoader Work
  • 1.       When JVM requests for a class, it invokes loadClass function of the ClassLoader by passing the fully classified name of the Class.
  • 2.       loadClass function calls for findLoadedClass() method to check that the class has been already loaded or not. It’s required to avoid loading the class multiple times.
  • 3.       If the Class is not already loaded then it will delegate the request to parent ClassLoader to load the class.
  • 4.       If the parent ClassLoader is not finding the Class then it will invoke findClass() method to look for the classes in the file system.


Custom ClassLoader in Java

By extending ClassLoader class and overriding loadClass(String name) 

Saturday, August 27, 2016

Hibernate

Hibernate is a high-performance Object/Relational persistence and query service which is licensed under the open source GNU Lesser General Public License (LGPL) and is free to download. Hibernate not only takes care of the mapping from Java classes to database tables (and from Java data types to SQL data types), but also provides data query and retrieval facilities.

It involves two files :-
  1. Hibernate configuration file (.cfg.xml)
  2. Hibernate mapping file (.hbm.xml) 
Both these files are places in src folder. The configuration file is the main and the mapping file is declared inside it inside <mapping> tag with attribute resource.


Hibernate configuration file :-




<hibernate-configuration>  
<session-factory>  
<property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property>  
<property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> 
 <!-- Assume test is the database name -->  
<property name="hibernate.connection.url"> jdbc:mysql://localhost/test </property>  
<property name="hibernate.connection.username"> root </property>  
<property name="hibernate.connection.password"> root123  </property>  
<!-- List of XML mapping files -->  
<mapping resource="Employee.hbm.xml"/> 
 </session-factory>  
</hibernate-configuration>

Properties :- 
hibernate.dialect  :-

Hibernate Mapping File :-

Monday, June 6, 2016

Eclipse Issues

Issue: 
"Java compiler level does not match the version of the installed Java project facet."

Resolution:
Go to project >> Properties >> Project Facets and change the facets to required java version

Issue:
Unbound classpath container: 'JRE System Library' in project 'XYZ'

Resolution:
Go to Project >> build path >> Configure Buildpath >> Libraries >> Add Library >> JRE System Library >> Select an existing library

Issue:
"Cannot change version of project facet Dynamic Web Module to 3.0"

Resolution:
Change the attributes of web-app tag in web.xml file of the project to point to module 3.0


Issue:
The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

Resolution:
Add the dependency for java servlet api to the classpath

Issue:
How to enable to write mult-line string in eclipse

Resolution:
Go to Windows >> Preferences >> Java >> Editors >> Tying and Make sure that check box "Escape text when pasting into a string literal" is checked

Issue:
Warning message in an XML file "No grammar constraints (DTD or XML Schema) referenced in the document."

Resolution:
Just add <!DOCTYPE xml> below the tags <?xml version="1.0" encoding="UTF-8"?>

Issue:
While compiling maven project getting exception "Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project {project name}: Compilation failure: Compilation failure"

Resolution:
Go to pom.xml and remove <scope>test</scope> all the dependencies.

Issue:

Resolution:



Thursday, June 2, 2016

MAVEN

MAVEN is an innovative software project management tool that provides new concept of a project object model (POM) file to manage project’s build, dependency and documentation. The most powerful feature is able to download the project dependency libraries automatically.

Steps to install MAVEN:-
  • Download the  binary file form apache's site
  • Unzip the zip file and add to the environment variable.
  • To confirm installation use mvn -version on command prompt.
A simple maven project consists of following folder structure
  •  project
    • src
      • main
        • java (the source code)
        • resources
      • test
        • java (the unit test code )
        • resources ()
    • pom.xml (the configuration file containing the information to build the project)
POM.xml stands for Project Object Model and it has the below structure

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>PROJECT_NAME_IN_PACKAGE_NAME_STYLE</groupId>
<artifactId>NAME_OF_JAR</artifactId>
<version>VERSION_OF_JAR</version>
</project>

MVN Install:-
  • It is used to install the package to local repository so that it can be referenced by several projects
MVN package:-
  •  It is used to build the project
mvn archetype:generate:-

To create a java project in java 
mvn archetype:generate
-DgroupId=com.anshul.ws
-DartifactId=MyWebService
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false

To support eclipse

mvn eclipse:eclipse -Dwtpversion=2.0
Life cycle of Maven:-
Like we have targets in case of Ant , we have phases in maven

Features of MAVEN:-
  • Build process becomes very easy
To create a web-app using maven
  1. Using command prompt go to the directory where project needs to be created
  2. Type below
    mvn archetype:generate -DgroupId=org.arsoft.projects.sood -DartifactId=arsood -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
  3. To be able to access the project on eclipse go to the directory and run command
    mvn eclipse:eclipse -Dwtpversion=2.0
To create a Java project:

mvn archetype:generate -DgroupId=org.arsoft.projects.sood -DartifactId=arsood -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

To create an EJB project:

mvn archetype:generate -DgroupId=com.arsoft.projects -DartifactId=Common -DarchetypeArtifactId=ejb-javaee6 -DinteractiveMode=false

Understanding POM
  1. It stands for Project Object Model and is the fundamental unit of work in maven.

SpringBoot Application Event Listeners

When a spring boot application starts few events occurs in below order ApplicationStartingEvent ApplicationEnvironmentPreparedEvent Applicat...