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


相关推荐

  • 文件共享服务 FTP,NFS 和 Samba

    文件共享服务 FTP,NFS 和 Samba

    2022年2月21日
    30
  • 图片url地址的生成获取方法

    图片url地址的生成获取方法图片url地址的生成获取方法  在写博客插入图片时,许多时候需要提供图片的url地址。作为菜鸡的我,自然是一脸懵逼。那么什么是所谓的url地址呢?又该如何获取图片的url地址呢?  首先来看一下度娘对url地址的解释:url是统一资源定位符,对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址。互联网上的每个文件都有一个唯一的url,它包含的信息指出文件的位置以及浏览器应该怎么处理它。  简单来说,url地址是是用来定位、访问网上资源用的。常见的网址也属于url地

    2022年9月22日
    0
  • 在我一生中最猥琐的时候遇见你(1)

    在我一生中最猥琐的时候遇见你(1)【书名】在我一生最猥琐的时候遇见你【作者】无良某鸡【文案】当我还是一个光屁股天使的时候,有一天,我在凡间晃悠。  突然我看到前面那个头上长着红色山羊角的男人从他的黑色斗篷里落下了一个东西。  “嗨,先生,”我朝他喊,“您掉东西了!”  那人不仅没有回头,反而越走越快,转眼就消失在街角。  我走上前去捡起落在地上的东西,那是一本小说.  小说的封面,一男一女紧密相拥着。  …

    2022年6月2日
    32
  • 计算机三级网络技术大题暴力做题法

    计算机三级网络技术大题暴力做题法

    2021年9月28日
    51
  • git免密码提交_git设置全局账号密码

    git免密码提交_git设置全局账号密码1.安装gityuminstallgit2.创建一个用户供git使用useraddgit#创建了一个名为git的用户passwordgit#设置密码3.打开RSA认证打开三项配置vim/etc/ssh/sshd_configRSAAuthenticationyesPubkeyA…

    2022年10月5日
    0
  • 多进程 python_python课程

    多进程 python_python课程进程前置知识点进程:一个程序运行起来后,代码+用到的资源称之为进程,它是操作系统分配资源的基本单元。并发:指的是任务数多余cpu核数,通过操作系统的各种任务调度算法,实现用多个任务“一起”执行

    2022年7月28日
    3

发表回复

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

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