Spring – Ders 9 Spring Expression Language (SpEL) – 3

Merhaba Arkadaslar,
Bu yazimda Spring Expression Language (SpEL ) ve Collection yapisini inceleyecegiz.
Ornegimi java.util.List uzerinden yapacagim, daha onceki derslerde Set, Map, List uzerinde ornekler yapmistik.

Ornegimizde City sinifi, CityList sinifi ve CityTest sinifimiz olacak.
City sinifimida 3 tane degiskenimiz olacak.

City.java

package erguder.levent_09.spel.collection;

public class City {

	private String cityName;
	private String region;
	private Integer population;

        //getter setter
	@Override
	public String toString() {
		return "City [cityName=" + cityName + ", region=" + region + ", population=" + population + "]";
	}

}


CityList.java

package erguder.levent_09.spel.collection;

import java.util.List;

public class CityList {

	private City indexCity;
	private List<City> marmaraCities;
	private List<City> firstCity;
	private List<City> lastCity;
	private List<City> bigCities;
	private List<String> bigCitiesName;
	private List<String> cityNameList;
	//getter setter

}


tutorial.xml

	
        <bean id="city1" class="erguder.levent_09.spel.collection.City">
		<property name="cityName" value="Istanbul" />
		<property name="region" value="Marmara" />
		<property name="population" value="13000" />
	</bean>

        // city2 city3 city4 city5 beans seklinde devam ediyor

	<bean id="turkeyCityList" class="java.util.ArrayList">
		<constructor-arg>
			<list>
				<ref bean="city1"></ref>
				<ref bean="city2"></ref>
				<ref bean="city3"></ref>
				<ref bean="city4"></ref>
				<ref bean="city5"></ref>
			</list>
		</constructor-arg>
	</bean>

	<bean id="cityList" class="erguder.levent_09.spel.collection.CityList">
		<property name="indexCity" value="#{turkeyCityList[1]}" />
		<property name="marmaraCities" value="#{turkeyCityList.?[region eq 'Marmara']}"></property>
		<property name="firstCity" value="#{turkeyCityList.^[region eq 'Marmara']}"></property>
		<property name="lastCity" value="#{turkeyCityList.$[region eq 'Marmara']}"></property>
		<property name="bigCities" value="#{turkeyCityList.?[population gt 2000]}"></property>
		<property name="bigCitiesName" value="#{turkeyCityList.?[population gt 2000].![cityName]}"></property>
		<property name="cityNameList" value="#{turkeyCityList.![cityName]}" />

	</bean>
</beans>

xml dosyamizi incelemeye baslayalim. 5 tane City bean ‘i tanimliyoruz ve cityname, region, population degerlerini atiyoruz.
Daha sonrasinda turkeyCityList adinda yeni bir ArrayList bean ‘i olusturuyoruz ve listemize tanimladigimiz city bean’leri ekliyoruz.
SpEL kismi cityList bean’i ile basliyor. Simdi adim adim inceleyelim.

  1. indexCity degiskenimizi City turunde tanimlamistik. Burada #{turkeyCityList[1]} seklinde bir tanimlama ile turkeyList listesinin 1.indexli elamanini indexCity degiskenine atiyoruz.
  2. marmaraCities isimli degiskenimiz List turunden ve generic yapida tanimlandi. Selection operator (.?[] ) ve eq ( bir oncaki derste eq , gt gibi kavramlari gormustuk) ile turkeyCityList listesinde region degiskeninin degeri ‘Marmara’ olan ogeleri marmaraCities degiskenine atiyoruz.
  3. .^[] selection operatoru eslesen ilk ogeyi getirir.
  4. .$[] selection operatoru ise eslesen son ogeyi getirir.
  5. 2.maddeye benzer olarak burada gt (greater than ) kullandik.
  6. bigCitiesName degiskenimiz List<String> olarak tanimlandi,  yani sadece String turunde degerler alabilir. Burada 5.maddede yer alan ifadeye ek olarak .![]  projection operatorunu kullandik. Burada population i 2000 den buyuk olanlarin cityName degiskenini cekip aliyoruz.
  7. .![] projection operatorumuzu .?[] selection operatorumuzden bagimsiz olarak kullanarabiliriz. Tum sehirlerin isimlerini cekip alabiliriz.

CityTest.java

package erguder.levent_09.spel.collection;

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

import erguder.levent_05.collection.set.FriendSet;

public class CityTest {
	public static void main(String[] args) {

		ApplicationContext context = new ClassPathXmlApplicationContext("tutorial_09.xml");

		CityList cList = (CityList) context.getBean("cityList");

		System.out.println(cList.getIndexCity());
		System.out.println(cList.getMarmaraCities());
		System.out.println(cList.getFirstCity());
		System.out.println(cList.getLastCity());
		System.out.println(cList.getBigCities());
		System.out.println(cList.getBigCitiesName());
		System.out.println(cList.getCityNameList());

	}
}

Programimizi calistirdigimizda ;

09 spel  list output

Bu yazimi da burada bitiriyorum 🙂

Kaynak Kodlar: SpringProject_v9

Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
injavawetrust

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *