Spring – 08 – Autowiring with XML

Merhaba Arkadaslar
Bu bolumde Spring Autowiring kavramini incelemeye baslayacagiz.  Spring , otomatik olarak bean bagimlilillarini cozebilir. Varsayilan olarak autowiring teknigi disable/kapali durumdadir.

Spring , autowiring icin su modlari destekler ;

  • byName
  • byType
  • constructor

Autowiring with byName

autowiring=”byName” olarak tanimlandigi durumda <bean> id ya da name attribute ile ayni isimde bir property degerimiz varsa bu durumda autowiring islemi gerceklesecektir.

Employee.java

package _09.autowiring.byName.model;

public class Employee {

	private String name;
	private String surname;
	private Address addressByName;
        //getters and setters
...
}

Burada dikkat etmemiz gereken nokta Address tipinde bir property/instance variable tanimladik. autowiring=”byName” kullandigimiz durumda

Address.java

package _09.autowiring.byName.model;

public class Address {

	private String city;
        //getters and setters
...
}

09.autowiring.byName.xml
Burada dikkat edecek olursak autowire=”byName” attribute degerini kullandik. Ikinci olarak <bean> id olarak “addressByName” kullandik , bu deger Employee addressByName property/instance variable ismi ile ayni olmalidir.

Autowiring islemi yapildiginda bean id “addressByName” olan bean Employee objesine inject edilecektir.

<bean id="employeeId" class="_09.autowiring.byName.model.Employee"
autowire="byName">
    <property name="name" value="Levent" />
    <property name="surname" value="Erguder" />
</bean>


<bean id="addressByName" class="_09.autowiring.byName.model.Address">
    <property name="city" value="Istanbul" />
</bean>

<bean id="addressId" class="_09.autowiring.byName.model.Address">
    <property name="city" value="Istanbul - Not used autowire byName" />
</bean>

AutowiringByNameTest.java

package _09.autowiring.byName.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import _09.autowiring.byName.model.Employee;

public class AutowiringByNameTest {

	public static void main(String[] args) {

		ApplicationContext ctx = new ClassPathXmlApplicationContext("09.autowiring.byName.xml");

		Employee employee = ctx.getBean("employeeId", Employee.class);
		System.out.println(employee);

		((ClassPathXmlApplicationContext) ctx).close();
	}
}

Ornegimizi calistirdigimizda ;

Employee [name=Levent, surname=Erguder, addressByName=Address [city=Istanbul]]

Autowiring with byType

autowiring=”byType” tanimlandigi durumda Spring otomatik olarak property/instance variable tipindeki bean’leri arayacaktir ve baglayacaktir (wire).
Eger birden fazla target bean tipinde bean tanimi varsa bu durumda org.springframework.beans.factory.UnsatisfiedDependencyException hatasi firlatilacaktir.
Bu durumda autowire-candidate=”false” attribute kullanilabilir.

Employee.java

package _10.autowiring.byType.model;
public class Employee {

	private String name;
	private String surname;
	private Address address;
        //getters and setters
...
}

Address.java

package _10.autowiring.byType.model;

public class Address {

	private String city;
        //getters and setters
...
}

10.autowiring.byType.xml
autowire=”byType” olarak tanimliyoruz. Birden fazla Address tipinde bean tanimladik bu durumda eger birtanesi icin autowire-candidate=”false” tanimlamazsak hata aliriz.

<bean id="employeeId" class="_10.autowiring.byType.model.Employee" 
autowire="byType">
    <property name="name" value="Levent" />
    <property name="surname" value="Erguder" />
</bean>


<bean id="addressId1" class="_10.autowiring.byType.model.Address">
    <property name="city" value="Istanbul" />
</bean>

<bean id="addressId2" class="_10.autowiring.byType.model.Address" autowire-candidate="false">
    <property name="city" value="autowire-candidate false!" />
</bean>

AutowiringByTypeTest.java

package _10.autowiring.byType.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import _10.autowiring.byType.model.Employee;

public class AutowiringByTypeTest {

	public static void main(String[] args) {

		ApplicationContext ctx = new ClassPathXmlApplicationContext("10.autowiring.byType.xml");

		Employee employee = ctx.getBean("employeeId", Employee.class);
		System.out.println(employee);

		((ClassPathXmlApplicationContext) ctx).close();
	}
}

Ornegimizi calistirdigimizda ;

Employee [name=Levent, surname=Erguder, address=Address [city=Istanbul]]

Autowiring with constructor

Constructor uzerinden autowiring islemi byType modu wire/baglama islemi gibi calisir. byType setter metotlar uzerinden gerceklesirken constructor autowiring modunda ise yapilandirici/constructor kullanilir.

Employee.java

package _11.autowiring.constructor.model;

public class Employee {

	private String name;
	private String surname;
	private Address address;
	

	public Employee(Address address) {
		super();
		this.address = address;
	}
        //getters and setters
...
}

Address.java

package _11.autowiring.constructor.model;

public class Address {

	private String city;
        //getters and setters
...
}

11.autowiring.constructor.xml

<bean id="employeeId" class="_11.autowiring.constructor.model.Employee"
autowire="constructor">
    <property name="name" value="Levent" />
    <property name="surname" value="Erguder" />
</bean<


<bean id="addressId1" class="_11.autowiring.constructor.model.Address"<
    <property name="city" value="Istanbul" />
</bean<

AutowiringConstructorTest.java

package _11.autowiring.constructor.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import _11.autowiring.constructor.model.Employee;

public class AutowiringConstructorTest {

	public static void main(String[] args) {

		ApplicationContext ctx = new ClassPathXmlApplicationContext("11.autowiring.constructor.xml");

		Employee employee = ctx.getBean("employeeId", Employee.class);
		System.out.println(employee);

		((ClassPathXmlApplicationContext) ctx).close();
	}
}

Ornegimizi calistirdigimizda ;

Employee [name=Levent, surname=Erguder, address=Address [city=Istanbul]]

Github kaynak dosyalar/ source folder
leventerguder/injavawetrust-spring-tutorial

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *