007_SpEL表达式
生活随笔
收集整理的這篇文章主要介紹了
007_SpEL表达式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. SpEL: Spring Expression Language, Spring的表達式語言。
2.?SpEL的屬性注入(Spring3.0以后), 語法: #{SpEL}。
3. SpEL的使用
4. SpEL實例
4.1. 新建一個名為SpringDI_SpEL的Java工程, 拷入Spring相關包
4.2. 創建Teacher.java
package com.lywgames.bean;public class Teacher {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Teacher [id=" + id + ", name=" + name + "]";}}4.3. 創建Clazz.java
package com.lywgames.bean;public class Clazz {private Teacher teacher;private int teacherId;private String teacherName;public Teacher getTeacher() {return teacher;}public void setTeacher(Teacher teacher) {this.teacher = teacher;}public int getTeacherId() {return teacherId;}public void setTeacherId(int teacherId) {this.teacherId = teacherId;}public String getTeacherName() {return teacherName;}public void setTeacherName(String teacherName) {this.teacherName = teacherName;}@Overridepublic String toString() {return "Clazz [teacher=" + teacher + ", teacherId=" + teacherId + ", teacherName=" + teacherName + "]";}}4.4. 創建Test.java
package com.lywgames;import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lywgames.bean.Clazz; import com.lywgames.bean.Teacher;public class Test {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");System.out.println(context.getBean(Teacher.class).toString());System.out.println(context.getBean(Clazz.class).toString());context.close();} }4.5. 在src目錄下創建applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <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.xsd"><!-- 表達式注入 --><bean id="teacher" class="com.lywgames.bean.Teacher"><property name="id" value="#{901}"></property><property name="name" value="#{'xiaocui'}"></property></bean> <bean id="clazz" class="com.lywgames.bean.Clazz"> <property name="teacher" value="#{teacher}"></property><!-- 調用其它類的方法給我們的類屬性注入值 --><property name="teacherId" value="#{teacher.getId()}"></property><!-- 調用其它類的屬性給我們的類屬性注入值 --><property name="teacherName" value="#{teacher.name}"></property></bean> </beans>4.6. 運行項目
?
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的007_SpEL表达式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 006_P名称空间的属性注入
- 下一篇: 008_多配置文件的引入