Drop Down

Saturday, September 21, 2019

Spring_App_8.2_BeanLifeCycleUsingInstanceFactoryMethod

package com.amit.beans;

public class HelloBean
{
/*
* HelloBean() { System.out.println("HelloBean object created......"); }
*/
  public String sayHello()
  {
  return "Hello User";
  }
}

=====================================================================
package com.amit.factory;

import com.amit.beans.HelloBean;

public class HelloBeanFactory     // Factory class...configure it 
{
HelloBeanFactory()
{
System.out.println("HelloBeanFactory Object created.....");
}
public HelloBean getHelloBeanInstance() //Factory Method...configure it
{
System.out.println("Bean instantiation through Instance factory Method...");
return new HelloBean(); 
}
public String getMsg()
{
return "msg from HelloBeanFactory";
}

}

=====================================================================
package com.amit.test;

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

import com.amit.beans.HelloBean;
import com.amit.factory.HelloBeanFactory;

public class Test 
{

public static void main(String[] args) 
{
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  HelloBean hb = (HelloBean)context.getBean("helloBean");
  String ss = hb.sayHello();
  System.out.println(ss);
  
  HelloBeanFactory hbf = (HelloBeanFactory)context.getBean("helloBeanFactory");
  System.out.println(hbf.getMsg());

}

}


No comments:

Post a Comment

Java 8 Notes Pics