Spring – 06 – Injection – Spring Expression Language

Merhaba Arkadaslar
Bu bolumde Spring Expression Language (SpEL) ile nasil Injection islemini gerceklestirdigimizi inceleyecegiz.

  • SpEL Injection with XML
  • SpEL Injection with Annotation

Spring Expression Language Injection with XML

Spring Expression Language , Spring 3.0 ile birlikte gelen bir ozelliktir. SpEL ile expression’lar dinamik olarak degerlendirilecek/evaluate kullanabiliriz.

InjectSimpleConfig.java

public class InjectSimpleConfig {

	private String name = "Levent";
	private String surname = "Erguder";
	private char gender = 'M';
	private int birthYear = 1989;
        //getters and setters
...
...
}

Person.java

public class Person {
	private String name;
	private String surname;
	private char gender;
	private int birthYear;
        //getters and setters
...
}

XML dosyamizda , #{beanId.propertyName} seklinde injection islemini gerceklestirebiliriz.

06.spel.xml

...
<bean id="configXML" class="_06.spel.model.InjectSimpleConfig" />

<bean id="personIdXML" class="_06.spel.model.Person">
	<property name="name" value="#{configXML.name}"/>
	<property name="surname" value="#{configXML.surname}"/>
	<property name="gender" value="#{configXML.gender}"/>
	<property name="birthYear" value="#{configXML.birthYear}"/>
</bean>
...

SpELTest.java

//imports
import _06.spel.model.Person;

public class SpELTest {

	public static void main(String[] args) {

		ApplicationContext ctx = new ClassPathXmlApplicationContext("06.spel.xml");

		Person person = ctx.getBean("personXIdML", Person.class);
		System.out.println(person);

		((ClassPathXmlApplicationContext) ctx).close();
	}
}

Ornegimizi calistirdigimizda ;

Person [name=Levent, surname=Erguder, gender=M, birthYear=1989]

Spring Expression Language Injection with Annotation

XML ile Spring Expression Language kullanabildigimiz gibi Annotation yardimi ile de Spring Expression Language kullanabiliriz.

InjectSimpleConfigAnnotation.java

import org.springframework.stereotype.Service;
@Service("configIdAnnotation")
public class InjectSimpleConfigAnnotation {

	private String name = "Levent";
	private String surname = "Erguder";
	private char gender = 'M';
	private int birthYear = 1989;
        //getters and setters
...
}

PersonAnnotation.java
@Value annotation ile Injection islemini gerceklestirebiliriz , @Value annotation ile Spring Expression Language kullanabiliriz. Autowired islemini kullanabilmek icin @Service annotation’i ekleyelim.

http://www.injavawetrust.com/spring-10-autowiring-with-component-scan-annotations/

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service("personIdAnnotation")
public class PersonAnnotation {
	
        @Value("#{configIdAnnotation.name}")
	private String name;
	@Value("#{configIdAnnotation.surname}")
	private String surname;
	@Value("#{configIdAnnotation.gender}")
	private char gender;
	@Value("#{configIdAnnotation.birthYear}")
	private int birthYear;
        //getters and setters
...
}

06.spel.annotation.xml
context:component-scan tag’ini kullanabilmek icin ilgili xlmns eklememiz gereklidir.
xmlns:context=”http://www.springframework.org/schema/context”
base-package attribute’unde scan/taranacak package bilgisini veriyoruz.
Boylece @Service olarak isaretledigimiz PersonAnnotation sinifi Spring tarafindan bulunacaktir.

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd">

	<context:component-scan base-package="_06.spel.model" />

</beans>
	

SpELAnnotationTest.java

//imports

public class SpELAnnotationTest {

	public static void main(String[] args) {

		ApplicationContext ctx = new ClassPathXmlApplicationContext("06.spel.annotation.xml");

		PersonAnnotation personAnnotationBean = ctx.getBean("personIdAnnotation", PersonAnnotation.class);
		System.out.println(personAnnotationBean);

		((ClassPathXmlApplicationContext) ctx).close();
	}
}

Ornegimizi calistiralim ;

Person [name=Levent, surname=Erguder, gender=M, birthYear=1989]

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 to Deneme Cancel reply

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