Drop Down

Saturday, September 21, 2019

Spring_App_12_Autowiring_by_Name

package 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 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 beans;

public class Employee 
{
private String name;
private String emp_code;
private String salary;
private Address addr;  // In byName autowiring id must be same as bean property eg. acc/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());
}

}

=====================================================================
package Test;

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

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

}

}
<?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="acc" class="beans.Account"> <!-- In byName autowiring id must be same as bean property eg. acc/addr -->
<property name="acc_number" value="8789654410911"></property>
<property name="ifsc" value="ICIC00004839"></property>
</bean>

<bean id= "addr" class = "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 ="beans.Employee" autowire="byName">  <!-- Autowired here -->
<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 ="acc"></property>  
-->

</bean>

</beans>

Spring_App_11

package com.amit.bean;

public class HelloBean
{
private String message;

public void setMessage(String s)
{
this.message = s;
}
public String getMessage()
{
return message;
}

}

\===========================================================
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 hello = (HelloBean)context.getBean("hb");
  hello.setMessage("Amit");
  String message = hello.getMessage();
  System.out.println("Welcome Dear "+message);
  

}

}

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

</bean>
        
</beans>        

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>        
====================================================================

Java 8 Notes Pics