Merhaba Java dostları,

Daha önceki yazımda  Spring annotation configuration nasıl tanımlanır konusuna değinmiştim. Bu yazımda ise nesnelerin birbirilerine olan bağımlılıklarının nasıl düzenleneceğini anlatmak istiyorum.

Bir sınıfta başka bir sınıfa ihtiyaç duyulması durumu kaçınılmazdır. Spring’ in temel çıkış noktalarından birisi olan dependency injection ise bu durumda vazgeçilmez vazgeçilmemesi gereken bir tasarım olarak ortaya çıkıyor. Yani A nesnesi ihtiyaç duyduğu B nesnesini direkt olarak constructor veya set metotları aracılığı ile kabul edecektir. Kendi içersinde new ile oluşturup gerekli işlemleri yapması istenilmeyen bir durumdur.

Yukarıda anlatmaya çalıştığım durumu sağlamak için spring annotation injection nasıl yapılır hemen bakalım. İşte örnek sınıflarımız.

package anajavatica.spring.bean.annotation.injection;

public class Honey {

private String honeyName = “”;

public Honey(String honeyName) {
this.honeyName = honeyName;
}

public String getHoneyName() {
return honeyName;
}

}

package anajavatica.spring.bean.annotation.injection;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class HoneyConfig {

@Bean(name = “sweetHoney”)
@Scope(value = “prototype”)
public Honey createHoney() {
return new Honey(“Sweet honey”);
}

@Bean(name = “oldHoney”)
@Scope(value = “prototype”)
public Honey createHoney2() {
return new Honey(“Old honey”);
}

}

Burada Honey sınıfına ait iki tane prototype bean tanımlıyoruz. Dikkatinizi çekmek istediğim bir nokta da @Bean içerisindeki name değerleri. Bunları ileride kullanacağız.

package anajavatica.spring.bean.annotation.injection;

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

public class Bear {

@Autowired
private Honey sweetHoney = null;

@Autowired
private Honey oldHoney = null;

public void sweetHoneyParty() {
System.out.println(“I’ m eating “.concat(sweetHoney.getHoneyName()));
}

public void oldHoneyParty() {
System.out.println(“I’ m eating “.concat(oldHoney.getHoneyName()));
}

}

 

@Autowired annotation tanımladığımız beanlerin birbirileri ile olan ilişkilerini ve atamalarını sağlayan temel anahtar. @Autowired olarak işaretlenen alanların isimlendirmesine dikkat edin. Bir önceki HoneyConfig sınıfında verilen bean name ile aynı. Böyle Spring bizim yerimize atamaları sağlıyor.

package anajavatica.spring.bean.annotation.injection;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BearConfig {

@Bean(name = “bear”)
public Bear createBear() {
return new Bear();
}

}

 

Yine klasik bir bean tanımı ve son olarak da main sınıfımız.

package anajavatica.spring.bean.annotation.injection;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class HoneyMain {

public static void main(String[] args) {

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(HoneyConfig.class, Bear.class);
Bear bear = context.getBean(“bear”, Bear.class);
bear.oldHoneyParty();
bear.sweetHoneyParty();
context.close();

}

}

 

Çok basit bir @Autowired annotation ile nesne bağımlılıklarını yönetmiş olduk ve böylece bu yazımda Spring Annotation Injection konusunu elimden geldiğince anlatmaya çalıştım.

Bol Java’ lı günler dileğiyle… 😀

No responses yet

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir