记录我对BeanFactor接口的简单的学习.

BeanFactory我感觉就是管理bean用的容器,持有一堆的bean,你可以get各种bean.然后也提供一些bean相关的功能比如别名呀之类的..

 

结构

Spring 学习记录5 BeanFactory

我觉得核心功能都写在了3个子接口里面了.

小实验:

 1     /**
 2      * 测试BeanFactory
 3      */
 4     @Test
 5     public void testBeanFactory() {
 6         BeanFactory beanFactory = this.applicationContext;
 7         System.out.println(beanFactory.getBean("myBeanFactoryTestBean")); // spring.BeanFactoryTest@484b2882
 8 
 9         System.out.println(beanFactory.getBean(Environment.class)); // StandardEnvironment {activeProfiles=[], defaultProfiles=[default], propertySources=[systemProperties,systemEnvironment,class path resource [test.properties]]}
10 
11         System.out.println(beanFactory.getBean("myFactoryBean-1")); // java.lang.Object@327ac9a7
12         System.out.println(beanFactory.getBean("myFactoryBean-1")); // java.lang.Object@5f8581f3
13         System.out.println(beanFactory.isSingleton("myFactoryBean-1")); // false
14         System.out.println(beanFactory.getBean("&myFactoryBean-1")); // spring.MyFactoryBean@1b4d0cd5 &开头的是FactoryBean
15         System.out.println(beanFactory.isSingleton("&myFactoryBean-1")); // true
16 
17         // System.out.println(beanFactory.getBean("not exists bean")); // 异常
18     }

 

 

HierarchicalBeanFactory

 1 /*
 2  * Copyright 2002-2012 the original author or authors.
 3  *
 4  * Licensed under the Apache License, Version 2.0 (the "License");
 5  * you may not use this file except in compliance with the License.
 6  * You may obtain a copy of the License at
 7  *
 8  *      http://www.apache.org/licenses/LICENSE-2.0
 9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.springframework.beans.factory;
18 
19 /**
20  * Sub-interface implemented by bean factories that can be part
21  * of a hierarchy.
22  *
23  * <p>The corresponding {@code setParentBeanFactory} method for bean
24  * factories that allow setting the parent in a configurable
25  * fashion can be found in the ConfigurableBeanFactory interface.
26  *
27  * @author Rod Johnson
28  * @author Juergen Hoeller
29  * @since 07.07.2003
30  * @see org.springframework.beans.factory.config.ConfigurableBeanFactory#setParentBeanFactory
31  */
32 public interface HierarchicalBeanFactory extends BeanFactory {
33 
34     /**
35      * Return the parent bean factory, or {@code null} if there is none.
36      */
37     BeanFactory getParentBeanFactory();
38 
39     /**
40      * Return whether the local bean factory contains a bean of the given name,
41      * ignoring beans defined in ancestor contexts.
42      * <p>This is an alternative to {@code containsBean}, ignoring a bean
43      * of the given name from an ancestor bean factory.
44      * @param name the name of the bean to query
45      * @return whether a bean with the given name is defined in the local factory
46      * @see BeanFactory#containsBean
47      */
48     boolean containsLocalBean(String name);
49 
50 }
View Code

相关文章:

  • 2022-12-23
  • 2021-10-17
  • 2021-10-23
  • 2021-10-24
  • 2021-05-28
  • 2021-09-04
  • 2021-10-23
  • 2021-06-17
猜你喜欢
  • 2021-12-07
  • 2021-12-20
  • 2022-12-23
  • 2021-11-16
  • 2021-10-13
相关资源
相似解决方案