First Program in spring framework using eclipse IDE


Spring is powerful framework also called mother of all Framework



First Program in Spring

1.  Calulator.java

package com.login;

public class calculator implements operation{

    public calculator()
    {
        System.out.println("Inside the calculator");
    }
public int sum()
{
    return 4;
   
}
public  int substraction()
{
    return 6;
}
}




2. operation.java

package com.login;

public interface operation {
    public int sum();
    public int substraction();
   

}



3. TestCal.java

package com.login;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

public class TestCal {
    public static void main(String[] args)
    {
        try{

BeanFactory factory = new XmlBeanFactory(new ClassPathResource("aplication.xml"));   

calculator cl=(calculator)factory.getBean("bean1");
int a=0;
a=cl.sum();
System.out.println("The value of sum is "+a);
        }
        catch(Exception e)
        {
            System.out.println("The exception is"+e.getMessage());
        }
       
    }

}


Outpt:

After the run this program Output will
  Inside the calculator
  The value of sum is 4

Here we bean class Calcultor is not directly instantiated , it has done through XMLBeanFactory i.e. configure into
application.xml file, so spring framework says that programmer to concentrate only on bussiness loging not on creation of object
i.e. Spring IOC take responsibility of creation of object

application.xml

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.springframework.org/schema/beans    
              http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

  <bean id="bean1" class="com.login.calculator"/>
  <bean id="audi" class="com.aspec.Audience"/>
</beans> 

Import following highlighted jar file in lib directory



Connection pooling JNDI name setting in IBM WAS App Server stepwise

For Setting JNDI name for connection pooling in IBM WAS App Server






Object constructor concept in Java | can we write a method name same as constructor name in class definition

Concept 1:  can we write a method same name as constructor in class definition.

Exp:     Yes we can write a method same name as constructor in java class definition.

Code :

package com.threadTest;

public class constructorTest {

    public void constructorTest()
    {
        System.out.println("Inside the constructor");
        return;
       
    }
public static void main(String[] args)
{
    constructorTest ct=new constructorTest();
}

}

Above code compile without Error





Object wait() method concept in java multithread enviornment

Object method wait() in multithread enviornment
  •    the wait() method causes a thread to release the lock it is holding on an object; allowing another thread to run
  •     the wait() method is defined in the Object class
  •     wait() can only be invoked from within synchronized code
  •     it should always be wrapped in a try block as it throws IOExceptions
    there are actually three wait() methods
        wait()
        wait(long timeout)
        wait(long timeout, int nanos)
    the timeout is measured in milliseconds
    nanos is measured in nanoseconds
  •     wait() can only invoked by the thread that own's the lock on the object

  •     when wait() is called, the thread becomes disabled for scheduling and lies dormant until one of four things occur:
  •         another thread invokes the notify() method for this object and the scheduler arbitrarily chooses to run the thread
  •         another thread invokes the notifyAll() method for this object
        another thread interrupts this thread
        the specified wait() time elapses
  •     when one of the above occurs, the thread becomes re-available to the Thread scheduler and competes for a lock on the object once it regains the lock on the object, everything resumes as if no suspension had occurred
  •     if the thread was interrupted by another thread, an InterruptedException is thrown BUT not until after the thread regains it's lock on the object

Throws

    the wait() method throws three exceptions
        IllegalArgumentException - if the timeout value passed is invalid
        IllegalMonitorStateException - if the current thread does not own the object's lock
        InterruptedException - if another thread interrupts the current thread. The interrupted status of the current thread is cleared

Static keyword concept in Java |

Static keyword concept in Java
concept 1 : When we use Static keyword in class definition then JVM will allocate only one memory location for that and all object will take copy of that  i.e. for each object there is no own property of that define static in class definition.
Code :
 package com.stat;
public class staticTest {
public  int k=9;
public int getNum()
{
 return this.k;

}
public void setNum(int a)
{
 this.k=a;
}
public static void main(String [] args)
{

 staticTest st1= new staticTest();
 staticTest st2= new staticTest();
 System.out.println("the value of k ="+st1.k);
 st2.setNum(27);
 System.out.println("the value of k ="+st2.k);
 System.out.println("the value of k ="+st1.k);

}
}
Output :
    
      the value of k =9
      the value of k =27
      the value of k =9
Exp:  here from object st1 coming value 9 , after modify value to 27 . again it value comes 9 i.e. for each object there is own copy of int k.
Code :
    package com.stat;
public class staticTest {
public static int k=9;
public int getNum()
{
 return this.k;

}
public void setNum(int a)
{
 this.k=a;
}
public static void main(String [] args)
{

 staticTest st1= new staticTest();
 staticTest st2= new staticTest();
 System.out.println("the value of k ="+st1.k);
 st2.setNum(27);
 System.out.println("the value of k ="+st2.k);
 System.out.println("the value of k ="+st1.k);

}
}
output:

the value of k =9
the value of k =27
the value of k =27
Exp : when we declare int k to staic the JVM will allocate memory once for K and all object created of class will copy of that k , so when we modify the value
        will effect for every object.

Will default constructor is run When we deserialized of serialized object that implements serializable interface ?

Concept : Answer will NO, when we deserialized the object that implements serializable interface Default constructor will not run
Code :
 class SerializationBox  implements Serializable { 
  public int a=900;
  private int serializableProp = 100;
  private transient int b=800;
  private int c;
  private static String st;
  private  String st1;
  public SerializationBox()
  {
   System.out.println("The Default constructor running");
  }
  public SerializationBox(int a)
   {
 this.c=a;
                   System.out.println("The constructor running");
   }
  public int getSerializableProp() { 

    return serializableProp; 
  } 
  public int getStaticValue() { 

     return b; 
   } 

public class SerializationSample { 
public static void main(String args[]) throws IOException,  FileNotFoundException, ClassNotFoundException
  { 
    SerializationBox serialB = new SerializationBox(10); 
    serialize("C:\\Temp\\kumud.txt", serialB); 
    System.out.println("After Serialize");
    SerializationBox sb = (SerializationBox) deSerialize("C:\\Temp\\kumud.txt"); 
    System.out.println(sb.getSerializableProp()); 
    System.out.println("Static Value"+sb.getStaticValue()); 

  } 
  
  public static void serialize(String outFile, Object serializableObject)  throws IOException { 
    FileOutputStream fos = new FileOutputStream(outFile); 
    ObjectOutputStream oos = new ObjectOutputStream(fos); 
    oos.writeObject(serializableObject); 

  } 
  
  public static Object deSerialize(String serilizedObject)  throws FileNotFoundException, IOException, ClassNotFoundException { 
    FileInputStream fis = new FileInputStream(serilizedObject); 
    ObjectInputStream ois = new ObjectInputStream(fis); 
    return ois.readObject(); 

  } 
}
Output :
The constructor running
After Serialize
100
Static Value0
Concept :
In above output  when object desrialized default constructor will not run

Serialization concept in java | how serialization work in java

Serialization in JAVA

Serialization in JAVA
Definition  :  java serialization is to write an object into a stream, so that it can be transported through a network and that object can be                  rebuilt again
Code :
   package core;
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.Serializable; 
class SerializationBox implements Serializable { 
  private int serializableProp = 100;
 public int getSerializableProp() { 
    return serializableProp; 
  } 
 
public class SerializationSample { 
public static void main(String args[]) throws IOException,  FileNotFoundException, ClassNotFoundException
  { 
    SerializationBox serialB = new SerializationBox(10); 
// serialization

   serialize("C:\\Temp\\kumud.txt", serialB); 
    System.out.println("After Serialize");
//deserialization
    SerializationBox sb = (SerializationBox) deSerialize("C:\\Temp\\kumud.txt"); 
    System.out.println(sb.getSerializableProp()); 
    System.out.println("Static Value"+sb.getStaticValue()); 
  } 
   public static void serialize(String outFile, Object serializableObject)  throws IOException { 
    FileOutputStream fos = new FileOutputStream(outFile); 
    ObjectOutputStream oos = new ObjectOutputStream(fos); 
    oos.writeObject(serializableObject); 
  } 
  
  public static Object deSerialize(String serilizedObject)  throws FileNotFoundException, IOException, ClassNotFoundException { 
    FileInputStream fis = new FileInputStream(serilizedObject); 
    ObjectInputStream ois = new ObjectInputStream(fis); 
    return ois.readObject(); 
  } 
}

Output :
After Serialize
100
in above code when we serialize object and save into file C:\\Temp\\kumud.txt file and when desrialize agiain readObject convert into object and return function value 100
Concept :  when you want to prevent to save the state during searialization , use Transient and static Key
package core;
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.Serializable; 
class SerializationBox implements Serializable { 
  private int serializableProp = 100;
  private transient int b=800;
 
public int getSerializableProp() { 
    return serializableProp; 
  } 
  public int getNontransientValue() { 
     return b; 
   } 
public class SerializationSample { 
public static void main(String args[]) throws IOException,  FileNotFoundException, ClassNotFoundException
  { 
    SerializationBox serialB = new SerializationBox(10); 
    serialize("C:\\Temp\\kumud.txt", serialB); 
    System.out.println("After Serialize");
    SerializationBox sb = (SerializationBox) deSerialize("C:\\Temp\\kumud.txt"); 
    System.out.println(sb.getSerializableProp()); 
    System.out.println("Transient Value"+sb. getNontransientValue); 
  } 
  
  public static void serialize(String outFile, Object serializableObject)  throws IOException { 
    FileOutputStream fos = new FileOutputStream(outFile); 
    ObjectOutputStream oos = new ObjectOutputStream(fos); 
    oos.writeObject(serializableObject); 
  } 
  
  public static Object deSerialize(String serilizedObject)  throws FileNotFoundException, IOException, ClassNotFoundException { 
    FileInputStream fis = new FileInputStream(serilizedObject); 
    ObjectInputStream ois = new ObjectInputStream(fis); 
    return ois.readObject(); 
  } 
}
Output :
After Serialize
100
Transient Value 0
Concept :
In above transient value does not save , so when we deserialize the coming value is 0,
so there is question coming in mind that is constructor is run during deserializtion