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>
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
 
 
 
 Posts
Posts
 
 
No comments:
Post a Comment