SpEL Bean引用

本文介绍了Spring中如何引用Bean,特别是SpEL(Spring Expression Language)的使用。讲解了$和#的区别,$用于读取property配置文件,#用于执行更复杂的SpEL表达式。通过示例展示了从XML配置文件中获取Bean的方法,并分享了在寻找XML资源时遇到的问题及解决方案。
摘要由CSDN通过智能技术生成

Bean的引用

声明

首先这个系列的本篇博客参照油管Spring Expression Language教学视频Referencing Beans而写。

开始

这集就是简单的看看怎么使用spEL来引用Bean

  • All Spring Expresions should be declared inside ${...} or #{...}
  • Any bean can be directly accessed using the id attribute of the bean.

2点应该很容易理解,我们来聊聊第1点吧。百度了一下几乎没有说这个的,所以Google🌏了一下,Spring Expression Language (SpEL) with @Value: dollar vs. hash ($ vs. #)中的高赞回答解释的非常清晰

${...} is the property placeholder syntax. It can only be used to dereference properties.

#{...} is SpEL syntax, which is far more capable and complex. It can also handle property placeholders, and a lot more besides.

Both are valid, and neither is deprecated.

#{...}非常强大可以做spEL的任何事情,而${...}则用于获得property值得注意的是这里propertyproperty配置文件里的property

Use ${key_name} To Read Property Value In Properties File.

  1. Get the value of the specified key in the external properties file.
  2. Can be used in XML configuration file or in java file with @Value annotation.
  3. Get spring-context environment variables in pom.xml properties definition.

更详细的区别和应用可继续阅读该博客Spring Expression Language Example $ vs #

首先创建几个类

Book

public class Book {
   
    private int bookId;
    private String bookName;

    public int getBookId() {
   
        return bookId;
    }

    public void setBookId(int bookId) {
   
        this.bookId = bookId;
    }

    public String getBookName() {
   
        return bookName;
    }

    public void setBookName(String bookName) {
   
        this.bookName = bookName;
    }
}

BookCollection

import java.util.List;

public class BookCollection {
   
    private List<Book> bookList;

    public List<<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值