当前位置: 首页 > news >正文

1.SpringEL初始

SpringEL初始

文章目录

  • SpringEL初始
    • 什么是SpringEL
    • Spring Beans
    • Spring EL以XML形式
    • Spring EL以注解形式
      • 启用自动组件扫描
    • 执行输出

什么是SpringEL

  • Spring EL与OGNL和JSF EL相似,计算评估或在bean创建时执行。此外,所有的Spring表达式都可以通过XML或注解
  • 我们将学习如何使用Spring表达式语言(SpEL),注入字符串,整数,Bean到属性,无论是在XML和注释

Spring Beans

两个简单Bean,后来利用 SpEL 注入值到属性,在 XML 和 注释。

public class Customer {private Item item;private String itemName;}
public class Item {private String name;private int qty;}

Spring EL以XML形式

  • 使用 SpEL关闭的#{ SpEL expression }括号,请参阅XML bean定义文件下面的例子
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><bean id="itemBean" class="com.yiibai.core.Item"><property name="name" value="itemA" /><property name="qty" value="10" /></bean><bean id="customerBean" class="com.yiibai.core.Customer"><property name="item" value="#{itemBean}" /><property name="itemName" value="#{itemBean.name}" /></bean></beans>
  1. #{itemBean} – 注入"itemBean"到"customerBean"Bean 的"item"属性。
  2. #{itemBean.name} – 注入"itemBean"的"name"属性到 “customerBean” bean的"itemname"属性。

Spring EL以注解形式

在注解使用使用SpEL,必须通过注解注册您的组件。如果注册bean在XML和Java类中定义@Value,该@Value将无法执行

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component("customerBean")
public class Customer {@Value("#{itemBean}")private Item item;@Value("#{itemBean.name}")private String itemName;//...}
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component("itemBean")
public class Item {@Value("itemA") //inject String directlyprivate String name;@Value("10") //inject interger directlyprivate int qty;public String getName() {return name;}//...
}

启用自动组件扫描

<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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:component-scan base-package="com.yiibai.core" /></beans>

在注解模式下,可以使用@Value定义Spring EL。在这种情况下,一个String和Integer值直接注入到“itemBean”,之后又注入“itemBean”到“customerBean”属性

执行输出

运行它,无论是使用 SpEL在XML 还是注释都显示了同样的结果:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Customer obj = (Customer) context.getBean("customerBean");System.out.println(obj);}
}

输出结果

Customer [item=Item [name=itemA, qty=10], itemName=itemA]
http://www.lryc.cn/news/169689.html

相关文章:

  • HTTP 状态码
  • ddtrace 系列篇之 dd-trace-java 项目编译
  • 华为aarch64架构的泰山服务器EulerOS 2.0 (SP8)系统离线安装saltstack3003.1实践
  • C#中的方法
  • 【Flowable】使用UEL整合Springboot从0到1(四)
  • WebGL 计算点光源下的漫反射光颜色
  • Java精品项目源码第61期垃圾分类科普平台(代号V061)
  • 【Unity3D】资源管理
  • 数据结构-----队列
  • postgresql教程
  • 1万6千多最好的背单词SQLITE\ACCESS数据库
  • springboot aop Aspectj 切面
  • Leetcode 2862. Maximum Element-Sum of a Complete Subset of Indices
  • 第一百四十七回 自定义组件一
  • MySQL 重复数据的处理
  • Java文字描边效果实现
  • 【Web_环境搭建_Python3_pip】pip的升级、安装、更新、卸载,以及pipupgrade和pip-review的基础使用
  • 农民朋友有福利啦!建行江门市分行“裕农通+农资结算”平台正式上线
  • super详解
  • GMS地下水数值模拟丨GMS各模块、三维地质模型构建及与MODFLOW耦合、地下水流动数值模拟及报告编制、地下水溶质运移模型、反应性溶质运移等
  • Redis 配置文件详解 - 持久化(RDB、AOF)
  • 在线Excel转JSON工具
  • Spring编程常见错误50例-Spring Bean依赖注入常见错误(下)
  • SpringBoot整合Canal实现MySQL与ES数据同步
  • Zookeeper 源码分析流程
  • 计数排序与基数排序
  • Mysql—表操作
  • SpringCloud——微服务
  • 深入理解Java单例模式和优化多线程任务处理
  • 已解决 Kotlin Error: Type mismatch: inferred type is String but Int was expected