کانال بله, جهت پشتیبانی و اطلاع رسانی کانال بله, جهت پشتیبانی و اطلاع رسانی
عضویت

آموزش CI Inheriting Bean-Java Spring

ارث بری CI از Bean

به ارث بردن bean در اسپرینگ

با استفاده از خصیصه والد(parent) مربوط به bean ، می توانیم رابطه وراثتی بین bean ها را مشخص کنیم. در چنین حالتی، مقادیر bean والد به bean فعلی به ارث می رسد. مثالی ساده از وراثت bean را با هم بررسی می کنیم.


Employee.java :

این کلاس شامل سه خصیصه، سه سازنده و متد show() برای نمایش مقادیر است.

package com.javatpoint;   
public class Employee {  
private int id;  
private String name;  
private Address address;  
public Employee() {}  
public Employee(int id, String name) {  
    super();  
    this.id = id;  
    this.name = name;  
}  
public Employee(int id, String name, Address address) {  
    super();  
    this.id = id;  
    this.name = name;  
    this.address = address;  
}  
void show(){  
    System.out.println(id+" "+name);  
    System.out.println(address);  
}   
}  

Answer.java

package com.javatpoint;    
public class Address {  
private String addressLine1,city,state,country;  
  
public Address(String addressLine1, String city, String state, String country) {  
    super();  
    this.addressLine1 = addressLine1;  
    this.city = city;  
    this.state = state;  
    this.country = country;  
}  
public String toString(){  
    return addressLine1+" "+city+" "+state+" "+country;  
}  
}   

applicationContext.xml

< ?xml version="1.0" encoding="UTF-8" ?>

< beans 3. xmlns="http://www.springframework.org/schema/beans"
         4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         5. xmlns:p="http://www.springframework.org/schema/p"
         6. xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">    
< bean id="e1" class="com.javatpoint.Employee">  
< constructor-arg value="101">< /constructor-arg>  
< constructor-arg value="Sachin">< /constructor-arg>  
< /bean>  
< bean id="address1" class="com.javatpoint.Address">  
< constructor-arg value="21,Lohianagar">< /constructor-arg>  
< constructor-arg value="Ghaziabad">< /constructor-arg>  
< constructor-arg value="UP">< /constructor-arg>  
< constructor-arg value="USA">< /constructor-arg>  
< /bean>   
< bean id="e2" class="com.javatpoint.Employee" parent="e1">  
< constructor-arg ref="address1">< /constructor-arg>  
< /bean>  
< /beans>    

Test.java :

این کلاس bean را از فایل applicationContext.xml می گیرد و متد show را فراخوانی می کند.

package com.javatpoint;   
import org.springframework.beans.factory.BeanFactory;  
import org.springframework.beans.factory.xml.XmlBeanFactory;  
import org.springframework.core.io.ClassPathResource;  
import org.springframework.core.io.Resource;    
public class Test {  
public static void main(String[] args) {  
    Resource r=new ClassPathResource("applicationContext.xml");  
    BeanFactory factory=new XmlBeanFactory(r);        
    Employee e1=(Employee)factory.getBean("e2");  
    e1.show();        
}  
}  
1398/12/14 2123 530
رمز عبور : tahlildadeh.com یا www.tahlildadeh.com
نظرات شما

نظرات خود را ثبت کنید...