• Breaking News

    Sunday 6 October 2013

    First Spring Programme

    Following files are required:-

    1.Client.java
    2.Spring.xml
    3.Customer.java


    Customer.java

    package com.tutorialsolver;

    public class Customer {

         String userName;
         String password;
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
       
    }



    spring.xml


    <?xml version="1.0" encoding="UTF-8"?>


    <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-3.0.xsd">
              
           
                 <bean id="customer"  class="com.tutorialsolver.Customer">
                
                         <property name="userName" value="Arpit Sharma"></property>
                         <property name="password" value="arpit sharma"></property>
                
                 </bean>
             
             
     </beans> 



    Client.java

    package com.tutorialsolver;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class Client {

       
         public static void main(String[] args) {
           
           
             ApplicationContext applicationContext=new                    ClassPathXmlApplicationContext("spring.xml");
             Customer customer=(Customer)applicationContext.getBean("customer");
             System.out.println("UserName is "+customer.getUserName());
             System.out.println("Password is "+customer.getPassword());
        }
    }



    Explain:-


    Customer.java:-  this class contain two property username and password and getter and setter of that property.


    spring.xml:-   1.  In this xml file first we declere doctype.
                          2.  <beans/>  tag which contain bean.
                          3.  bean tag contain id by which we can refrence Customer bean
                               in our xml. which i explain in next tutorial.
                          4.  class attribute specify class which object create when
                               container    intialized.
                          5.  we inject the property of customer class using property tag and
                                property name same as Customer class property.                       
     Clien.java :-  1.  applicationContext allow to load metadata from various                                         resource .
                          2.  We get instance of customer by using getBean() method.








    No comments:

    Post a Comment