El siguiente ejemplo utiliza metadatos de configuración basados en XML para DI basada en definidor. Una pequeña parte del archivo de configuración Spring XML especifica algunas definiciones de beans de la siguiente manera:
<bean id="exampleBean" class="examples.ExampleBean">
<!-- inyección mediante setter utilizando un elemento ref anidado -->
<nombre de propiedad="beanOne">
<ref bean="otroExampleBean"/>
</propiedad>
<!-- inyección mediante setter utilizando el atributo ref más preciso -->
<nombre de propiedad="beanTwo" ref="yetAnotherBean"/>
<nombre de propiedad="integerProperty" valor="1"/>
</frijol>
<bean id="otroExampleBean" class="ejemplos.OtroBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
El siguiente ejemplo muestra la clase ExampleBean
correspondiente:
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;
}
}
clase EjemploBean {
lateinit var beanOne: OtroBean
lateinit var beanTwo: YetAnotherBean
var i: Int = 0
}
En el ejemplo anterior, los definidores se declaran para asignarse a las propiedades especificadas en el archivo XML. El siguiente ejemplo utiliza DI basada en constructor:
<bean id="exampleBean" class="examples.ExampleBean">
<!-- inyección de constructor utilizando un elemento ref anidado -->
<constructor-arg>
<ref bean="otroExampleBean"/>
</constructor-arg>
<!-- inyección de constructor utilizando el atributo ref más preciso -->
<constructor-arg ref="yetAnotherBean"/>
<constructor-arg type="int" value="1"/>
</frijol>
<bean id="otroExampleBean" class="ejemplos.OtroBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
El siguiente ejemplo muestra la clase ExampleBean
correspondiente:
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)
Los argumentos del constructor especificados en la definición del bean se utilizan como argumentos para el
constructor ExampleBean
.
Ahora considere una variación de este ejemplo que, en lugar de usar un constructor Spring, llama a un método de
fábrica static
para devolver una instancia del objeto:
<bean id="exampleBean" class="examples.ExampleBean" factory-method="createInstance">
<constructor-arg ref="otroExampleBean"/>
<constructor-arg ref="yetAnotherBean"/>
<constructor-arg value="1"/>
</frijol>
<bean id="otroExampleBean" class="ejemplos.OtroBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
El siguiente ejemplo muestra la clase ExampleBean
correspondiente:
public class ExampleBean {
// private (private) constructor
private ExampleBean(...) {
...
}
// static factory method; the arguments of this method can be considered
// dependencies of the returned bean,
// no matter how these arguments are actually used.
public static ExampleBean createInstance (
AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
ExampleBean eb = new ExampleBean(...);
// some other operations...
return eb;
}
}
class ExampleBean private constructor() {
companion object {
// static factory method; the arguments of this method can be considered
// dependencies of the returned bean,
// no matter how these arguments are actually used.
@JvmStatic
fun createInstance(anotherBean: AnotherBean, yetAnotherBean: YetAnotherBean, i: Int): ExampleBean {
val eb = ExampleBean(...)
// some other operations...
return eb
}
}
}
Los argumentos para un método de fábrica static
son proporcionados por elementos <constructor-arg/>
,
como si realmente se estuviera utilizando un constructor. El tipo de clase devuelta por el método de fábrica no
tiene que ser el mismo tipo que la clase que contiene el método de fábrica static
(aunque lo es en este
ejemplo). Un método de fábrica de instancia (no estático) se puede usar de manera casi idéntica (excepto por usar el
atributo factory-bean
en lugar del atributo class
), por lo que estos matices no están
cubiertos aquí.
GO TO FULL VERSION