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


相关推荐

  • sed与awk的使用

    sed与awk的使用

    2021年5月31日
    92
  • vue双向数据绑定的原理「建议收藏」

    vue双向数据绑定的原理「建议收藏」有关双向数据绑定的原理最近两次面试的时候,被问到了vue中双向数据绑定的原理,因为初学不精,只是使用而没有深入研究,所以答不出来。之后就在网上查找了别人写的博客,学习一下。下面是博客园一篇博客,以及MDN上讲解Object.defineProperty()方法的地址。文章链接:vue的双向绑定原理及实现Mozilla开发者服务:Object.defineProperty…

    2022年10月17日
    2
  • linux 动态库加载_linux默认动态库加载路径

    linux 动态库加载_linux默认动态库加载路径    当我们在linux系统引用动态库时,经常会遇到一个问题,加入我们需要的动态库没有在系统的默认目录下,我们编译时使用-L指定了动态库的路径,编译时没有问题,但是执行调用该动态库的可执行文件时,却提示找不到动态库。下面我将以一个简单的例子来,复现这个问题和解决这个问题的办法。    假设我们使用tensorflow,把需要调用的libtensorflow.so和libtens…

    2022年9月28日
    3
  • NPS 是什么?[通俗易懂]

    NPS 是什么?[通俗易懂]NPS是NetPromoterScore的缩写。它是一种用来测定顾客对公司忠诚度的工具。NPS值得测定通常用发出NPS调研问卷的方式获得。例如:请问经过这次服务,您向朋友推荐我们服务的意向如何?10分非常想推荐,1分非常不推荐,共10分,请为我们打分,谢谢!根据顾客所反馈的分值,将顾客分为三类。推荐者:10分到9分被动者:8分到7分贬损者:6分到0分顾客能向能向朋

    2022年6月9日
    52
  • Pytest和Allure测试框架-超详细版+实战

    Pytest和Allure测试框架-超详细版+实战文章目录一:pytest的介绍,安装,框架结构,执行方式1,特点2,安装3,pytest的框架结构4,执行方式二:Pytest-断言、跳过及运行1,Pytest-断言、跳过及运行2,mark中的skip(跳过)3,mark中的xfail(失败)4,使用自定义标记mark只执行部分用例5,文件名类名方法执行部分用例6,-k组合调用执行部分用例三,Pytest-fixture1. 如下:…

    2022年7月26日
    9
  • 对全连接层(fully connected layer)的通俗理解

    对全连接层(fully connected layer)的通俗理解我的机器学习教程「美团」算法工程师带你入门机器学习已经开始更新了,欢迎大家订阅~任何关于算法、编程、AI行业知识或博客内容的问题,可以随时扫码关注公众号「图灵的猫」,加入”学习小组“,沙雕博主在线答疑~此外,公众号内还有更多AI、算法、编程和大数据知识分享,以及免费的SSR节点和学习资料。其他平台(知乎/B站)也是同名「图灵的猫」,不要迷路哦~定义…

    2022年6月16日
    37

发表回复

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

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