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值得注意的是这里property是property配置文件里的property
Use
${key_name}
To Read Property Value In Properties File.
- Get the value of the specified key in the external properties file.
- Can be used in XML configuration file or in java file with
@Value
annotation.- 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<<