當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
通过Spring将继承树加载到List中
生活随笔
收集整理的這篇文章主要介紹了
通过Spring将继承树加载到List中
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我注意到有趣的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将继承树加载到List中的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 石材宝电脑版(中国石材网下载)
- 下一篇: 电脑emoji表情代码(怎么显示emoj