栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在春天按顺序实例化bean?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在春天按顺序实例化bean?

@Order
javadoc

注意:仅基于 特定类型的组件才
支持基于注释的排序,例如,基于注释的AspectJ方面。另一方面,Spring容器内的排序策略通常基于Ordered接口,以便允许对每个实例进行编程配置。

因此我猜想Spring

@Order()
在创建bean时不会遵循。

但是,如果您只想填充集合,那么这对您来说已经足够了:

import com.google.common.collect.Multimap;import com.google.common.collect.MultimapBuilder;import org.apache.commons.lang3.tuple.Pair;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.stereotype.Component;import java.lang.annotation.*;import java.util.ArrayList;import java.util.Collection;import java.util.List;@Configurationpublic class OrderingOfInstantiation {    public static void main(String[] args) {        new AnnotationConfigApplicationContext(OrderingOfInstantiation.class);    }    @Component    @CollectionOrder(collection = "myBeans", order = 1)    public static class MyBean1 {{        System.out.println(getClass().getSimpleName());    }}    @Component    @CollectionOrder(collection = "myBeans", order = 2)    public static class MyBean2 {{        System.out.println(getClass().getSimpleName());    }}    @Configuration    public static class CollectionsConfig {        @Bean        List<Object> myBeans() { return new ArrayList<>();        }    }    // PopulateConfig will populate all collections beans    @Configuration    public static class PopulateConfig implements ApplicationContextAware {        @SuppressWarnings("unchecked")        @Override        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { Multimap<String, Object> beansMap = MultimapBuilder.hashKeys().arrayListValues().build(); // get all beans applicationContext.getBeansWithAnnotation(CollectionOrder.class)         .values().stream()         // get CollectionOrder annotation         .map(bean -> Pair.of(bean, bean.getClass().getAnnotation(CollectionOrder.class)))         // sort by order         .sorted((p1, p2) -> p1.getRight().order() - p2.getRight().order())         // add to multimap         .forEach(pair -> beansMap.put(pair.getRight().collection(), pair.getLeft())); // and add beans to collections beansMap.asMap().entrySet().forEach(entry -> {     Collection collection = applicationContext.getBean(entry.getKey(), Collection.class);     collection.addAll(entry.getValue());     // debug     System.out.println(entry.getKey() + ":");     collection.stream()  .map(bean -> bean.getClass().getSimpleName())  .forEach(System.out::println); });        }    }    @Retention(RetentionPolicy.RUNTIME)    @Target({ElementType.TYPE})    @documented    public @interface CollectionOrder {        int order() default 0;        String collection();    }}

它不会更改实例化顺序,但是您将获得有序集合。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/446680.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号