1. 程式人生 > >spring BeanFactory和ApplicatContext

spring BeanFactory和ApplicatContext

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); }
View Code

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;
}
View Code

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>
View Code
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...");
    }
}
View Code