spring in action 学习笔记三:对spring 容器的理解,以及如何利用AnnotationConfigApplicationContext这个容器创建对象

一:spring的容器就是bean所居住的地点,这个居民点有很多的bean,有外来的bean(相当于创建了一个bean),有出去谋生的(相当于消亡了一个bean),他们之间都有某种联系

(bean与bean之间的联系spring通过wiring 来实现)。而这个居民点在spring中就是BeanFactory这个接口,至于ClassPathXmlApplicationContext、AnnotationConfigApplicationContext、 AnnotationConfigWebApplicationContext、FileSystemApplicationContext等这些我们经常用的容器要么是BeanFactory的实现类,要么是BeanFactory的子类。

如下帮助文档所示:

 org.springframework.beans.factory
Interface BeanFactory
All Known Subinterfaces:
ApplicationContext, AutowireCapableBeanFactory, ConfigurableApplicationContext, ConfigurableBeanFactory, ConfigurableListableBeanFactory, ConfigurablePortletApplicationContext, ConfigurableWebApplicationContext, HierarchicalBeanFactory, ListableBeanFactory, WebApplicationContext
All Known Implementing Classes:
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配置的方式)

他的核心演示代码如下:

 package com.qls.test;

 import com.qls.impl.BraveKnight;
import com.qls.impl.SlayDragonQuest;
import com.qls.inter.Knight;
import com.qls.inter.Query;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* Created by ${秦林森} on 2017/6/6.
*/ /**
* @Bean indicated that a method produced a bean to be managed by the spring container;
* @Bean indicated that a method produced a bean to be managed by the spring container;
* @Bean indicated that a method produced a bean to be managed by the spring container.
*/
@Configuration
public class KnightConfig {
@Bean(name = "knight")
public Knight knight(){
return new BraveKnight(quest());
}
@Bean(name = "quest")
public Query quest(){
return new SlayDragonQuest(System.out);
}
}
 package com.qls.test;

 import com.qls.inter.Knight;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* Created by ${秦林森} on 2017/6/6.
*/
public class Test4 {
public static void main(String[] args) {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(KnightConfig.class);
Knight knight = ac.getBean(Knight.class);
knight.embarkOnQuest();
}
}
上一篇:多线程总结之旅(1):线程VS进程


下一篇:spring in action 学习笔记四:bean的生命周期