Monday, September 17, 2012

Spring MVC

Spring MVC
Spring फ्रेमवर्क एम वी सी
हा एक हेल्लो वल्ड एक्षम्प्ल 

Sunday, September 16, 2012

Spring MVC BeanNameUrlHandlerMapping

=================
What is Spring MVC BeanNameUrlHandlerMapping
=================

This is the default mapping provided by spring. This is a bit similar to struts style. In this Spring MVC mapping , developer is allowed to specify URI and control for that url specifically. As shown below. WE have to specify mapping class first and then bean ids.



<beans ...>
 
   <bean 
 class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
 
   <bean name="/welcome.htm" 
        class="com.bunty.anil.controller.WelcomeController" />
 
   <bean name="/college.htm" 
        class="com.spring.bunty.controller.StudentController" />
 
   <bean name="/x*.htm" 
        class="a.b.c.controller.CommonController" />
 
</beans>

Sunday, September 2, 2012

Performance Tuning in Java

=====
What all perfromance tuning tools you are aware
======

1. JProbe is the good tool for all meamory leak related problems. Gives good graphical reports.

2. Jmeter is also good one.

3. Pluggins like PMD and FindBugs are there.

4. Java 5 itself provide its own console to do so that is jConsole


Threading in core Java

==========
Synchronized block vs Synchronized method?


When to use Synchronized block and when synchronized method?

If method has only few lines to by synchronized which way will you prefer for synchronization?
========

if you go for synchronized block it will lock a specific object. 
if you go for synchronized method it will lock all the objects. 

If there is a separate class and has one method. You want to call that method with restricted access then use synchronized block. Else use synchronized method.

Synchronized methods are used when we are sure all instance will work on the same set of data through the same function
Synchronized block is used when we use code which we cannot modify ourselves like third party jars etc


The object taken in the parantheses by the synchronized construct is called a monitor object.

public class Counter{
     
     long count = 0;
    
     public synchronized void add(long value){
       this.count += value;
     }
  }
  public class CounterThread extends Thread{
 
     protected Counter counter = null;
 
     public CounterThread(Counter counter){
        this.counter = counter;
     }
 
     public void run() {
            for(int i=0; i<10; i++){
           counter.add(i);
        }
     }
  }
  public class Example {
 
    public static void main(String[] args){
      Counter counter = new Counter();
      Thread  threadA = new CounterThread(counter);
      Thread  threadB = new CounterThread(counter);
 
      threadA.start();
      threadB.start(); 
    }
  }
Two threads thread are created. The same Counter instance is passed to both of them in their constructor. The Counter.add() method is synchronized on the instance, because the add method is an instance method, and marked as synchronized. Therefore only one of the threads can call the add() method at a time. The other thread will wait until the first thread leaves the add() method, before it can execute the method itself.
If the two threads had referenced two separate Counter instances, there would have been no problems calling the add() methods simultanously. The calls would have been to different objects, so the methods called would also be synchronized on different objects (the object owning the method). Therefore the calls would not block. Here is how that could look:
  public class Example {
 
    public static void main(String[] args){
      Counter counterA = new Counter();
      Counter counterB = new Counter();
      Thread  threadA = new CounterThread(counterA);
      Thread  threadB = new CounterThread(counterB);
 
      threadA.start();
      threadB.start(); 
    }
  }
Notice how the two threads, threadA and threadB, no longer reference the same counter instance.




Saturday, September 1, 2012

Annotations in Java

Annotation in java

========
What are the advantages of Annotation?
========

As we are aware that every class is subclass of object. AnnotationOverrideTest ca object class has two overridden methods. But Object class does not have toStirng123() method. Here compiler will give an error. Which will save developer time as well as errors caused by human ignorance.
public class AnnotationOverrideTest {
  
   @Override public String toString() {
      return "Override the toString() of the superclass";
   }
  
   // Compilation Error because superclass Object does not have this method
   @Override public String toString123() {
      return "Override the toString123() of the superclass";
   }
}

If you look at below code, developer is trying to do some action on window close event. Program will easily compile and run, but will not work as expected on closing event.  If you look carefully method name is not spelled correctly. This could have been avoided if @override annotation is been used. Compile will give compile time error if you do this kind of mistake. This can save lots of debugging time.
import java.awt.*;
import java.awt.event.*;
public class AnnotationOverrideDemo extends Frame {
   public AnnotationOverrideDemo() {
      this.addWindowListener(new WindowAdapter() {
         public void windowclosing(WindowEvent e) {
            System.exit(0);
         }
      });
      setSize(200, 100);
      setTitle("Annotation Override Demo");
      setVisible(true);
   }
   public static void main(String[] args) { new AnnotationOverrideDemo(); }
}