Quyidagi misolda setterni asoslangan DI uchun XML asosidagi konfiguratsion metama'lumotlar ishlatiladi. Spring XML konfiguratsion faylining kichik bir qismi ba'zi beanlarni quyidagicha aniqlaydi:
<bean id="exampleBean" class="examples.ExampleBean">
<!-- setter orqali qabul qilish nested element ref orqali -->
<property name="beanOne">
<ref bean="anotherExampleBean"/>
</property>
<!-- setter orqali qabul qilish aniqroq ref atributi yordamida -->
<property name="beanTwo" ref="yetAnotherBean"/>
<property name="integerProperty" value="1"/>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
Quyidagi misolda ExampleBean
sinfning mos keladigan klassi keltirilgan:
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public void setBeanOne(AnotherBean beanOne) {
this.beanOne = beanOne;
}
public void setBeanTwo(YetAnotherBean beanTwo) {
this.beanTwo = beanTwo;
}
public void setIntegerProperty(int i) {
this.i = i;
}
}
class ExampleBean {
lateinit var beanOne: AnotherBean
lateinit var beanTwo: YetAnotherBean
var i: Int = 0
}
Oldingi misolda, XML faylda ko'rsatilgan xususiyatlar bilan mos keladigan setterlar e'lon qilindi. Keyingi misolda konstruktor asosidagi DI ishlatiladi:
<bean id="exampleBean" class="examples.ExampleBean">
<!-- konstruktor orqali qabul qilish nested element ref bilan -->
<constructor-arg>
<ref bean="anotherExampleBean"/>
</constructor-arg>
<!-- konstruktor orqali qabul qilish aniqroq ref atributi yordamida -->
<constructor-arg ref="yetAnotherBean"/>
<constructor-arg type="int" value="1"/>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
Quyidagi misolda ExampleBean
sinfning mos keladigan klassi keltirilgan:
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public ExampleBean(
AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
this.beanOne = anotherBean;
this.beanTwo = yetAnotherBean;
this.i = i;
}
}
class ExampleBean(
private val beanOne: AnotherBean,
private val beanTwo: YetAnotherBean,
private val i: Int)
Konstruktor argumentlari, bean aniqlanishida keltirilgan, ExampleBean
konstruktori uchun argument sifatida ishlatiladi.
Endi misolning bitta variantini ko'rib chiqamiz, bunda Spring konstruktor o'rniga statik fabrika metod yordamida ob'ekt namunasini qaytarishni taklif etmoqda:
<bean id="exampleBean" class="examples.ExampleBean" factory-method="createInstance">
<constructor-arg ref="anotherExampleBean"/>
<constructor-arg ref="yetAnotherBean"/>
<constructor-arg value="1"/>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
Quyidagi misolda ExampleBean
sinfning mos keladigan klassi keltirilgan:
public class ExampleBean {
// yopiq (private) konstruktor
private ExampleBean(...) {
...
}
// statik fabrika metod; argumentlari metodi uchun qabul qilingan bean
// mustaqil ishlanadigan elementlar bo'lishi mumkin,
// real ishlanishidan qat'i nazar.
public static ExampleBean createInstance (
AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
ExampleBean eb = new ExampleBean (...);
// Boshqa ba'zi operatsiyalar...
return eb;
}
}
class ExampleBean private constructor() {
companion object {
// statik fabrika metod; argumentlari metodi uchun qabul qilingan bean
// mustaqil ishlanadigan elementlar bo'lishi mumkin,
// real ishlanishidan qat'i nazar.
@JvmStatic
fun createInstance(anotherBean: AnotherBean, yetAnotherBean: YetAnotherBean, i: Int): ExampleBean {
val eb = ExampleBean (...)
// Boshqa ba'zi operatsiyalar...
return eb
}
}
}
statik
fabrika metod uchun argumentlar <constructor-arg/>
elementlari orqali taqdim etiladi, xuddi konstruktor ishlatilgan kabi. Fabrika metodi qaytaradigan sinf turi statik
fabrika metodining o'zidagi sinf bilan bir xil bo'lishi shart emas (bu misolda shunday bo'lsa ham). Instansiyaviy (oddiy) fabrika metod ham deyarli bir xil tarzda ishlatilishi mumkin (agar factory-bean
atributi o'rniga class
atributi ishlatilsa), shuning uchun bu yerda bu tafsilotlar ko'rib chiqilmagan.
GO TO FULL VERSION