Pages

get our extension

Search program

Sunday 23 June 2013

Write Your First String Program

First create a POJO class
Right Click on
SRC ( inside application 1)à newà class
Write any package name like I’m writing com.alok and give class name like Book then click finish

package com.alok;

public class Book
{
      private String title;
      private String author;
      public Book()
      {
            System.out.println("Book()");
      }
      public String getTitle() {
            System.out.println("getTitle");
            return title;
      }
      public void setTitle(String title) {
            System.out.println("set title");
            this.title = title;
      }
      public String getAuthor() {
            System.out.println("getAuthor");
            return author;
      }
      public void setAuthor(String author) {
            System.out.println("set Author");
            this.author = author;
      }
     
}

Inside config.xml register this POJO class with a unique bean id

<?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.xsd">


<bean id="b1" class="com.alok.Book">
      <property name="title" value="java made easy"></property>
      <property name="author" value="prince"></property>

</bean>

</beans>




 à b1 is pointing to a object which is already created then we are calling setTitle and setAuthor 

à inside config.xml, for every bean tag, object is created

à All Setter method is calling before delimiter itself

Now Develop a Client Program

src-->com.alok-->new--> class

BookClient.java

package com.alok;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BookClient {
public static void main(String[] args) {
BeanFactory fac=new ClassPathXmlApplicationContext(new String[]{"config.xml"});
System.out.println("----111-----");
Book b1=(Book)fac.getBean("b1");
System.out.println("----222-----");
System.out.println(b1.getTitle());
System.out.println(b1.getAuthor());
}

}
à  While creating beanFactory object inside config.xml, corresponding setter and getter method is calling

try to understand output
output:-
Book()
set title
set Author
Book()
set title
set Author
----111-----
----222-----
getTitle
java made easy
getAuthor
prince

No comments:

Post a Comment