<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>rayyildiz.com &#187; di</title>
	<atom:link href="http://rayyildiz.com/tag/di/feed/" rel="self" type="application/rss+xml" />
	<link>http://rayyildiz.com</link>
	<description>Bilgi Paylaşıldıkça Güzelleşir</description>
	<lastBuildDate>Mon, 16 Jan 2012 11:07:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>JDK6 da Dependency Injection (ServiceLoader)</title>
		<link>http://rayyildiz.com/2010/07/jdk6-da-dependency-injection-serviceloader/</link>
		<comments>http://rayyildiz.com/2010/07/jdk6-da-dependency-injection-serviceloader/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 23:33:53 +0000</pubDate>
		<dc:creator>rayyildiz</dc:creator>
				<category><![CDATA[Yazılım Geliştirme]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[di]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jdk6]]></category>

		<guid isPermaLink="false">http://rayyildiz.com/?p=226</guid>
		<description><![CDATA[DI ne olduğuna dair bir yazıyı buradan ulaşabilirsiniz. JDK6 da ise ServiceLoader gelmektedir. ServiceLoader sayesinde bir nevi DI sağlamış olmaktayız. Bunun nasıl yapacağımızı bir örnekle gösterelim. Uygulamanın örnek test kodlarını github üzerinden bulabilmeniz mümkündür. Bu örneği yapabilmek için 3 tane proje oluşturalım. Bu 3 proje ve açıklaması şu şekildedir: UserAuth : Arayüzün yer aldığı proje.IUserAuthService [...]]]></description>
			<content:encoded><![CDATA[<p>DI ne olduğuna dair bir yazıyı <a href="http://rayyildiz.net/2010/05/what-is-dependency-injection/" target="_blank">buradan</a> ulaşabilirsiniz. JDK6 da ise <a href="http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/util/ServiceLoader.html" target="_blank">ServiceLoader</a> gelmektedir. ServiceLoader sayesinde bir nevi DI sağlamış olmaktayız. Bunun nasıl yapacağımızı bir örnekle gösterelim.</p>
<p>Uygulamanın örnek test kodlarını <a href="http://github.com/rayyildiz/UserAuthSample" target="_blank">github</a> üzerinden bulabilmeniz mümkündür.</p>
<p>Bu örneği yapabilmek için 3 tane proje oluşturalım. Bu 3 proje ve açıklaması şu şekildedir:</p>
<p>UserAuth : Arayüzün yer aldığı proje.IUserAuthService diye bir arayüz yer almakta ve bu arayüz login diye bir metot tanımı içermektedir.</p>
<p>UserAuthImpl: Uygulamanın gerçekleştirildiği sınıf yer alır.</p>
<p>UserAuthTest: Main sınıfının yer aldığı örnek uygulamadır.</p>
<p><a href="http://www.rayyildiz.com/wp-content/uploads/2011/02/project_tree1.png" rel="lightbox[226]"><img class="alignleft size-medium wp-image-227" title="project_tree" src="http://www.rayyildiz.com/wp-content/uploads/2011/02/project_tree1.png?w=246" alt="" width="246" height="300" /></a>Arayüzün yer aldığı UserAuth projesinde farklı olarak META-INF/services klasörü- ve bu klasör içinde com.rayyildiz.userauth.IUserAuthService dosyası yer almaktadır. Bu dosya içine bakarsanız sadece bir satır yer almaktadır.</p>
<pre class="brush: java; title: ; notranslate">
com.rayyildiz.userauth.impl.UserAuthService
</pre>
<p>ServiceLoader bu dosyalara bakarak hangi implemantosyunu yüklemesi gerektiğini anlar. UserAuth.jar implementasyonun yer aldığı UserAuthImpl.jar referans olarak bilmez. SericeLoader load sırasında bu implementasyonu arayacaktır.</p>
<p>Burada asıl önemli test projemize bakalım:</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<pre class="brush: java; title: ; notranslate">
package com.rayyildiz.userauth;

import java.util.Iterator;
import java.util.ServiceLoader;

public class Main {
  public static void main(String[] args) {
    ServiceLoader&lt;IUserAuthService&gt; serviceLoader = ServiceLoader.load(IUserAuthService.class);
    Iterator&lt;IUserAuthService&gt; iterator = serviceLoader.iterator();-
    while(iterator!= null &amp;&amp; iterator.hasNext()){
      IUserAuthService userAuthService = iterator.next();
      boolean login = userAuthService.login(&quot;demo&quot;, &quot;password&quot;);
      System.out.println(&quot;Login for username: demo and password:password is &quot; + login);
   }
}
</pre>
<p>Gördüğünüz gibi test uygulaması implementasyon nesnesini görmez. ServiceLoader META-INF/services içinde yer alan dosyalara bakarak implementasyon nesnelerini örnek projenin olduğu klasörde ve CLASSPATH de arar.</p>
]]></content:encoded>
			<wfw:commentRss>http://rayyildiz.com/2010/07/jdk6-da-dependency-injection-serviceloader/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

