2013년 7월 23일 화요일

(ORACLEJAVA개발자실무교육,오엔제이프로그래밍실무교육센터)Spring SpEL을 이용한 value Injection(XML방식) - Spring expression language

Spring 3.X추가된 기능
- SpEL을 이용하면 동적으로 표현식을 해석하고 그 결과를 SpringApplicationContext에서 사용할 수있다.
- 결국 동적으로 생성된 값을 다른 자바 빈에 주입할 있다.


오라클자바커뮤니티에서 설립한 오엔제이프로그래밍 실무교육센터
(오라클SQL, 튜닝, 힌트,자바프레임워크, 안드로이드, 아이폰, 닷넷  실무전문 강의)

u app-context2.xml

 
<bean name="onj" class="onj.spel.injection.Onj"/>
<bean name="sample" class="onj.spel.injection.SpelInjectionExam">
<property name="name">
  <value>#{onj.name}</value> 
        <!--  spel을 통해서 Onj 객체에서 값 주입받음 -->
</property>
<property name="age">
  <value>#{onj.age}</value>
</property>
uOnj.java
 
package onj.spel.injection;
public class Onj {
    private String name="OnJ";
    private String age = "10";
/*
app-context2.xml에서 SpelInjectionExam 에 값을 주입하기 위해 getter만 만듬
#{onj.name} 구문에 의해 getter가 호출되고 그 값이 SpelInjectionExam에 주입된다.
*/
    public String getName() {
        return name;
    }
    public String getAge() {
        return age;
    }
}
u SpelInjectionExam.java
 
package onj.spel.injection;
import org.springframework.context.support.GenericXmlApplicationContext;
public class SpelInjectionExam {
private String name;
private String age;
// 빈 정의 XML에서 name이라는 Propertyvalue 값을 주입받음
public void setName(String name) {
this.name = name;
}
// 빈 정의 XML에서 age라는 Propertyvalue 값을 주입받음
public void setAge(String age) {
this.age = age;
}
public String toString() {
return "This is spel injection example... " +
           "Your name is " + name + " age is " + age;
}
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:app-context2.xml");
ctx.refresh();
SpelInjectionExam sample = (SpelInjectionExam) ctx.getBean("sample");
System.out.println(sample);
}
}
[결과]
This is spel injection example... Your name is OnJ age is 10

댓글 없음:

댓글 쓰기