Spring EL hello worldの例
Spring ELはOGNLおよびJSF ELと似ており、Bean作成時に評価または実行されます。さらに、すべてのSpring式は、XMLまたはアノテーションを介して利用できます。
このチュートリアルでは、XMLとアノテーションの両方でString、Integer、Beanをプロパティに挿入する
Spring Expression Language(SpEL)
の使い方を説明します。
1. Spring EL依存性
Mavenの `pom.xml`ファイルの中核となるSpring Jarを宣言し、自動的にSpring EL依存関係をダウンロードします。
File:pom.xml
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependencies>
2. Spring Beans
2つのシンプルなBeanは、後でSpELを使用して、値をプロパティ、XML、アノテーションに挿入します。
package com.mkyong.core;
public class Customer {
private Item item;
private String itemName;
}
package com.mkyong.core;
public class Item {
private String name;
private int qty;
}
3. XMLのSpring EL
SpELは `#{SpEL expression}`で囲まれています。次のXML Bean定義ファイルの例を参照してください。
<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="itemBean" class="com.mkyong.core.Item">
<property name="name" value="itemA"/>
<property name="qty" value="10"/>
</bean>
<bean id="customerBean" class="com.mkyong.core.Customer">
<property name="item" value="#{itemBean}"/>
<property name="itemName" value="#{itemBean.name}"/>
</bean>
</beans>
-
#\ {itemBean}
– “customerBean” beanの “item”に “itemBean”を注入する
プロパティ。
-
#\ {itemBean.name}
– “itemBean”の “name”プロパティを
“customerBean” beanの “itemName”プロパティ。
4. AnnotationにおけるSpring EL
アノテーションモードで同等のバージョンを参照してください。
package com.mkyong.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("customerBean")
public class Customer {
@Value("#{itemBean}")
private Item item;
@Value("#{itemBean.name}")
private String itemName;
//...
}
package com.mkyong.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("itemBean")
public class Item {
@Value("itemA")//inject String directly
private String name;
@Value("10")//inject interger directly
private int qty;
public String getName() {
return name;
}
//...
}
自動コンポーネントスキャンを有効にする。
<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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.mkyong.core"/>
</beans>
アノテーションモードでは、Spring ELを定義するために `@ Value`を使います。この場合、文字列と整数値を直接「
itemBean
」に挿入し、後で「
customerBean
」プロパティに「itemBean」を挿入します。
5.出力
それを実行すると、XMLのSpELと注釈の両方に同じ結果が表示されます。
package com.mkyong.core;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[]args) {
ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");
Customer obj = (Customer) context.getBean("customerBean");
System.out.println(obj);
}
}
出力
Customer[item=Item[name=itemA, qty=10], itemName=itemA].... === ソースコードをダウンロードする ダウンロードする - リンク://wp-content/uploads/2011/06/Spring3-EL-Hello-Worldr-Example.zip[Spring3-EL-Hello-Worldr-Example.zip](6 KB) === リファレンス . http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/expressions.html[Spring ELリファレンス] link://tag/hello-world/[hello world]link://タグ/spring-el/[spring el]link://tag/spring3/[spring3]