當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
spring预加载与懒加载_通过Spring将继承树加载到List中
生活随笔
收集整理的這篇文章主要介紹了
spring预加载与懒加载_通过Spring将继承树加载到List中
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
spring預(yù)加載與懶加載
我注意到有趣的Spring功能。 我的一位同事使用它將Spring Bean的整個(gè)繼承樹加載到列表中。 在學(xué)習(xí)Spring文檔時(shí)錯(cuò)過(guò)了這一點(diǎn)。
讓我們來(lái)看看Spring bean的繼承樹:
下面的代碼片段是通過(guò)構(gòu)造函數(shù)注入將此豆樹加載到列表中的:
@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表達(dá)式將已加載的bean輸出到控制臺(tái)。 這些天來(lái),您會(huì)發(fā)現(xiàn)很多有關(guān)此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();} }控制臺(tái)輸出:
PolarBear [] Wolf [] Animal [] Grizzly [] Bear []- 有時(shí)此功能可能很方便。 這個(gè)簡(jiǎn)短示例的源代碼在Github上 。
翻譯自: https://www.javacodegeeks.com/2014/05/load-inheritance-tree-into-list-by-spring.html
spring預(yù)加載與懶加載
總結(jié)
以上是生活随笔為你收集整理的spring预加载与懒加载_通过Spring将继承树加载到List中的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 国产操作系统崛起的机会国产操作系统崛起的
- 下一篇: 想玩红警却不知道怎么下载如何在电脑上下载