一:spring的容器就是bean所居住的地点,这个居民点有很多的bean,有外来的bean(相当于创建了一个bean),有出去谋生的(相当于消亡了一个bean),他们之间都有某种联系
(bean与bean之间的联系spring通过wiring 来实现)。而这个居民点在spring中就是BeanFactory这个接口,至于ClassPathXmlApplicationContext、AnnotationConfigApplicationContext、 AnnotationConfigWebApplicationContext、FileSystemApplicationContext等这些我们经常用的容器要么是BeanFactory的实现类,要么是BeanFactory的子类。
如下帮助文档所示:
1 org.springframework.beans.factory 2 Interface BeanFactory 3 All Known Subinterfaces: 4 ApplicationContext, AutowireCapableBeanFactory, ConfigurableApplicationContext, ConfigurableBeanFactory, ConfigurableListableBeanFactory, ConfigurablePortletApplicationContext, ConfigurableWebApplicationContext, HierarchicalBeanFactory, ListableBeanFactory, WebApplicationContext 5 All Known Implementing Classes: 6 AbstractApplicationContext, AbstractAutowireCapableBeanFactory, AbstractBeanFactory, AbstractRefreshableApplicationContext, AbstractRefreshableConfigApplicationContext, AbstractRefreshablePortletApplicationContext, AbstractRefreshableWebApplicationContext, AbstractXmlApplicationContext, AnnotationConfigApplicationContext, AnnotationConfigWebApplicationContext, ClassPathXmlApplicationContext, DefaultListableBeanFactory, FileSystemXmlApplicationContext, GenericApplicationContext, GenericWebApplicationContext, GenericXmlApplicationContext, ResourceAdapterApplicationContext, SimpleJndiBeanFactory, StaticApplicationContext, StaticListableBeanFactory, StaticPortletApplicationContext, StaticWebApplicationContext, XmlBeanFactory, XmlPortletApplicationContext, XmlWebApplicationContext
而BeanFactory单从字面的意思也显而易见"Bean 工厂"、工厂自然是这些工人(Bean)生活居住的地点。在spring中的Bean就是javaBean,对应一个java类。
二:如何使用AnnotationConfigApplicationContext这个容器:
这个容器专门是为注解方式而设计的,(这里用的是注解,而不是xml配置的方式)
他的核心演示代码如下:
1 package com.qls.test; 2 3 import com.qls.impl.BraveKnight; 4 import com.qls.impl.SlayDragonQuest; 5 import com.qls.inter.Knight; 6 import com.qls.inter.Query; 7 import org.springframework.context.annotation.Bean; 8 import org.springframework.context.annotation.Configuration; 9 10 /** 11 * Created by ${秦林森} on 2017/6/6. 12 */ 13 14 /** 15 * @Bean indicated that a method produced a bean to be managed by the spring container; 16 * @Bean indicated that a method produced a bean to be managed by the spring container; 17 * @Bean indicated that a method produced a bean to be managed by the spring container. 18 */ 19 @Configuration 20 public class KnightConfig { 21 @Bean(name = "knight") 22 public Knight knight(){ 23 return new BraveKnight(quest()); 24 } 25 @Bean(name = "quest") 26 public Query quest(){ 27 return new SlayDragonQuest(System.out); 28 } 29 }
1 package com.qls.test; 2 3 import com.qls.inter.Knight; 4 import org.springframework.context.annotation.AnnotationConfigApplicationContext; 5 6 /** 7 * Created by ${秦林森} on 2017/6/6. 8 */ 9 public class Test4 { 10 public static void main(String[] args) { 11 AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(KnightConfig.class); 12 Knight knight = ac.getBean(Knight.class); 13 knight.embarkOnQuest(); 14 } 15 }