Spring – Ders 10 Autowiring – 01

Merhaba Arkadaslar,
Bir kac yazim boyunca Spring de Autowiring kavramindan bahsedecegim.
Autowiring  , <constructor-arg> , <property> gibi elamanlara olan ihtiyaci azaltmak icin kullanilir. Spring , otomatik olarak bean bagimliliklarini cozebilir.
XML konfigurasyonunu minimize etmemizi saglar . Hem XML de autowiring islemini belirtebilecegimiz gibi hem de notasyon (@) ile bu islemi gerceklestirebiliriz.

4 cesit Autowiring yapisi vardir , bunlari sirasiyla isleyelim , ornek uzerinden gidelim.

byName ile autowiring
Employee sinifimizda 3 adet degiskenimiz olacak, burada dikkat etmemiz gereken Address turunde olan degiskenimizdir.

Employee.java

package erguder.levent._10.autowiring.byName;

public class Employee {
	private String name;
	private int age;
	private Address address;

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "Employee [name=" + name + ", age=" + age + ", address=" + address + "]";
	}

}

Address sinifimizda 2 adet degiskenimiz olacak , asil dikkat etmemiz gereken yapi xml dosyamizda olacak.

Address.java

package erguder.levent._10.autowiring.byName;

public class Address {

	private String city;
	private String district;

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getDistrict() {
		return district;
	}

	public void setDistrict(String district) {
		this.district = district;
	}

	@Override
	public String toString() {
		return "Address [city=" + city + ", district=" + district + "]";
	}

}


tutorial_10_byName.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="employeeid" class="erguder.levent._10.autowiring.byName.Employee"
		autowire="byName">
		<property name="name" value="Levent Erguder" />
		<property name="age" value="23" />
	</bean>

	<bean id="employeeid2" class="erguder.levent._10.autowiring.byName.Employee">
		<property name="address" ref="address2" />
		<property name="name" value="Burak Okumus" />
		<property name="age" value="22" />
	</bean>

	<!-- burada id address olmali yoksa problem cikartir null deger dondurur. -->
	<bean id="address" class="erguder.levent._10.autowiring.byName.Address">
		<property name="city" value="Istanbul" />
		<property name="district" value="Umraniye" />

	</bean>

	<bean id="address2" class="erguder.levent._10.autowiring.byName.Address">
		<property name="city" value="Kocaeli" />
		<property name="district" value="Izmit" />

	</bean>

</beans>

XML dosyamizi inceleyelim. Burada hem autowire hem de daha once kullandigimiz sekilde ref ozelligi ile beanleri bagladik.Su kisma dikkat edeim burada autowire ozelligini kullandik ve byName degerini verdik. Yani Spring’e , Employee sinifinda bulunan degiskenle(address) bean idsi ayni olan (bean id=”address”) bean’i kullanarak deger atamasini gerceklestir.

	<bean id="employeeid"
		autowire="byName">
		<property name="name" value="Levent Erguder" />
		<property name="age" value="23" />
	</bean>
	<bean id="address">
		<property name="city" value="Istanbul" />
		<property name="district" value="Umraniye" />

	</bean>


NOT
: Eger bean idsi address olmasa baglama islemi gerceklesmez ve NULL deger doner.

Diger bean wiring islemini daha onceki yazilarimda anlatmistim. Burada ref ozelligini kullanarak  ilgili bean’in id degerini verdik.

	<bean id="employeeid2">
		<property name="address" ref="address2" />
		<property name="name" value="Burak Okumus" />
		<property name="age" value="22" />
	</bean>
	<bean id="address2">
		<property name="city" value="Kocaeli" />
		<property name="district" value="Izmit" />

	</bean>


TestAutoWiringByName.java

package erguder.levent._10.autowiring.byName;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAutoWiringByName {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"tutorial_10_byName.xml");

		Employee emp1 = (Employee) context.getBean("employeeid");
		System.out.println(emp1);

		Employee emp2 = (Employee) context.getBean("employeeid2");
		System.out.println(emp2);
	}
}

Test sinifimizi calistirdigimizda su ciktiyi elde ederiz.

Employee [name=Levent Erguder, age=23, address=Address [city=Istanbul, district=Umraniye]]
Employee [name=Burak Okumus, age=22, address=Address [city=Kocaeli, district=Izmit]]

byType ile autowiring
Bir Autowiring yontemi olarak byType yontemi kullanilabilir. ByName ozelligine benzer olarak calisir. Spring , ilgili degiskenin (address) tipine uygun bir bean arayacaktir (Address bean’i). Burada aklimiza su gelebilir birden fazla Address bean i tanimlarsak ne olur ? Cevap biraz asagida,  okumaya devam…

Ayni siniflar uzerinden gidiyorum , xml dosyamizda degisiklik oldugu icin xml dosyamizdan devam edelim.

tutorial_10_byName.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<bean id="employeeid" class="erguder.levent._10.autowiring.byType.Employee"
		autowire="byType">
		<property name="name" value="Levent Erguder" />
		<property name="age" value="23" />
	</bean>

	<bean id="employeeid2" class="erguder.levent._10.autowiring.byType.Employee"
		autowire="byType">
		<property name="name" value="Recep Bostanci" />
		<property name="age" value="23" />
	</bean>

	<bean id="addressid" class="erguder.levent._10.autowiring.byType.Address">
		<property name="city" value="Istanbul" />
		<property name="district" value="Umraniye" />

	</bean>

	<bean id="addressid2" class="erguder.levent._10.autowiring.byType.Address"
		autowire-candidate="false">
		<property name="city" value="Kocaeli" />
		<property name="district" value="Izmit" />
	</bean>

</beans>

employee beanlerinde autowire ozelligini ve byType degerini kullandik. Spring,address degiskeni icin ilgili beani aramaya baslar burada 2 tane Address sinifindan bean tanimladik. Eger addressid2 olan beande autowire-candidate=false degerini atamasaydik
org.springframework.beans.factory.NoUniqueBeanDefinitionException tarzinda bir hata aliriz.

Test sinifimizi calistirdigimizda su sonucu elde ederiz..

Employee [name=Levent Erguder, age=23, address=Address [city=Istanbul, district=Umraniye]]
Employee [name=Recep Bostanci, age=23, address=Address [city=Istanbul, district=Umraniye]]

constructor ile autowiring

Beanleri birbirine baglarken constructor (yapilandirici) da ki eslesmeyi kullanir. Uygun bir bean bulunamazsa ilgili alan (address) null olarak doner.

Bu ornegimizde Address turunde bir arguman alan constructor (yapilandiric) tanimladik.

Employee.java

package erguder.levent._10.autowiring.constructor;

public class Employee {

	private Address address;
	private String name;
	private int age;

	public Employee(Address address) {
		this.address = address;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Employee [address=" + address + ", name=" + name + ", age=" + age + "]";
	}

}

xml dosyamizi inceleyelim…

	<bean id="employeeid1" class="erguder.levent._10.autowiring.constructor.Employee"
		autowire="constructor">
				<property name="name" value="Levent Erguder" />
				<property name="age" value="23" />			
		</bean>

	<bean id="address" class="erguder.levent._10.autowiring.constructor.Address">
		<property name="city" value="Istanbul" />
		<property name="district" value="Umraniye" />
	</bean>

autowire ozelligine bu sefer constructor degerini atadik. Employee sinifimizda tanimladigimiz yapilandirici araciligi ile  constructor wiring islemi gerceklestirilir.

	public Employee(Address address) {
		this.address = address;
	}

Test sinifimizi calistirdigimizda  su ciktiyi elde ederiz.
Employee [address=Address [city=Istanbul, district=Umraniye], name=Levent Erguder, age=23]

autodetect ile autowiring

autowire ozelligi icin autodetect degerini atadigimizda, tercih hakkini Spring e birakmis oluruz. Spring varsayilan olarak constructor a gore autowiring islemini gerceklestirmeye calisacaktir eger uygun yoksa bytype degerine gore autowiring islemini gerceklestirir.

Not : autodetect ornegi icin kullanilan xml dosyasinda , 3.0 yerine 2.5.xsd kullanilmistir.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

Yazima burada son veriyorum.
Kaynak kodlar :SpringProject_v10

Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
injavawetrust

Print Friendly, PDF & Email

Leave a Reply

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