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>        

No comments:

Post a Comment

Java 8 Notes Pics