springEL表达式_Spring详解

springEL表达式_Spring详解文章目录SpEL表达式SpEL表达式概述1、什么是SpEL表达式2、SpEL表达式的作用SpEL的使用方式1、xml配置的方式2、注解的方式SpEL表达式的调用SpEL表达式SpEL表达式概述1、什么是SpEL表达式SpEL:(springexpressionlanguage)是一种表达式语言,是一种强大,简洁的装配Bean的方式。他可以通过运行期间执行的表达式将值装配到我们的…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺


SpEL表达式

SpEL表达式概述

1、什么是SpEL表达式

  • SpEL:(spring expression language) 是一种表达式语言,是一种强大,简洁的装配Bean的方式。
  • 他可以通过运行期间执行的表达式将值装配到我们的属性或构造函数当中,也可以调用JDK中提供的静态常量,获取外部Properties文件中的的配置。

2、SpEL表达式的作用

  • 能够更加简单,多元的装配Bean,弥补了XML静态注入的不足。
  • 简化开发,减少了一些逻辑、配置的编写,使代码更加简洁。

SpEL表达式的格式:#{表达式}

SpEL的使用方式

1、xml配置的方式

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" 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">
	
	<bean id="MyMessage" class="com.jp.MyMessage">
        <property name="message" value="#{systemProperties['user.language']}"></property>
	</bean>

</beans>

代码

package com.jp;

public class MyMessage { 
   
	 
	private String message;
 
	public String getMessage() { 
   
		return message;
	}
 
	public void setMessage(String message) { 
   
		this.message = message;
	}
}

测试

package com.jp;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test { 
   
	public static void main(String[] args) { 
   
		ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
		MyMessage myMessage =context.getBean(MyMessage.class);
		System.out.println(myMessage.getMessage());
	}
}

结果
在这里插入图片描述
解释:这里使用了表达式#{systemProperties['user.language']}来设置值,用来检索用户语言系统的属性。

2、注解的方式

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" 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="com.jp"></context:component-scan>		
</beans>

代码

package com.jp;

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

@Component
public class MyMessage2 { 
   
	
	@Value("#{systemProperties['user.language']}")
	private String message;
 
	public String getMessage() { 
   
		return message;
	}
}

解释:这里使用了@Value注解的方式,当实例化MyMessage2这个bean的时候,将使用该注解设置默认值。此处还是使用了之前的SpEL表达式,来设置用户语言系统的属性。(在这里@Value注解既可以在类的字段属性上面,也可以在构造函数和方法上面使用)。
测试

package com.jp;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test { 
   
	public static void main(String[] args) { 
   
		ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
		MyMessage2 myMessage =context.getBean(MyMessage2.class);
		System.out.println(myMessage.getMessage());
	}
}

结果
在这里插入图片描述

SpEL表达式的调用

Peron类

package com.jp;

public class Person { 
   
	private String name;
	private Integer age;
	private Student s;
	
	public Person() { 
   
		super();
	}
	public Person(String name, Integer age) { 
   
		super();
		this.name = name;
		this.age = age;
	}
	
	public String getName() { 
   
		return name;
	}
	public void setName(String name) { 
   
		this.name = name;
	}
	public Integer getAge() { 
   
		return age;
	}
	public void setAge(Integer age) { 
   
		this.age = age;
	}
	public Student getS() { 
   
		return s;
	}
	public void setS(Student s) { 
   
		this.s = s;
	}	
}

Student类

package com.jp;

public class Student { 
   
	
	private String name;
	private Integer age;
	
	public Student() { 
   
		super();
	}
	public Student(String name, Integer age) { 
   
		super();
		this.name = name;
		this.age = age;
	}
	
	public String getName() { 
   
		return name;
	}
	public void setName(String name) { 
   
		this.name = name;
	}
	public Integer getAge() { 
   
		return age;
	}
	public void setAge(Integer age) { 
   
		this.age = age;
	}
	
}

配置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" 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">
	
	<bean id="p" class="com.jp.Person">

		<!-- value="xx" 等价于 value="#{stu.name}" value="stu.age" 等价于 value="#{stu.getAge()}" ref="stu" 等价于 value="#{stu}" -->
		<property name="name" value="#{stu.name}"></property>
		<property name="age" value="#{stu.getAge()>18?19:17}"></property>
		<property name="s" value="#{stu}"></property>
	</bean>
	
	<bean id="stu" class="com.jp.Student">
		<property name="name" value="大白"></property>
		<property name="age" value="18"></property>
	</bean>
</beans>

测试类

package com.jp;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Demo { 
   
	
	@Test
	public void testSpEL(){ 
   
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person p = (Person)ac.getBean("p");
		System.out.println(p.getName()+","+p.getAge()+","+p.getS());
	}
}

结果
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/195009.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Burp Suite安装配置详解(附Java 环境安装)

    Burp Suite安装配置详解(附Java 环境安装)BurpSuite安装配置详解1.Java安装与环境配置详解1.0下载JavaSDK1.8最新版2.0配置Java环境变量功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,…

    2022年7月12日
    52
  • 列车调度问题PTA

    列车调度问题PTA7-20列车调度(25分)火车站的列车调度铁轨的结构如下图所示。两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道。每趟列车从入口可以选择任意一条轨道进入,最后从出口离开。在图中有9趟列车,在入口处按照{8,4,2,5,3,9,1,6,7}的顺序排队等待进入。如果要求它们必须按序号递减的顺序从出口离开,则至少需要多少条平行铁轨用于调度?输入格…

    2022年7月14日
    17
  • NSGA2算法中文详解与MATLAB实现整理「建议收藏」

    NSGA2算法中文详解与MATLAB实现整理「建议收藏」NSGA2算法NSGA-II多目标遗传算法概述http://www.omegaxyz.com/2017/04/14/nsga-iiintro/NSGA2算法MATLAB实现(能够自定义优化函数)http://www.omegaxyz.com/2018/01/22/new_nsga2/NSGA2算法特征选择MATLAB实现(多目标)http://www.omegaxyz.co…

    2022年5月12日
    46
  • rotate 3D [初识篇]

    rotate 3D [初识篇]随着前端技术发展,尤其是html5中canvas和svg的应用,开始让web也可以轻易的渲染出各种绚丽的效果。本篇讨论的是基于rotate(旋转)的3d效果的初识。在canvas的getContext

    2022年8月1日
    20
  • DFS(深度优先搜索算法)「建议收藏」

    DFS(深度优先搜索算法)「建议收藏」基本概念深度优先搜索算法(DepthFirstSearch,简称DFS):一种用于遍历或搜索树或图的算法。沿着树的深度遍历树的节点,尽可能深的搜索树的分支。当节点v的所在边都己被探寻过或者在搜寻时结点不满足条件,搜索将回溯到发现节点v的那条边的起始节点。整个进程反复进行直到所有节点都被访问为止。属于盲目搜索,最糟糕的情况算法时间复杂度为O(!n)。算法思想回溯法(探索与回溯法…

    2022年6月18日
    29
  • java必背代码_Java编程中必须要死记硬背的几十个代码段

    java必背代码_Java编程中必须要死记硬背的几十个代码段向文件末尾添加内容字符串有整型的相互转换转字符串到日期java.util.Date=java.text.DateFormat.getDateInstance().parse(dateString);得到当前方法的名字StringmethodName=Thread.currentThread().getStackTrace()[1].getMethodName();使用JDBC链接O…

    2022年7月7日
    27

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号