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

2.SpringEL bean引用实例

SpringEL bean引用实例

文章目录

  • SpringEL bean引用实例
    • 介绍
    • Spring EL以注解的形式
    • Spring EL以XML的形式

介绍

在Spring EL,可以使用点(.)符号嵌套属性参考一个bean。例如,“bean.property_name”

public class Customer {@Value("#{addressBean.country}")private String country;
}

在上面的代码片段,它从"addressBean""bean注入了"country"属性到现在的"customer"类的"country"属性的值

Spring EL以注解的形式

使用 SpEL 引用一个bean,bean属性也它的方法

package com.yiibai.core;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component("customerBean")
public class Customer {@Value("#{addressBean}")private Address address;@Value("#{addressBean.country}")private String country;@Value("#{addressBean.getFullAddress('yiibai')}")private String fullAddress;//getter and setter methods@Overridepublic String toString() {return "Customer [address=" + address + "\n, country=" + country+ "\n, fullAddress=" + fullAddress + "]";}}
package com.yiibai.core;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component("addressBean")
public class Address {@Value("GaoDeng, QiongShang")private String street;@Value("571100")private int postcode;@Value("CN")private String country;public String getFullAddress(String prefix) {return prefix + " : " + street + " " + postcode + " " + country;}//getter and setter methodspublic void setCountry(String country) {this.country = country;}@Overridepublic String toString() {return "Address [street=" + street + ", postcode=" + postcode+ ", country=" + country + "]";}}

执行结果

Customer obj = (Customer) context.getBean("customerBean");
System.out.println(obj);

输出结果

Customer [address=Address [street=GaoDeng, QiongShang, postcode=571100, country=CN]
, country=CN
, fullAddress=yiibai : GaoDeng, QiongShang 571100 CN]

Spring EL以XML的形式

<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="customerBean" class="com.yiibai.core.Customer"><property name="address" value="#{addressBean}" /><property name="country" value="#{addressBean.country}" /><property name="fullAddress" value="#{addressBean.getFullAddress('yiibai')}" /></bean><bean id="addressBean" class="com.yiibai.core.Address"><property name="street" value="GaoDeng, QiongShang" /><property name="postcode" value="571100" /><property name="country" value="CN" /></bean></beans>
http://www.lryc.cn/news/168502.html

相关文章:

  • 通用商城项目(下)之——Nginx的安装及使用
  • 滑动时间窗口的思想和实现,环形数组,golang
  • SpringBoot 使用异步方法
  • Django框架学习大纲
  • 基于matlab实现的电力系统稳定性分析摆幅曲线代码
  • mybatis基本构成mybatis与hibernate的区别添加mybatis支持
  • c++23中的新功能之十四输入输出指针
  • Day42:网易云项目,路由进阶
  • Open3D(C++) 三维点云边界提取
  • AUTOSAR汽车电子嵌入式编程精讲300篇-经典 AUTOSAR 安全防御能力的分析及改善
  • LeetCode 1584. 连接所有点的最小费用【最小生成树】
  • 超简单,几行js代码就实现一个 vue3 的数字滚动效果!
  • 两阶段鲁棒优化matlab实现——CCG和benders
  • 二进制安全虚拟机Protostar靶场(4)写入shellcode,基础知识讲解 Stack Five
  • 【Flink实战】玩转Flink里面核心的Source Operator实战
  • [2023-09-12]Oracle备库查询报ORA-01187
  • leetcode 16.最接近的三数之和
  • antd table 自定义排序图标
  • 第十九章、【Linux】开机流程、模块管理与Loader
  • GMAC PHY介绍
  • 华为OD机考算法题:最远足迹
  • QScrollBar滚动条、QSlider滑块、 QDial表盘
  • Prometheus+Grafana可视化监控【MySQL状态】
  • 五,编译定制rom并刷机实现硬改(二)
  • Modbus协议详解3:数据帧格式 - RTU帧 ASCII帧的区别
  • 认识数据分析
  • Learn Prompt-ChatGPT 精选案例:写作博客
  • 《确保安全:PostgreSQL安全配置与最佳实践》
  • Unity中Shader抓取屏幕并实现扭曲效果
  • 深浅拷贝详解