标签:hide c51 contains ade iss try except color div
spring容器
BeanFactory提供了IoC的功能;ApplicatContext建立在BeanFactory的基础上
BeanFactory顶层接口
package org.springframework.beans.factory; import org.springframework.beans.BeansException; import org.springframework.core.ResolvableType; public interface BeanFactory { String FACTORY_BEAN_PREFIX = "&"; // getBean()方法从容器中返回Bean Object getBean(String var1) throws BeansException; <T> T getBean(String var1, Class<T> var2) throws BeansException; Object getBean(String var1, Object... var2) throws BeansException; <T> T getBean(Class<T> var1) throws BeansException; <T> T getBean(Class<T> var1, Object... var2) throws BeansException; // 容器里有没有一个Bean boolean containsBean(String var1); // 一个Bean是不是单例的 boolean isSingleton(String var1) throws NoSuchBeanDefinitionException; // 一个Bean是不是复例的 boolean isPrototype(String var1) throws NoSuchBeanDefinitionException; boolean isTypeMatch(String var1, ResolvableType var2) throws NoSuchBeanDefinitionException; boolean isTypeMatch(String var1, Class<?> var2) throws NoSuchBeanDefinitionException; Class<?> getType(String var1) throws NoSuchBeanDefinitionException; String[] getAliases(String var1); }
BeanFactory的子接口ListableBeanFactory
package org.springframework.beans.factory; import java.lang.annotation.Annotation; import java.util.Map; import org.springframework.beans.BeansException; import org.springframework.core.ResolvableType; public interface ListableBeanFactory extends BeanFactory { // 是否包含特定的Bean boolean containsBeanDefinition(String var1); // 查看Bean的数量 int getBeanDefinitionCount(); String[] getBeanDefinitionNames(); String[] getBeanNamesForType(ResolvableType var1); String[] getBeanNamesForType(Class<?> var1); String[] getBeanNamesForType(Class<?> var1, boolean var2, boolean var3); <T> Map<String, T> getBeansOfType(Class<T> var1) throws BeansException; <T> Map<String, T> getBeansOfType(Class<T> var1, boolean var2, boolean var3) throws BeansException; String[] getBeanNamesForAnnotation(Class<? extends Annotation> var1); Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> var1) throws BeansException; <A extends Annotation> A findAnnotationOnBean(String var1, Class<A> var2) throws NoSuchBeanDefinitionException; }
BeanFactory的子接口HierarchicalBeanFactory
通过这个接口Spring的IoC容器可以建立父子层级关联的容器体系,子容器可以访问父容器中的Bean,但父容器不能访问子容器中的Bean。
AutowireCapableBeanFactory接口
定义了自动装配
DefaultSingletonBeanRegistry接口
DefaultSingletonBeanRegistry里有一个addSingleton()方法,会把单例的Bean保存到一个ConcurrentHashMap
// 缓存到一个map里 private final Map<String, Object> singletonObjects = new ConcurrentHashMap(256); protected void addSingleton(String beanName, Object singletonObject) { Map var3 = this.singletonObjects; synchronized(this.singletonObjects) { // 添加单对象到map this.singletonObjects.put(beanName, singletonObject != null ? singletonObject : NULL_OBJECT); this.singletonFactories.remove(beanName); this.earlySingletonObjects.remove(beanName); this.registeredSingletons.add(beanName); } }
DefaultListableBeanFactory 实现类
这个类实现了上面所有的接口
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="tank" class="my.Tank"></bean> </beans>
package my;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import java.io.IOException;
public class MyTest {
public static void main(String[] args) throws IOException {
// 创建一个资源加载器
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
// 使用ClassPathResource加载配置文件
Resource resource = resolver.getResource("classpath:my.xml");
// 创建一个BeanFactory的对象
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(resource);
// 单例的Bean已经缓存到容器中
Tank tank = (Tank)factory.getBean(Tank.class);
tank.run();
}
}
class Tank {
public void run() {
System.out.println("run...");
}
}
spring BeanFactory和ApplicatContext
标签:hide c51 contains ade iss try except color div
原文地址:https://www.cnblogs.com/Mike_Chang/p/10261122.html