當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
spring预加载与懒加载_通过Spring将继承树加载到List中
生活随笔
收集整理的這篇文章主要介紹了
spring预加载与懒加载_通过Spring将继承树加载到List中
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
spring預加載與懶加載
我注意到有趣的Spring功能。 我的一位同事使用它將Spring Bean的整個繼承樹加載到列表中。 在學習Spring文檔時錯過了這一點。
讓我們來看看Spring bean的繼承樹:
下面的代碼片段是通過構造函數注入將此豆樹加載到列表中的:
@Component public class Nature {List<Animal> animals;@Autowiredpublic Nature(List<Animal> animals) {this.animals = animals;}public void showAnimals() {animals.forEach(animal -> System.out.println(animal));} }showAnimals方法使用Java 8 lambda表達式將已加載的bean輸出到控制臺。 這些天來,您會發現很多有關此Java 8新功能的文章。
Spring上下文由此主類加載:
public class Main {public static void main(String[] args) {AnnotationConfigApplicationContext context =new AnnotationConfigApplicationContext(SpringContext.class);Nature nature = context.getBean(Nature.class);nature.showAnimals();} }控制臺輸出:
PolarBear [] Wolf [] Animal [] Grizzly [] Bear []- 有時此功能可能很方便。 這個簡短示例的源代碼在Github上 。
翻譯自: https://www.javacodegeeks.com/2014/05/load-inheritance-tree-into-list-by-spring.html
spring預加載與懶加載
總結
以上是生活随笔為你收集整理的spring预加载与懒加载_通过Spring将继承树加载到List中的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 国产操作系统崛起的机会国产操作系统崛起的
- 下一篇: rome rss_RSS阅读器使用:RO