Drop Down

Saturday, September 21, 2019

Spring_App_9_Bean_Initialization_Distruction_using_custom_initialization_distruction

package com.amit.test;

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

import amit.com.beans.HelloBean;

public class Test {

public static void main(String[] args)
{
     ApplicationContext context = new ClassPathXmlApplicationContext("applicationConfig.xml");
     HelloBean bean = (HelloBean) context.getBean("hb");
     System.out.println(bean.sayHello());

}

}
=====================================================================

package amit.com.beans;

public class HelloBean 
{
HelloBean()
{
System.out.println("Object Creation.......");
}
public void initialize()
{
System.out.println("Initialization......");
}
public void destroy()
{
System.out.println("Destruction......");
}
public String sayHello()
{
return "Hello User";
}
}

=====================================================================
<?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="hb" class ="amit.com.beans.HelloBean" init-method="initialize" destroy-method="destroy"></bean>
</beans>        

====================================================================

Spring_App_8.3_BeanLifeCycleUsingStaticFactoryMethod

package com.amit.bean;

public class HelloBean
{

private HelloBean()    // here private method is getting accessed due to Reflection
{
System.out.println("Constructor: Bean Object Creating...");
}

public static HelloBean getInstance()  //---getInstance()  (use it in config file)
{
System.out.println("Bean instantiation through static factory method");
return new HelloBean();
}
public String sayHello()
{
return "Hello User";
}

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

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

import com.amit.bean.HelloBean;

public class Test {

public static void main(String[] args) 
{
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  HelloBean b = (HelloBean)context.getBean("helloamit");
  System.out.println(b.sayHello());

}

}
====================================================================
<?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="helloamit" class="com.amit.bean.HelloBean"/>   --> <!-- Use this for normal instantiation -->
<bean id="helloamit" class="com.amit.bean.HelloBean" factory-method="getInstance"/>   <!-- Use this for static factory method -->
</beans>        

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());

}

}


Spring_App_8.1_BeanLifeCycleUsingConstructor

package com.amit.bean;

//private class HelloBean  --> we can keep any scope (public , private)
public class HelloBean
{
public HelloBean()               //--> Parameterized constructor not allowed
{
System.out.println("...Bean Instantiation...");
}
public String sayHello()
{
return "Welcome";
}
}
====================================================================
package com.amit.test;

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

import com.amit.bean.HelloBean;

public class Test {

public static void main(String[] args) 
{
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  HelloBean bean = (HelloBean)context.getBean("helloBean");
  String msg = bean.sayHello();
  System.out.println(msg);

}

}

====================================================================
<?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="helloBean" class="com.amit.bean.HelloBean">

</bean>
        
</beans>        
====================================================================

Spring_App_8_JavaConfig

package com.amit.beans;

public class HelloBean
{
public HelloBean()
{
System.out.println("HelloBean Loanding....");
}
public String sayHello()
{
return "Message from HelloBean";
}

}

===================================================================
package com.amit.beans;

public class WelcomeBean  // Class of Bean needs to be mentioned in BeanConfiguration
{
public WelcomeBean()
{
System.out.println("WelcomeBean Loading.....");
}
public String getMsg() 
{
return "Message 1 from WelcomeBEAN";
}

}

====================================================================
package com.amit.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.amit.beans.HelloBean;
import com.amit.beans.WelcomeBean;

@Configuration   // optional
public class BeanConfiguration 
{
@Bean        //returned object in bean therefore declare @Bean
public WelcomeBean welcome_Bean() // id = welcome_Bean
{
return new WelcomeBean();
}
@Bean
public HelloBean helloBean()
{
return new HelloBean();
}

}

=====================================================================
package com.amit.Test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.amit.beans.HelloBean;
import com.amit.beans.WelcomeBean;
import com.amit.config.BeanConfiguration;

public class Test {

public static void main(String[] args) 
{
  ApplicationContext context = new AnnotationConfigApplicationContext(BeanConfiguration.class);
  WelcomeBean bean = (WelcomeBean)context.getBean("welcome_Bean");  //welcomeBean act like id (in classpathxmlApplicationcontext)
  String s = bean.getMsg();
  System.out.println("MSG: "+s);
  
  HelloBean bean2 = (HelloBean)context.getBean("helloBean");
  String s2 = bean2.sayHello();
  System.out.println("MSG: "+s2);
  
  
  
  
}

}

======================================================================

Spring_App_7_CustomScope

package com.amit.beans;

public class HelloBean
{
public HelloBean()
{
System.out.println("Hello bean created....");
}

}

=========================================================================
package com.amit.customScope;

import java.util.HashMap;

public class MyThreadLocal extends ThreadLocal 
{
@Override
protected Object initialValue()  //executed when we call getMethod() but no set value
{
  return new HashMap<String,Object>();
}

}

=======================================================================
package com.amit.customScope;

import java.util.HashMap;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.CustomScopeConfigurer;
import org.springframework.beans.factory.config.Scope;

public class ThreadScope implements Scope 
{
MyThreadLocal threadlocal = new MyThreadLocal();
HashMap<String,Object> scope = null; // to get scope object

@Override
public Object get(String name, ObjectFactory<?> objFactory)  //return bean object (available in the thread scope), if not available create a new bean object keep it in scope and return it.
{ //ObjectFactory class is in Spring used to create Bean Object
scope = (HashMap<String,Object>) threadlocal.get();  //calls initialValue() if no object is created
Object obj = scope.get(name);     // to check if it return object 
         if(obj == null)
         {
        obj = objFactory.getObject();
        scope.put(name, obj);                                  //keep obj in scope hashmap
         } 
return obj;
}

@Override
public String getConversationId() // to get id for bean e.g session id
{
return null;
}

@Override
public void registerDestructionCallback(String arg0, Runnable arg1) //when bean destroyed
{

}

@Override
public Object remove(String name) //to remove bean obj from scope
{
  Object obj = scope.remove(name);
return obj;
}

@Override
public Object resolveContextualObject(String arg0) // Resolves --> if 1 key is associated with multiple context object 
{
return null;
}

}

========================================================================
<?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="helloBean" class ="com.amit.beans.HelloBean" scope="thread"/>
<bean id="customThreadScope" class ="com.amit.customScope.ThreadScope" />
<bean id ="customScopeConfigurer" class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
    <map>
         <entry key="thread" value-ref = "customThreadScope">
         </entry>
    </map>
</property>
</bean>

</beans>        
========================================================================
package com.amit.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.amit.beans.HelloBean;

public class Test {

public static void main(String[] args) 
{
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  HelloBean bean1 = (HelloBean)context.getBean("helloBean");
  System.out.println("bean1 "+bean1);
  
  HelloBean bean2 = (HelloBean)context.getBean("helloBean");
  System.out.println("bean2 "+bean2);
}

}

Spring_App_6

package com.amit.Test;

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

import com.amit.Beans.HelloBean;

public class Test {

public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext("beanconfig.xml");

HelloBean bean = (HelloBean)context.getBean("tesingscope");
System.out.println("1>>"+bean);
System.out.println("1>>"+bean);

HelloBean bean2 = (HelloBean)context.getBean("testingscope2");
System.out.println("2>>"+bean2);
System.out.println("2>>"+bean2);

HelloBean bean3 = (HelloBean)context.getBean("testingscope2");
System.out.println("3>>"+bean3);
System.out.println("3>>"+bean3);


bean.setName("Amit");
String name = bean.getName();
System.out.println(name);

}

}

//--- for prototype
/*
 * 1>>com.amit.Beans.HelloBean@3cbbc1e0
 * 1>>com.amit.Beans.HelloBean@3cbbc1e0
 * 2>>com.amit.Beans.HelloBean@35fb3008
 * 2>>com.amit.Beans.HelloBean@35fb3008
 * 3>>com.amit.Beans.HelloBean@7225790e
 * 3>>com.amit.Beans.HelloBean@7225790e
 * Amit
 */
//--- for singleton
/*
1>>com.amit.Beans.HelloBean@3cbbc1e0
1>>com.amit.Beans.HelloBean@3cbbc1e0
2>>com.amit.Beans.HelloBean@35fb3008
2>>com.amit.Beans.HelloBean@35fb3008
3>>com.amit.Beans.HelloBean@35fb3008
3>>com.amit.Beans.HelloBean@35fb3008
Amit
* Amit
*/


=======================================================================
package com.amit.Beans;

public class HelloBean
{
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


}
=======================================================================
<?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="tesingscope" class ="com.amit.Beans.HelloBean" scope="singleton">

</bean>
<!--<bean id ="testingscope2" class="com.amit.Beans.HelloBean" scope="prototype"> -->
<bean id ="testingscope2" class="com.amit.Beans.HelloBean" scope="singleton">
</bean>
        
</beans>        

Java 8 Notes Pics