Spring – 04 – Injection – Setter Injection & Constructor Injection

Merhaba Arkadaslar.
Setter Injection ve Constructon Injection ornegi yapacagiz.

  • Setter Injection
  • Constructor Injection

Setter Injection

XML konfigurasyonu kullanarak Setter Injection’i gerceklestirebiliriz. Bunun icin <property> etiketini <bean> etiketi icerisinde kullanabiliriz.

Employee.java

package _03.setter.injection.model;

public class Employee {

	private String name;
	private String surname;
	private long salary;
	private int birthYear;
	private char gender;

	//getters and setters
	//toString

}

03.appcontext.setter.injection.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.xsd">

	<bean id="empId" class="_03.setter.injection.model.Employee">
		<property name="name" value="Levent" />
		<property name="surname" value="Erguder" />
		<property name="salary" value="100" />
		<property name="birthYear" value="1989" />
		<property name="gender" value="M" />
	</bean>
</beans>

XML konfigurasyon dosyamiza namespace olarak su satiri ekledigimizde ;

xmlns:p="http://www.springframework.org/schema/p"

p namespace ile Setter Injection’i gerceklestirebiliriz.

	<bean id="empId2" class="_03.setter.injection.model.Employee"
		p:name="Test" 
		p:surname="TestSurname" 
		p:salary="100" 
		p:birthYear="1989"
		p:gender='M' />

<property> etiketinde value attribute ile deger atamasinda bulunduk. Benzer sekilde ref attribute ile bean atamasi yapabiliriz.

EmployeeSetterInjectionTest.java
DefaultListableBeanFactory yerine GenericXmlApplicationContext de kullanabiliriz.

package _03.setter.injection.test;

import org.springframework.context.support.GenericXmlApplicationContext;
import _03.setter.injection.model.Employee;

public class EmployeeSetterInjectionTest {

	public static void main(String[] args) {

		// Instead of DefaultListableBeanFactory, an instance of
		// GenericXmlApplicationContext is instantiated.
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
		ctx.load("classpath:03.appcontext.setter.injection.xml");
		ctx.refresh();

		Employee employee = ctx.getBean("empId", Employee.class);
		Employee employee2 = ctx.getBean("empId2", Employee.class);
		System.out.println(employee);
		System.out.println(employee2);

		ctx.close();

	}
}

Ornegimizi calistirdigimizda ;

...
Employee [name=Levent, surname=Erguder, salary=100, birthYear=1989, gender=M]
Employee [name=Test, surname=TestSurname, salary=100, birthYear=1989, gender=M]
...

Bir baska ornek olarak Computer ve Keyboard siniflari olusturalim.
Computer HAS-A Keyboard iliskisi olsun.

Computer.java

package _03.setter.injection.model;

public class Computer {

	private String brand;
	private int price;
	private Keyboard keyboard;

	//getters and setters
	//toString
	
}

Keyboard.java

package _03.setter.injection.model;

public class Keyboard {

	private String model;

	//getters and setters
	//toString
}

Computer HAS-A Keyboard iliskisi var. ref attribute kullanarak bean id bilgisi verebiliriz.

03.appcontext.setter.injection.xml

....
	<bean id="computerId" class="_03.setter.injection.model.Computer">
		<property name="brand" value="Acer 7741G"/>
		<property name="price" value="500"/>
		<property name="keyboard" ref="keyboardId"/>
	</bean>
		
	<bean id="keyboardId" class="_03.setter.injection.model.Keyboard">
		<property name="model" value="K360"/>
	</bean>
....

ComputerSetterInjectionTest.java

package _03.setter.injection.test;

import org.springframework.context.support.GenericXmlApplicationContext;
import _03.setter.injection.model.Computer;

public class ComputerSetterInjectionTest {

	public static void main(String[] args) {

		// Instead of DefaultListableBeanFactory, an instance of
		// GenericXmlApplicationContext is instantiated.
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
		ctx.load("classpath:03.appcontext.setter.injection.xml");
		ctx.refresh();

		Computer computer = ctx.getBean("computerId", Computer.class);
		System.out.println(computer);

		ctx.close();

	}
}

Ornegimizi calistirdigimizda ;

...
Computer [brand=Acer 7741G, price=500, keyboard=Keyboard [model=K360]]
...

Constructor Injection

Dependency Injection teknigini setter metotlar uzerinden gerceklestirebilecegimiz gibi constructor/yapilandirici uzerinden de gerceklestirebiliriz.

Constructor Injection yaklasiminda <property> etiketi yerine <constructor-arg> etiketini kullanabiliriz.

Employee.java

package _04.constructor.injection.model;

public class Employee {
	private String name;
	private String surname;
	private long salary;
	private int birthYear;
	private char gender;
	
	public Employee(String name, String surname, long salary, int birthYear, char gender) {
		super();
		this.name = name;
		this.surname = surname;
		this.salary = salary;
		this.birthYear = birthYear;
		this.gender = gender;
	}
....
}

XML konfigurasyonunda constructor-arg etiketini kullanacagiz benzer sekilde namespace olarak xlmns:c kullanabiliriz.

xmlns:c="http://www.springframework.org/schema/c"

04.appcontext.constructor.injection.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:c="http://www.springframework.org/schema/c"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="empId" class="_04.constructor.injection.model.Employee">
		<constructor-arg value="Levent"/>
		<constructor-arg value="Erguder"/>
		<constructor-arg value="100"/>
		<constructor-arg value="1989"/>
		<constructor-arg value="M"/>
	</bean>
	
	<bean id="empId2" class="_04.constructor.injection.model.Employee"
		c:name="Test" 
		c:surname="TestSurname" 
		c:salary="100" 
		c:birthYear="1989"
		c:gender='M' />

</beans>

EmployeeConstructorInjectionTest.java

package _04.constructor.injection.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import _04.constructor.injection.model.Employee;

public class EmployeeConstructorInjectionTest {

	public static void main(String[] args) {

		ApplicationContext ctx = new ClassPathXmlApplicationContext("04.appcontext.constructor.injection.xml");
		Employee employee = ctx.getBean("empId", Employee.class);
		Employee employee2 = ctx.getBean("empId2", Employee.class);

		System.out.println(employee);
		System.out.println(employee2);

		((ClassPathXmlApplicationContext) ctx).close();

	}
}

Ornegimizi calistirdigimizda;

...
Employee [name=Levent, surname=Erguder, salary=100, birthYear=1989, gender=M]
Employee [name=Test, surname=TestSurname, salary=100, birthYear=1989, gender=M]
...

Bir baska ornek olarak simdi de ref attribute ornegini constructor icin uygulayalim.
Yine Computer ve Keyboard siniflari olusturalim.

Computer.java

package _04.constructor.injection.model;

public class Computer {

	private String brand;
	private int price;
	private Keyboard keyboard;

	public Computer(String brand, int price, Keyboard keyboard) {
		super();
		this.brand = brand;
		this.price = price;
		this.keyboard = keyboard;
	}

       //getters and setters
       //toString
}

Keyboard.java

package _04.constructor.injection.model;

public class Keyboard {

	private String model;

	public Keyboard(String model) {
		super();
		this.model = model;
	}

        //getters and setters
        //toString

}

XML konfigurasyonunda constructor-arg etiketinde value yerine ref attribute bilgisini kullandik.

<bean id="computerId" class="_04.constructor.injection.model.Computer">
	<constructor-arg value="Acer 7741G"/>
	<constructor-arg value="500"/>
	<constructor-arg ref="keyboardId"/>
</bean>

<bean id="keyboardId" class="_04.constructor.injection.model.Keyboard">
	<constructor-arg value="K360"/>
</bean>

CoputerConstructorInjectionTest.java

package _04.constructor.injection.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import _04.constructor.injection.model.Computer;

public class CoputerConstructorInjectionTest {

	public static void main(String[] args) {

		ApplicationContext ctx = new ClassPathXmlApplicationContext("04.appcontext.constructor.injection.xml");
		Computer computer = ctx.getBean("computerId", Computer.class);
		System.out.println(computer);

		((ClassPathXmlApplicationContext) ctx).close();

	}
}

Ornegimizi calistirdigimizda ;

Computer [brand=Acer 7741G, price=500, keyboard=Keyboard [model=K360]]

Bir baska ornek olarak su ornegimizi inceleyelim ;

ConstructorConfuse.java

package _04.constructor.injection.model;

public class ConstructorConfuse {

	private String strProperty;
	private int intProperty;

	public ConstructorConfuse() {

	}

	public ConstructorConfuse(String strProperty) {
		super();
		this.strProperty = strProperty;
	}

	public ConstructorConfuse(int intProperty) {
		super();
		this.intProperty = intProperty;
	}

	@Override
	public String toString() {
		return "ConstructorConfuse [strProperty=" + strProperty + ", intProperty=" + intProperty + "]";
	}
	

}

bean tanimimizi yapalim. <value> tagini kullanabiliriz.

<bean id="constructorConfuseId"	class="_04.constructor.injection.model.ConstructorConfuse">
     <constructor-arg>
	  <value>100</value>
     </constructor-arg>
</bean>

XML konfigurasyon dosyamizda bean taniminda type bilgisi kullanmadigimiz icin varsayilan olarak String parametre alan constructor kullanilacaktir. Eger int parametre alan constructor’i kullanmak istiyorsak bu durumda type bilgisi vermemiz gereklidir.

<bean id="constructorConfuseId2" class="_04.constructor.injection.model.ConstructorConfuse">
     <constructor-arg type="int" value="100"/> 
</bean>

ConfuseConstructorInjectionTest.java

package _04.constructor.injection.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import _04.constructor.injection.model.ConstructorConfuse;

public class ConfuseConstructorInjectionTest {

	public static void main(String[] args) {

		ApplicationContext ctx = new ClassPathXmlApplicationContext("04.appcontext.constructor.injection.xml");

		ConstructorConfuse confuse = ctx.getBean("constructorConfuseId", ConstructorConfuse.class);
		ConstructorConfuse confuse2 = ctx.getBean("constructorConfuseId2", ConstructorConfuse.class);

		System.out.println(confuse);
		System.out.println(confuse2);

		((ClassPathXmlApplicationContext) ctx).close();

	}
}

...
ConstructorConfuse [strProperty=100, intProperty=0]
ConstructorConfuse [strProperty=null, intProperty=100]
...

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 *