Menu
Software Engineering Notes
  • About Me
Software Engineering Notes

Spring Framework’e Genel Bakış

Posted on 15 Eylül 201815 Eylül 2018 by ogtadev

Neden Spring Framework ? 

  • Modüler, sök parçala çıkar değiştir.
  • Güzel paketler var. Quartz, JEE, Logging ve ORM konusunda bazı sorunları aşmış kaya gibi bir yapı var.
  • MVC yine bir gün hayat kurtarıyor.
  • Hafif, taşınabilir.
  • Scale edilebildiği söyleniyor. Gözlemleme fırsatım olmadı.
  • Inversion of Control (IoC)

Kısacası uygulama içinde yer alan nesne yaratma sürecinin bizden alınıp frameworke devredilmesi olayı. Dil bağımlılığı yok. İster java ister .NET. Genel bir kavram.

1
2
3
4
5
6
7
8
9
10
11
public class Voltran {
 
    private GucKaynagi kaynak;
 
    public Voltran() {
 
        kaynak= new GucKaynagi();
 
    }
 
}

Voltran sınıfı artık GucKaynagina doğrudan bağlı. Peki Güç Kaynağı sınıfı değişirse ne olacak. Voltran sınıfı da etkilenecek.

IoC ile güç kaynağı sınıfı itediği kadar değişsin Voltran sınıfının bundan haberi olmasın bu değişimlerle ilgilinmesin.

Olması gereken sınıf :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Voltran {    
 
  private IGucKaynagi kaynak;
 
  public setGucKaynagi(IGucKaynagi kaynak) {
 
        this.kaynak = kaynak
 
  }
 
  public Voltran() { 
 
     voltran.kullan(kaynak); // bu kismi salladim..  
 
  }
 
}

IGucKaynagi ‘ndaki I interface demek.

Voltran ile ona güç sağlayan güç kaynağı arasındaki bağı kestik buna decoupling denir.

Spring BeanFactory Container

Yukartta bahsettiğimiz DI BeanFactory interface’i ile sağlanmakta.

Örnek:

HelloWorld.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.tutorialspoint;  
 
public class HelloWorld { 
 
   private String message;  
 
   public void setMessage(String message){ 
 
      this.message  = message; 
 
   }  
 
   public void getMessage(){ 
 
      System.out.println("Your Message : " + message); 
 
   } 
 
}

MainApp.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.tutorialspoint;  
 
import org.springframework.beans.factory.InitializingBean; 
 
import org.springframework.beans.factory.xml.XmlBeanFactory; 
 
import org.springframework.core.io.ClassPathResource;  
 
public class MainApp { 
 
   public static void main(String[] args) { 
 
      XmlBeanFactory factory = new XmlBeanFactory (new ClassPathResource("Beans.xml")); 
 
      HelloWorld obj = (HelloWorld) factory.getBean("helloWorld");    
 
      obj.getMessage();    
 
   }
 
}

Beans.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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 = "helloWorld" class = "com.tutorialspoint.HelloWorld">
 
      <property name = "message" value = "Hello World!"/>
 
   </bean>
 
</beans>

Hibernate projeye dahil edilirken ilgili db bilgileri beans.xml içine set edilir.

Bean Scopes

  1. Singleton

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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 = "helloWorld" class = "com.tutorialspoint.HelloWorld" scope = "singleton">
 
   </bean>
 
</beans>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.tutorialspoint;
 
import org.springframework.context.ApplicationContext;
 
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class MainApp {
 
   public static void main(String[] args) {
 
      ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
 
      HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
 
      objA.setMessage("I'm object A");
 
      objA.getMessage();
 
      HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
 
      objB.getMessage();
 
   }
 
}

1
2
3
Your Message : I'm object A
 
Your Message : I'm object A

Tek bir nesne oluşması ve her seferinde aynı değeri dönmesi sağlanacaktır.

  1. Prototype

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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 = "helloWorld" class = "com.tutorialspoint.HelloWorld" scope = "prototype">
 
    </bean>
 
</beans>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.tutorialspoint;
 
import org.springframework.context.ApplicationContext;
 
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class MainApp {
 
   public static void main(String[] args) {
 
      ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
 
      HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
 
      objA.setMessage("I'm object A");
 
      objA.getMessage();
 
      HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
 
      objB.getMessage();
 
 }
 
}

1
2
3
Your Message : I'm object A
 
Your Message : null

Her çağrılışta yeni bir nesne oluşturur.

 

Not: Yazı Spring öğrenirken internet üzerinden aldığım notlardan oluşmaktadır. Üzülerek farkettim ki kullandığım örneklerin kaynakcalarından bazılarının linklerini kaydetmemişim. Eğer örnekler denk gelirse linkleri yorumdan iletirseniz eklemekten mutluluk duyarım.

 

Kaynakça:

tutorialspoint

Bir cevap yazın Cevabı iptal et

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

Kategoriler

  • Java (3)
  • Know How (1)

Son Yazılar

  • REST Servis Standartları
  • Dependency Injection Nedir?
  • Spring MVC
  • Spring Framework’e Genel Bakış

Arşivler

  • Ağustos 2019
  • Temmuz 2019
  • Eylül 2018
©2021 Software Engineering Notes | Powered by WordPress & Superb Themes