Spring-1.14 SpEl表式(1)-プロファイルと埋め込み値を最初から認識する

3668 ワード

この章ではSpEl表現の概要と埋め込み値について説明します.
1.SpEl表現の概要
Spring Excpression Language(SpEL)言語は、実行時の操作と照会対象をサポートします.
実行中に該当するオブジェクトや値を操作します.
2.SpEl埋め込み値
(1)domain
ケーキ類:(不変)
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_14;

public class Cake {

	private int id = 0;

	private String name = "";

	private double size = 0;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getSize() {
		return size;
	}

	public void setSize(double size) {
		this.size = size;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Override
	public String toString() {
		return "cake id:" + id + " name:" + name + " size:" + size;
	}

}
(2)試験類:(ケーキのオブジェクトを印刷する属性)
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_14;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
		"/com/raylee/my_new_spring/my_new_spring/ch01/topic_1_14/ApplicationContext-test.xml" })
public class CakeTest {

	@Autowired
	private ApplicationContext applicationContext;

	@Test
	public void testCake() {
		Cake cake = applicationContext.getBean(Cake.class);
		System.out.println(cake);
	}
}
(3)プロファイル
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

	<bean id="cake"
		class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_14.Cake">
		<property name="id" value="#{1}"/>
		<property name="name" value="#{'blueberry cheese cake'}"/>
	<!-- <property name='name' value='#{"blueberry cheese cake"}'/> -->
		<property name="size" value="#{5.5}"/>
	</bean>

</beans>
実際の値をそれぞれの属性に埋め込むということは、もちろん埋め込み値という意味ではないです.
テスト出力:
cake id:1 name:blueberry cheese cake size:5.5
まとめ:この章ではSpEl表現の概要と埋め込み値を紹介します.
ディレクトリ:http://blog.csdn.net/raylee2007/article/details/50611627 
 
私のgithub:https://github.com/raylee2015/my_new_スプリング