org.springframework.beans.factory.HierarchicalBeanFactory Java Examples
The following examples show how to use
org.springframework.beans.factory.HierarchicalBeanFactory.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpringUtils.java From onetwo with Apache License 2.0 | 6 votes |
public static Map<String, Object> beansOfAnnotationIncludingAncestors(ListableBeanFactory lbf, Class<? extends Annotation> annotationType) throws BeansException { Assert.notNull(lbf, "ListableBeanFactory must not be null"); Map<String, Object> result = new LinkedHashMap<String, Object>(); result.putAll(lbf.getBeansWithAnnotation(annotationType)); if (lbf instanceof HierarchicalBeanFactory) { HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf; if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { Map<String, Object> parentResult = beansOfAnnotationIncludingAncestors( (ListableBeanFactory) hbf.getParentBeanFactory(), annotationType); for (Map.Entry<String, Object> entry : parentResult.entrySet()) { String beanName = entry.getKey(); if (!result.containsKey(beanName) && !hbf.containsLocalBean(beanName)) { result.put(beanName, entry.getValue()); } } } } return result; }
Example #2
Source File: HierarchicalDependencyLookupDemo.java From geekbang-lessons with Apache License 2.0 | 5 votes |
public static void main(String[] args) { // 创建 BeanFactory 容器 AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); // 将当前类 ObjectProviderDemo 作为配置类(Configuration Class) applicationContext.register(ObjectProviderDemo.class); // 1. 获取 HierarchicalBeanFactory <- ConfigurableBeanFactory <- ConfigurableListableBeanFactory ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory(); // System.out.println("当前 BeanFactory 的 Parent BeanFactory : " + beanFactory.getParentBeanFactory()); // 2. 设置 Parent BeanFactory HierarchicalBeanFactory parentBeanFactory = createParentBeanFactory(); beanFactory.setParentBeanFactory(parentBeanFactory); // System.out.println("当前 BeanFactory 的 Parent BeanFactory : " + beanFactory.getParentBeanFactory()); displayContainsLocalBean(beanFactory, "user"); displayContainsLocalBean(parentBeanFactory, "user"); displayContainsBean(beanFactory, "user"); displayContainsBean(parentBeanFactory, "user"); // 启动应用上下文 applicationContext.refresh(); // 关闭应用上下文 applicationContext.close(); }
Example #3
Source File: HierarchicalDependencyLookupDemo.java From geekbang-lessons with Apache License 2.0 | 5 votes |
private static boolean containsBean(HierarchicalBeanFactory beanFactory, String beanName) { BeanFactory parentBeanFactory = beanFactory.getParentBeanFactory(); if (parentBeanFactory instanceof HierarchicalBeanFactory) { HierarchicalBeanFactory parentHierarchicalBeanFactory = HierarchicalBeanFactory.class.cast(parentBeanFactory); if (containsBean(parentHierarchicalBeanFactory, beanName)) { return true; } } return beanFactory.containsLocalBean(beanName); }
Example #4
Source File: ModuleDirectorImpl.java From yes-cart with Apache License 2.0 | 5 votes |
private <T> List<T> beansOfTypeIncludingAncestors(ListableBeanFactory lbf, Class<T> clazz) { final List<T> result = new LinkedList<>(); result.addAll(lbf.getBeansOfType(clazz).values()); if (lbf instanceof HierarchicalBeanFactory) { HierarchicalBeanFactory hbf = (HierarchicalBeanFactory) lbf; if (hbf.getParentBeanFactory() instanceof ListableBeanFactory) { List<T> parentResult = beansOfTypeIncludingAncestors( (ListableBeanFactory) hbf.getParentBeanFactory(), clazz); result.addAll(parentResult); } } return result; }
Example #5
Source File: HierarchicalDependencyLookupDemo.java From geekbang-lessons with Apache License 2.0 | 4 votes |
private static void displayContainsBean(HierarchicalBeanFactory beanFactory, String beanName) { System.out.printf("当前 BeanFactory[%s] 是否包含 Bean[name : %s] : %s\n", beanFactory, beanName, containsBean(beanFactory, beanName)); }
Example #6
Source File: HierarchicalDependencyLookupDemo.java From geekbang-lessons with Apache License 2.0 | 4 votes |
private static void displayContainsLocalBean(HierarchicalBeanFactory beanFactory, String beanName) { System.out.printf("当前 BeanFactory[%s] 是否包含 Local Bean[name : %s] : %s\n", beanFactory, beanName, beanFactory.containsLocalBean(beanName)); }