Drop Down

Saturday, September 21, 2019

Spring_App_10

package com.amit.beans;

public class Account
{
public String acc_number;
public String ifsc;

public String getAcc_number() {
return acc_number;
}
public void setAcc_number(String acc_number) {
this.acc_number = acc_number;
}
public String getIfsc() {
return ifsc;
}
public void setIfsc(String ifsc) {
this.ifsc = ifsc;
}



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

package com.amit.beans;

public class Address 
{
public String city;
public String state;
public String pincode;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getPincode() {
return pincode;
}
public void setPincode(String pincode) {
this.pincode = pincode;
}
}

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

public class Employee 
{
private String name;
private String emp_code;
private String salary;
private Address addr;
private Account acc;
public String getName() {
return name;
}



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



public String getEmp_code() {
return emp_code;
}



public void setEmp_code(String emp_code) {
this.emp_code = emp_code;
}



public String getSalary() {
return salary;
}



public void setSalary(String salary) {
this.salary = salary;
}



public Address getAddr() {
return addr;
}



public void setAddr(Address addr) {
this.addr = addr;
}



public Account getAcc() {
return acc;
}



public void setAcc(Account acc) {
this.acc = acc;
}



public void display_details()
{
System.out.println("--------BASIC DETAILS------------");
System.out.println("Name : "+name);
System.out.println("emp_code : "+emp_code);
System.out.println("salary : "+salary);
System.out.println("--------ACCOUNT DETAILS------------");
System.out.println("acc_number  : "+acc.getAcc_number());
System.out.println("ifsc        : "+acc.getIfsc());
System.out.println("----------ADDRESS DETAILS----------");
System.out.println("city        : "+addr.getCity());
System.out.println("state       : "+addr.getState());
System.out.println("pincode     : "+addr.getPincode());
}

}

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

<?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="account" class="com.amit.beans.Account">
<property name="acc_number" value="8789654410911"></property>
<property name="ifsc" value="ICIC00004839"></property>
</bean>

<bean id= "addr" class = "com.amit.beans.Address">
<property name="city" value ="Noid"></property>
<property name="state" value ="UP"></property>
<property name="pincode" value ="120130"></property>
</bean>

<bean id="emp" class ="com.amit.beans.Employee" autowire="no">
<property name="name" value ="Amit"></property>
<property name="emp_code" value ="NSTL-9372"></property>
<property name="salary" value ="3,00,000 p.m"></property>
<property name="addr" ref="addr"></property>
<property name="acc" ref ="account"></property>
</bean>

</beans>
======================================================================
package Test;

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

import com.amit.beans.Employee;

public class Test {

public static void main(String[] args) 
{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Employee emp = (Employee) context.getBean("emp");
emp.display_details();

}

}


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

}

Java 8 Notes Pics