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


相关推荐

  • pytest重试_pytest失败重跑

    pytest重试_pytest失败重跑安装:pip3installpytest-rerunfailures重新运行所有失败用例要重新运行所有测试失败的用例,请使用–reruns命令行选项,并指定要运行测试的最大次数:$py

    2022年7月29日
    6
  • 《梦断代码》摘录及感悟「建议收藏」

    《梦断代码》摘录及感悟

    2022年1月22日
    37
  • Spring的contextConfigLocation

    Spring的contextConfigLocationspring如何使用多个xml配置文件1,在web.xml中定义contextConfigLocation参数.spring会使用这个参数加载.所有逗号分割的xml.如果没有这个参数,spring默认加载web-inf/applicationContext.xml文件.例如:<context-param><param-name>conte…

    2022年7月14日
    15
  • LLDP简介

    LLDP简介1.1.1LLDP产生背景目前,网络设备的种类日益繁多且各自的配置错综复杂,为了使不同厂商的设备能够在网络中相互发现并交互各自的系统及配置信息,需要有一个标准的信息交流平台。LLDP(LinkLayerDiscoveryProtocol,链路层发现协议)就是在这样的背景下产生的,它提供了一种标准的链路层发现方式,可以将本端设备的信息(包括主要能力、管理地址、设备标识、接口标识等)组织成不同的TLV(Type/Length/Value,类型/长度/值),并封装在LLDPDU(Lin…

    2022年5月28日
    92
  • matlab 柱状图不同颜色(取巧哈)[通俗易懂]

    matlab 柱状图不同颜色(取巧哈)[通俗易懂]以前写过一个颜色索引的柱状图,但是年代久远想不起来了。今天需要出一个不同颜色的柱状图,看了一下博客,首先使用了matlab中的children,但是发现颜色没有变化。后来从另外的博客中发现,matalb2014以后的版本这个功能不能用了,what?只能用bar。好吧,用bar一遍一遍写,这里做个取巧的方式哈。mydata=[0.5,1.5,2.5,3.5,4.5];figure(1)holdonfori=1:length(mydata)h=bar(i,mydata.

    2022年10月10日
    2
  • java中executeQuery()方法

    java中executeQuery()方法介绍使用 JDBC 连接数据库需要 4 步 executeQuery 方法是第四步执行查询 要用 statement 类的 executeQuery 方法来下达 select 指令以查询数据库 executeQuery 方法会把数据库响应的查询结果存放在 ResultSet 类对象中供我们使用 举例如下 Stringstr9 selectsnofro

    2025年7月11日
    7

发表回复

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

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