Saturday, March 29, 2014

Spring Containers


  • Spring Containers are Provided to take care of Spring Beans Life Cycle.
  • Spring containers are also perform Dependency Injection on Spring beans in 2 ways, in configuration file every bean/resource configured with '<bean>' tag.
  • If we use '<property>' tag to perform dependency injection then container performs 'setter-injection' ,if we use '<constructor-arg>'    tag to perform dependency injection  then container performs 'Constructor-Injection' mode dependency injection.
  • The Java classes that we are using as resources of spring application are called as Spring Resources.
  • To configure these Spring Beans we need Spring configuration file. 'anyname'.xml can be taken as spring configuration file.
  • Underlying container will use this configuration file to recognize resources.
Spring Provides 2 containers,They are
  1. BeanFactory                                                          
  2. ApplicationContext
BeanFactory:
  • To Activate 'BeanFactory'  we need an Object of class which implements "org.springframework.beans.factory.BeanFactory"  interface.
  • "org.springframework.beans.factory.xml.XmlBeanFactory" is most commonly used implementation class of 'BeanFactory' interface.
  • To create Object of 'XmlBeanFactory' we need 'FileSystemResource' Object

Activating 'BeanFactory' Container.....

//Creating a resource
FileSystemRecource res=new FileSystemResource("springcfgfile.xml");
                                   or
ClassPathResource res=new ClassPathReosurce("springcfgfile.xml");

//Activate 'BeanFactory' container.
XmlBeanFactory factory=new XmlBeanFactory(res);


FileSystemResource: locates the given spring configuration file from the specified path of File System.

ClassPathResource: locates the given spring configuration file from the directories & jars that are added in class-path.

ApplicationContext:
  • ApplicationContext container is Enhancement of BeanFactory Container. 
  • To Activate 'ApplicationContext'  we need an Object of class which implements "org.springframework.context.ApplicationContext"  interface.
  • There are 3 popular classes implementing the this Interface

  • FileSystemAmlApplicationContext: Activates Spring Container by reading the spring configuration file from the specified path of 'File System'.
FileSystemXmlApplicationContext context=new FileSystemXmlApplicationContext("springconfigurationfile.xml");
                                                   or
ApplicationContext context=new FileSystemXmlApplicationContext("springconfigurationfile.xml");
  • ClassPathXmlApplicationContext: Activates Spring Container by reading the spring configuration file from the class path folders & jars.
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("springconfigurationfile.xml");
                                                 or
ApplicationContext context=new ClassPathXmlApplicationContext("springconfigurationfile.xml");
    • WebXmlApplicationContext: Activates Spring Container by reading the spring configuration file from web application resources(in web environment).

    Monday, March 24, 2014

    Dependency Injection

    " If The Underlying Technology or server or run-time environment or Framework is assigning dependent values to resources Dynamically even though the resources are not satisfying any contract,then it is called Dependency Injection".

    This type of approach is also called Inversion of Control.

    Majorly there are 2 types of Dependency-Injections.
                                 
    • Setter Injection.                                                       
    • Constructor Injection.

    setter Injection:
    •  In setter Injection approach we use setter & getter Methods to assign dependent values to resources dynamically. 
    • To Configure these methods in spring configuration file we use '<property>' tag.
    • These setter methods will be called after Constructor  creation of that resource(POJO) class.
    Eg configuration:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"................>

     <bean id="fb" class="firstbean">

    <property name="msg" value="hai..!"/>
    (or)<!-- You Can Use Either one of these ways. -->
    <property name="msg">  
    <value>     hello    </value>
    </property>

    </bean>
    </beans>

    in the above example   'msg'  is the name of the property of resource.

    Example application on spring setter injection.
    In this application i have
    jar files:
    commons-logging.jar
    spring-beans-3.2.2.RELEASE.jar
    spring-context-3.2.2.RELEASE.jar
    spring-core-3.2.2.RELEASE.jar
    spring-expression-3.2.2.RELEASE.jar

    You can get all the spring jars from spring 3.2.2 software and the commons-logging from spring 2.5.

    UserImpl class:
    It Contains Business Logic.

    package com.javagreat.springSI;

    import java.util.Calendar;
    import com.javagreat.springSI.Interface.User;

    public class UserImpl implements User {
    String msg;

    /**
    * @return the msg
    */
    public String getMsg() {
    return msg;
    }

    /**
    * @param msg the msg to set
    */
    public void setMsg(String msg) {
    this.msg = msg;
    }

    @Override
    public void displayMsg(String user_name) {
    Calendar c=Calendar.getInstance();
    int h=c.get(Calendar.HOUR_OF_DAY);
    if(h<12){
    System.out.println(msg+" Good Morning "+user_name);
    }else if(h<16){
    System.out.println(msg+" Good Afternoon "+user_name);
    }else if(h<20){
    System.out.println(msg+" Good Evening "+user_name);
    }else {
    System.out.println(msg+" Good Night "+user_name);
    }
    }
    }


    Spring Configuration file:
    To Configure Beans.

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    .......................>

    <bean id="userbean" class="com.javagreat.springSI.UserImpl">
    <!-- <property name="msg">
    <value>Hello...!</value>
    </property>
    -->
    <!-- You Can Use Either one of these ways. -->
    <property name="msg" value="Hai...!"></property>
    </bean>
    </beans>

    Userdemo class:
    This is to run the Application.

    package com.javagreat.springSI;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    import com.javagreat.springSI.Interface.User;

    public class UserDemo {
    public static void main(String s[]){
    ApplicationContext context=new FileSystemXmlApplicationContext("/Context.xml");
    User ui= (UserImpl) context.getBean("userbean");
    ui.displayMsg("raju");
    }
    }

    With this Application we can inject dependent values to resource from Outside(xml file). In This demo application we injected a string value But,in real time we need to inject 'Datasource','connection'  etc objects.

    Constructor Injection:

    • In Constructor Injection we use parameterized  constructors to assign dependent values to resources.
    • To pass dependent values to these parameterized  constructors we use '<constructor-arg>' in spring configure   file. 
    • In Constructor Injection dependent values will be assigned to resources at the time of object creation.
    Example Configuration:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"................>

     <bean id="fb" class="firstbean">

    <constructor-arg  value=''hello"/>

    (or)<!-- You Can Use Either one of these ways. -->

    <constructor-arg >
    <value>     hai    </value>
    </constructor-arg>

    </bean>
    </beans>

    When Spring Container reads the above data it generates a 1-param constructor and assigns hello/hai to property of the 'firstbean' class.

    Constructor-Injection based example application is contains  same files as setter-injection based application.But small changes are need in spring configuration file and  UserImpl class.

    UserImpl class:
    It Contains Business Logic.

    package com.javagreat.springSI;

    import java.util.Calendar;
    import com.javagreat.springSI.Interface.User;

    public class UserImpl implements User {
    String msg;

    public UserImpl(String msg){
    this.msg=msg;
    }//This is the only change needed in this file.

    @Override
    public void displayMsg(String user_name) {
    Calendar c=Calendar.getInstance();
    int h=c.get(Calendar.HOUR_OF_DAY);
    if(h<12){
    System.out.println(msg+" Good Morning "+user_name);
    }else if(h<16){
    System.out.println(msg+" Good Afternoon "+user_name);
    }else if(h<20){
    System.out.println(msg+" Good Evening "+user_name);
    }else {
    System.out.println(msg+" Good Night "+user_name);
    }
    }
    }

    Spring Configuration file:
    To Configure Beans.

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    .......................>

    <bean id="userbean" class="com.javagreat.springSI.UserImpl">
    <!-- <constructor-arg>
    <value>Hello...!</value>
    </constructor-arg>
    -->
    <!-- You Can Use Either one of these ways. -->
    <constructor-arg value="Hai...!"/>
    </bean>
    </beans>

    Except these 2 changes Everything is same as above example. If you run the above application with these changes you can assign dependent value through constructor.

    Saturday, March 22, 2014

    Linux Commands

    There are multiple ways to copy file...                  

    To Copy File From your system to remote system

    $ scp -i  key_file  'path to copy file' username@host_name:'target_location_to_paste'.


    To Copy File from remote System to your system

    $ scp -i  key_file  username@host_name:'target_location_to_copy' 'path to paste file'.

    To Find id of a Process.

    ps aux | grep 'name_of_software'

    Eg:  ps aux | grep tomcat

    Note: remove ' 'quotations.

    How to Find out Size of file in linux/unix environment,
    There are multiple commands to do this...

    1. ls
    2. du
    3. stat
    Each one display result(size) in different way.

    let see about ls command
    If you want to to check size of a zip file 

    >$ ls -l filename.zip
    -rw-rw-r--. 1 root root 24882 Oct 26 18:44  filename.zip

    if you want to see some more user friendly response, then try with 
    $ ls -lh filename.zip 
    -rw-rw-r--. 1 root root 25K Oct 26 18:44 filename.zip

    Now try with du command
    >$ du -h filename.sql
    3.8M filename.sql

    and lastly see the output for stat command
    >$  stat filename.sql
      File: `filename.sql'
      Size: 3902512   Blocks: 7624       IO Block: 4096   regular file
    Device: ****/***** Inode: 23859810    Links: 1
    Access: (0664/-rw-rw-r--)  Uid: (  500/  root)   Gid: (  500/  root)
    Access: 2014-04-26 11:56:06.941036903 +0530
    Modify: 2014-04-26 11:55:07.927037477 +0530
    Change: 2014-04-26 11:56:04.802038739 +0530

    Friday, March 21, 2014

    POJI

    "A Java Interface Which does not extends from any Technology Specific Interfaces is Called POJI".

    Eg:                                                                                                          

    public interface I {                   public interface J extends I{
    //Method Declaration.                    //Method Declaration.                    
    }                                                     }
    Here interface I and J are POJIs.

    public interface k extends java.rmi.Remote{
    //Method Declaration.                    
    }                                          
    Here K is not POJI Because 'Remote' is a part of rmi Technology.

    Thursday, March 20, 2014

    POJO class

    A POJO class is  simple java class with Setter & Getter   Methods but, it is  neither Implemented nor Extended from any Technology Specific Interface or Class Respectively.

    Ex:                                                                                        
    public class  a{                    
        //Setters & Getters                              
    }                                              
    class a  is POJO class.


     public class  b implements java.io.Serializable{
             //Setters & Getters
      }
    class  is POJO class(Because  of 'Serializable' is part of J2SE [not a Technology]).

     public class  b implements java.sql.Connection{
             //Setters & Getters
      }
    class b is not POJO class(Because  of 'Connection' is part of JDBC Technology).


    public class  c extends Thread{
             //Setters & Getters
      }
    class  is POJO class(Because  of 'Thread' is part of J2SE [not a Technology]).

    public class  c extends HttpServlet{
             //Setters & Getters
      }
    class  is not POJO class(Because  of 'HttpServlet' is part of Servlet Technology]).

    What is Spring?


    Definition:
    "Spring is a Open-SourceLight-Weight , Loosely Coupled and Dependency Injection Based  Aspect-Oriented Java/J2EE Application Framework."
                                         

    Come To Each Word...

    Open-Source:  As You Know Open-Source software  Means available at Free of Cost to Users and also Allows public to Provide enhancements and Modifications to The Software under some Licences(free).

    Light-Weight: Some Components required High-End Web servers or Application Servers to Run and Resources of these components also should be implemented by Involving the component's  APIs, but
    • Most of Spring  applications can run outside of web/Application servers.
    • Spring supplies two light weight containers they can activate without server also. 
    • The Resources of Spring Applications can be implemented without using spring API (as POJO & POJI).
    B'coz of These reasons SPRING called as Light-Weight.

    Loosely Coupled: If Degree of Dependency is less B/w components then those components called as Loosely Coupled components. Spring is also Loosely Coupled because of

    • The Dependent Values required for Spring Application can be supplied from out-side of app Through Xml files & Properties Files.
    • We Can Use Spring Modules Individually or Together in Application Development.
    • We can Integrate Spring other Java, JEE Technologies and F/Ws in Project Development. 

    Dependency Injection: If The Underlying Technology or Framework or Servlet or Run-time Environment is Dynamically pushes/assigns dependent values to the resources  even though  they are not satisfying any contract, then is is called Dependency Injection. This approach is also called  Inversion of Control.

    Aspect-Oriented: Spring is Aspect-Oriented Because it provides Provision to Apply Aspects/Middle ware Services on Spring Application.
    Spring allows us to apply middle ware services through Spring Aop Module, Aspectj.