Spring – 09 – Autowiring with Annotation

Merhaba Arkadaslar
Onceki bolumde XML konfigurasyonu ile autowiring’in nasil yapildigini inceledik.
Bu bolumde @Autowired annotation ile autowiring isleminin isleminin nasil yapilacagini inceleyecegiz.

@Autowired annotation’i property/instance variable uzerinde , setter metot uzerinde ya da constructor uzerinde yazabiliriz.

Employee sinifi icin 2 tane composition (HAS-A) iliskisi tanimlayacagiz.
Employee HAS-A Department
Employee HAS-A Address

Address.java

package _12.autowiring.annotation.model;

public class Address {

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

Department.java

package _12.autowiring.annotation.model;

public class Department {

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

@Autowired annotation kullanabilmek icin <context:annotation-config> tagini XML dosyamiza eklememiz gereklidir.
Package scan yapmadigimiz durumda <context:component-scan> kullanmasak da olur. Buradaki ornekte oldugu gibi bean tanimlarini XML de yaptigimiz durumda autowiring islemi icin <context:annotation-config> yeterli olacaktir.

12.autowiring.annotation.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"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd">

	<context:annotation-config />
...

Daha sonrasinda bean tanimlamalarimizi yapalim ;

	<bean id="employeeId" class="_12.autowiring.annotation.model.Employee">
		<property name="name" value="Levent" />
		<property name="surname" value="Erguder" />
	</bean>


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

	<bean id="addressId2" class="_12.autowiring.annotation.model.Address">
		<property name="city" value="Kocaeli" />
	</bean>

	<bean id="departmentId1" class="_12.autowiring.annotation.model.Department">
		<property name="departmentName" value="Software Engineer" />
	</bean>

Burada birden fazla Address bean’i tanimladik. Hangisini kullanacagimizi secmek icin @Qualifier annotation’i kullanabiliriz.

Employee.java
@Autowired annotation’i constructor ve property/instance variable uzerinde tanimlayabiliriz.
@Qualifier annotation kullanarak bean id addressId1 olan Address bean’i inject etmesini sagliyoruz , burada @Qualifier annotation kullanmazsak Address sinifi icin birden fazla bean tanimi oldugu icin hataya neden olacaktir.

package _12.autowiring.annotation.model;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Employee {

	private String name;
	private String surname;
	private Department department;

	@Autowired
	public Employee(Department department) {
		this.department = department;
	}

	@Autowired
	@Qualifier(value = "addressId1")
	private Address address;
        //getters and setters
...
}

AutowiringAnnotationTest.java

package _12.autowiring.annotation.test;

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

import _12.autowiring.annotation.model.Employee;

public class AutowiringAnnotationTest {

	public static void main(String[] args) {

		ApplicationContext ctx = new ClassPathXmlApplicationContext("12.autowiring.annotation.xml");

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

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

Ornegimizi calistirdigimizda;

Employee [name=Levent, surname=Erguder, department=Department [departmentName=Software Engineer], 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 *