spring 之浅析1
? ? 我们知道,在spring中,会通过一个listener来来监听事件,
? ?org.springframework.web.context.ContextLoadListener。
? ? 因为在webapp初始化之后,root web application context 就能够当做ServletContext作用于整个webapp,它能够被重新获得,通过spring中的方法,也就是说spring能够得到获得ServletContext,比如Servlet,Filter,JSP,
? ?如下:
? ?WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
?
? ?The core ?bean factory
? ?spring有个作用就是防止大量的工厂和单例模式的出现,bean factory提供一个统一的借口来操作所有的bean,不管是粗劣的还是设计优良的,让所有的子系统意识不到spring的存在。
? ? 顶级接口beanfactory中的方法:
? ? getBean(String name)
? ? getBean(String name,Class requiredType)
? ? containsBean(String name)
? ? isSingleton(String name)
? ? getAliases(String name)
调用getBean方法,默认是返回同一个示例,如果是prototype的bean ,就会返回不同的示例。其他方法,看文字就能知道意思了。
在各个beanfactory中,如果子系统没有找到需要的就会到parent beanfactory中去找,直到root factory,基于此,各个子接口可以提供各种各样丰富的拓展,比如说:
listablebeanFactory 支持列出所有factory中的bean的名字,借口如下:
? int getBeanDefinitionCount();
? String[] getBeanDefinitionNames();
? String[] getBeanDefinitionNames(Class type);
? boolean containsBeanDefinition(String name);
?
?如果一个类所依赖的类,只有在runtime的时候才知道到底依赖谁,这个接口就可以用上。
?
beanfactory是一个中心,他把各种各样的应用都集成到spring中去,DAOs,business object,或者controllers,如果想独立出来用spring的话,spring发行了一个叫做 spring-core.jar的jar包能够嵌入到任何系统中去。
?
Resource resource = new ClassPathResource("style.xml");
ListableBeanfactoy bf = new XmlBeanFactory(resource);
他的一个可替代的方案:
ListableBeanfactoy? bf = new DefaultListableBeanfactoy();
XmlbeanDefinitionReader reader = new?XmlbeanDefinitionReader(bf);
Resource resource = new ClassPathResource("style.xml");
reader.loadBeanDefinitions(resource);
这就是spring bean factory 配置的灵活性,以上所有原则可以运用在任何场所。
?
To be continue........