Drop Down

Saturday, September 21, 2019

Spring_App_15_Autowiring_By_Annotations

package com.amit.Beans;

public class BGV
{
private String fathersName;
private String mothersName;
private String state;
private String distict;
private String postoffice;
private String street;
private String house_No;
private String pincode;
private String phone;

public String getFathersName() {
return fathersName;
}
public void setFathersName(String fathersName) {
this.fathersName = fathersName;
}
public String getMothersName() {
return mothersName;
}
public void setMothersName(String mothersName) {
this.mothersName = mothersName;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getDistict() {
return distict;
}
public void setDistict(String distict) {
this.distict = distict;
}
public String getPostoffice() {
return postoffice;
}
public void setPostoffice(String postoffice) {
this.postoffice = postoffice;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getHouse_No() {
return house_No;
}
public void setHouse_No(String house_No) {
this.house_No = house_No;
}
public String getPincode() {
return pincode;
}
public void setPincode(String pincode) {
this.pincode = pincode;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}

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

import org.springframework.beans.factory.annotation.Autowired;

public class Employee 
{
private String emp_name;
private String emp_code;
private String joining_date;
 
//@Autowired(required = true) --> write here or above setter method of bgv (To inject bgv into Employee)
private BGV bgv;
public String getEmp_name() {
return emp_name;
}
public void setEmp_name(String emp_name) {
this.emp_name = emp_name;
}
public String getEmp_code() {
return emp_code;
}
public void setEmp_code(String emp_code) {
this.emp_code = emp_code;
}
public String getJoining_date() {
return joining_date;
}
public void setJoining_date(String joining_date) {
this.joining_date = joining_date;
}
public BGV getBgv() {
return bgv;
}
@Autowired(required = true)  //--> we can keep it above declaration Line no 11
public void setBgv(BGV bgv) {
this.bgv = bgv;
}

public void getEmpDetails()
{
System.out.println("     Employee Details");
System.out.println("---------------------------------");
System.out.println("Emp Name :"+emp_name);
System.out.println("Emp Code :"+emp_code);
System.out.println("Joining Date     :"+joining_date);
System.out.println();
System.out.println("     Employee BGV Details");
System.out.println("---------------------------------");
System.out.println("Fathers Name :"+bgv.getFathersName());
System.out.println("Mothers Name :"+bgv.getMothersName());
System.out.println("State :"+bgv.getState());
System.out.println("Distict :"+bgv.getDistict());
System.out.println("Post Office :"+bgv.getPostoffice());
System.out.println("Street :"+bgv.getStreet());
System.out.println("House No. :"+bgv.getHouse_No());
System.out.println("Pincode :"+bgv.getPincode());
System.out.println("Phone :"+bgv.getPhone());
}

}

======================================================================
package com.amit.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.getEmpDetails();
}

}

======================================================================
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

   
<context:annotation-config/>
<bean id ="emp" class="com.amit.Beans.Employee">
<property name="emp_name" value="A. K. Manjhi"></property>
<property name="emp_code" value="1444"></property>
<property name="joining_date" value="4th Aug"></property>
<!-- <property name="bgv" ref="bgv"></property> -->
</bean>

<bean id = "bgv" class="com.amit.Beans.BGV">
<property name="fathersName" value="G. Manjhi"></property>
<property name="mothersName" value="B. Devi"></property>
<property name="state" value="Jharkhand"></property>
<property name="distict" value="Ranchi"></property>
<property name="postoffice" value="Mahuda"></property>
<property name="street" value="Power House"></property>
<property name="house_No" value="121"></property>
<property name="pincode" value="828305"></property>
<property name="phone" value="7532051119"></property>
</bean>

</beans>        

Spring_App_14_Autowiring_By_Constructor

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

Spring_App_13_Autowiring_By_Type

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;   
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="account" class="beans.Account">      <!-- In byType autowiring ioc look for type LIMITATION: only supports for unique beans in config.  -->
<property name="acc_number" value="8789654410911"></property>
<property name="ifsc" value="ICIC00004839"></property>
</bean>

<bean id= "address" 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="byType">
<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="address"></property>
<property name="acc" ref ="account"></property>  
-->

</bean>

</beans>

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>        

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

Java 8 Notes Pics