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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • RIder2021激活码(JetBrains全家桶)「建议收藏」

    (RIder2021激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlS32PGH0SQB-eyJsa…

    2022年3月25日
    2.5K
  • php批量修改怎么实现,PinPHP购物分享系统2.2后台批量采集修改实现方法

    php批量修改怎么实现,PinPHP购物分享系统2.2后台批量采集修改实现方法因前段时间较忙,所以一直将这开发搁置了。今天看了一下新版的PinPHP,又心血来潮于是写了一下这个批量采集的实现,没想到写了差不多一两小时就实现了,虽然写得比较简单,也算是可以帮助一键采集一个分类。同时非常感谢PinPHP团队开发出如此好使的开源程序,哈,闲话先不多说,上代码。附源文件:下载源代码请猛击这里>>主要是对一个模板文件作了修改。/PinPHP_V2.21/admin/Tpl…

    2022年6月6日
    23
  • hive:函数:length[通俗易懂]

    hive:函数:length[通俗易懂]length函数:语法:length(stringA) 返回值:int 说明:返回字符串A的长度如:***wherelength(name)>2

    2022年5月10日
    63
  • 五种matlab读取excel文件方法

    五种matlab读取excel文件方法读取 excel 文件的五种方式 readcell filename 以 cell 元胞数组 注 访问 cell 元素用 格式导入所有信息 空格被识别为 missing 用 strcmp class references 2 3 missing 来判定 若为 1 则说明为空 最省事的方法 NUM TXT RAW xlsread filename NUM 返回的是 excel 中的数据 TXT 输出的是文本内容 RAW 输出的是未处理数据 一般情况下 我们直接用 reference xlsread exa

    2025年9月26日
    2
  • 华为kirin710f处理器怎么样是哪个手机型号(kirin710f是什么处理器)

    华为kirin710f处理器相当于高通骁龙636,麒麟710f是一个中端处理器,麒麟710采用4乘以A732.2GHz+4乘以A531.7GHz,独立DSP,ISP加持,支持LTECat.12/13,双卡双4G双voLTE,而荣耀8x搭载的是麒麟710f处理器,通过对荣耀8x安兔兔跑分测试,麒麟710安兔兔跑分大约在13万分左右,从跑分来看,麒麟710差不多与高通骁龙636差不多,目前搭载骁…

    2022年4月13日
    273
  • 零基础学Java(1)初识Java程序「建议收藏」

    零基础学Java(1)初识Java程序「建议收藏」前言就国内来说,Java毫无疑问是后端语言中的No.1没有之一,所以今天我们也来0基础学习Java!!!Java的好处(针对测试工程师)面试加分->涨薪大多数公司服务端用的都是Java,

    2022年7月29日
    4

发表回复

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

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