Spring IOC学习的自我总结(1)
声明:现在已经有很多有关Spring的文章,无论是转载还是原创。这一系列文章只是我对Spring学习的一些感悟。如果错误,不吝赐教。
首先,在Spring IOC容器中,有两个相关接口一个是BeanFactory,另外一个ApplicationContext。BeanFactory 提供一个进入Spring Bean容器的根途径,也就是说BeanFactory是Spring Ioc容器中的最根本的容器。BeanFactory中一共就10个方法,分别是:
// 常量,用来得到一个FactoryBean,也就是产生以及装饰某个Bean的工厂实例,一个修饰符而已static String FACTORY_BEAN_PREFIX = "&" boolean containsBean(String name)String[] getAliases(String name)<T> T getBean(Class<T> requiredType)Object getBean(String name)<T> T getBean(String name, Class<T> requiredType)Object getBean(String name, Object... args)Class<?> getType(String name)boolean isPrototype(String name)boolean isSingleton(String name)boolean isTypeMatch(String name, Class<?> targetType)
// 返回本地Bean工厂是否包含给定名字的Bean,忽略bean在祖先上下文中的定义boolean containsLocalBean(String name)// 返回父类bean工厂实例,如果没有返回空BeanFactory getParentBeanFactory()
// 单例的标示符,可以通过registerScope()方法设置. static String SCOPE_SINGLETON // 原型的标示符,可以通过registerScope()方法设置. static String SCOPE_PROTOTYPE void addBeanPostProcessor(BeanPostProcessor beanPostProcessor)// 为嵌入的值添加一个字符串解析器,比如annotation 属性void addEmbeddedValueResolver(StringValueResolver valueResolver)// 为所有bean的创建过程添加一个PropertyEditorRegistrarvoid addPropertyEditorRegistrar(PropertyEditorRegistrar registrar)// 从一个给定的其他工厂中复制所有关联的配置void copyConfigurationFrom(ConfigurableBeanFactory otherFactory)// 用这个工厂已经注册的定制的编辑器,初始化给定PropertyEditorRegistry void copyRegisteredEditorsTo(PropertyEditorRegistry registry)void destroyBean(String beanName, Object beanInstance)// 如果有这个beam,就在当前目标范围中销毁指定范围的bean,void destroyScopedBean(String beanName)// 销毁这个工厂下面的所有单例bean,包括已经注册为废弃的内部beanvoid destroySingletons()// Provides a security access control context relevant to this factory.AccessControlContext getAccessControlContext()ClassLoader getBeanClassLoader()// Return the resolution strategy for expressions in bean definition values.BeanExpressionResolver getBeanExpressionResolver()// Return the current number of registered BeanPostProcessors, if any.int getBeanPostProcessorCount()// Return the associated ConversionService, if any.ConversionService getConversionService()// Return the names of all beans that the specified bean depends on, if any.String[] getDependenciesForBean(String beanName)// Return the names of all beans which depend on the specified bean, if any.String[] getDependentBeans(String beanName)// Return a merged BeanDefinition for the given bean name, merging a child bean definition with its parent if necessary.BeanDefinition getMergedBeanDefinition(String beanName)// Return the Scope implementation for the given scope name, if any.Scope getRegisteredScope(String scopeName)// Return the names of all currently registered scopes.String[] getRegisteredScopeNames()// Return the temporary ClassLoader to use for type matching purposes, if any.ClassLoader getTempClassLoader()// Obtain a type converter as used by this BeanFactory.TypeConverter getTypeConverter()// Return whether to cache bean metadata such as given bean definitions (in merged fashion) and resolved bean classes.boolean isCacheBeanMetadata()// Determine whether the specified bean is currently in creation.boolean isCurrentlyInCreation(String beanName)// Determine whether the bean with the given name is a FactoryBean.boolean isFactoryBean(String name)// Given a bean name, create an alias. void registerAlias(String beanName, String alias)// Register the given custom property editor for all properties of the given type.void registerCustomEditor(Class<?> requiredType, Class<? extends PropertyEditor> propertyEditorClass)// Register a dependent bean for the given bean, to be destroyed before the given bean is destroyed. void registerDependentBean(String beanName, String dependentBeanName)// Register the given scope, backed by the given Scope implementation. void registerScope(String scopeName, Scope scope)// Resolve all alias target names and aliases registered in this factory, applying the given StringValueResolver to them.void resolveAliases(StringValueResolver valueResolver)// Resolve the given embedded value, e.g.String resolveEmbeddedValue(String value)// Set the class loader to use for loading bean classes.void setBeanClassLoader(ClassLoader beanClassLoader)// Specify the resolution strategy for expressions in bean definition values.void setBeanExpressionResolver(BeanExpressionResolver resolver)// Set whether to cache bean metadata such as given bean definitions (in merged fashion) and resolved bean classes.void setCacheBeanMetadata(boolean cacheBeanMetadata)// Specify a Spring 3.0 ConversionService to use for converting property values, as an alternative to JavaBeans PropertyEditors.void setConversionService(ConversionService conversionService)// Explicitly control in-creation status of the specified bean. void setCurrentlyInCreation(String beanName, boolean inCreation)// Set the parent of this bean factory.void setParentBeanFactory(BeanFactory parentBeanFactory)// Specify a temporary ClassLoader to use for type matching purposes. void setTempClassLoader(ClassLoader tempClassLoader)// Set a custom type converter that this BeanFactory should use for converting bean property values, constructor argument values, etc. void setTypeConverter(TypeConverter typeConverter)
// Check if this bean factory contains a bean definition with the given name.boolean containsBeanDefinition(String beanName)// Find a Annotation of annotationType on the specified bean, traversing its interfaces and super classes if no annotation can be found on the given class itself.<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)// Return the number of beans defined in the factory.int getBeanDefinitionCount()// Return the names of all beans defined in this factory.String[] getBeanDefinitionNames()// Return the names of beans matching the given type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.String[] getBeanNamesForType(Class<?> type)// Return the names of beans matching the given type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.String[] getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit)// Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.<T> Map<String,T> getBeansOfType(Class<T> type)// Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.<T> Map<String,T> getBeansOfType(Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)// Find all beans whose Class has the supplied Annotation type.Map<String,Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType)
// Deprecated. as of Spring 3.0: If you are using mixed autowiring strategies, prefer annotation-based autowiring for clearer demarcation of autowiring needs.static int AUTOWIRE_AUTODETECT// Constant that indicates autowiring bean properties by name (applying to all bean property setters).static int AUTOWIRE_BY_NAME// Constant that indicates autowiring bean properties by type (applying to all bean property setters). static int AUTOWIRE_BY_TYPE// Constant that indicates autowiring the greediest constructor that can be satisfied (involves resolving the appropriate constructor).static int AUTOWIRE_CONSTRUCTOR // Constant that indicates no externally defined autowiring.static int AUTOWIRE_NO// Apply BeanPostProcessors to the given existing bean instance, invoking their postProcessAfterInitialization methods.Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)// Apply BeanPostProcessors to the given existing bean instance, invoking their postProcessBeforeInitialization methods.Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)// Apply the property values of the bean definition with the given name to the given bean instance.void applyBeanPropertyValues(Object existingBean, String beanName)// Instantiate a new bean instance of the given class with the specified autowire strategy.Object autowire(Class beanClass, int autowireMode, boolean dependencyCheck)// Populate the given bean instance through applying after-instantiation callbacks and bean property post-processing (e.g.void autowireBean(Object existingBean)// Autowire the bean properties of the given bean instance by name or type.void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck)// Configure the given raw bean: autowiring bean properties, applying bean property values, applying factory callbacks such as setBeanName and setBeanFactory, and also applying all bean post processors (including ones which might wrap the given raw bean).Object configureBean(Object existingBean, String beanName)// Fully create a new bean instance of the given class.<T> T createBean(Class<T> beanClass)// Fully create a new bean instance of the given class with the specified autowire strategyObject createBean(Class beanClass, int autowireMode, boolean dependencyCheck)// Initialize the given raw bean, applying factory callbacks such as setBeanName and setBeanFactory, also applying all bean post processors (including ones which might wrap the given raw bean).Object initializeBean(Object existingBean, String beanName)// Resolve the specified dependency against the beans defined in this factory.Object resolveDependency(DependencyDescriptor descriptor, String beanName)// Resolve the specified dependency against the beans defined in this factory.Object resolveDependency(DependencyDescriptor descriptor, String beanName, Set<String> autowiredBeanNames, TypeConverter typeConverter)
static String CONTEXT_ATTRIBUTES_BEAN_NAME// Name of the ServletContext/PortletContext attributes environment bean in the factory.static String CONTEXT_PARAMETERS_BEAN_NAME// Name of the ServletContext/PortletContext init-params environment bean in the factory.static String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE// Context attribute to bind root WebApplicationContext to on successful startup.static String SCOPE_APPLICATION// Scope identifier for the global web application scope: "application".static String SCOPE_GLOBAL_SESSION// Scope identifier for global session scope: "globalSession".static String SCOPE_REQUEST// Scope identifier for request scope: "request".static String SCOPE_SESSION// Scope identifier for session scope: "session".static String SERVLET_CONTEXT_BEAN_NAME// Name of the ServletContext environment bean in the factory.Fields inherited from interface org.springframework.beans.factory.BeanFactoryFACTORY_BEAN_PREFIXFields inherited from interface org.springframework.core.io.support.ResourcePatternResolver CLASSPATH_ALL_URL_PREFIX Fields inherited from interface org.springframework.core.io.ResourceLoader CLASSPATH_URL_PREFIX
static String CONFIG_LOCATION_DELIMITERS// Any number of these characters are considered delimiters between multiple context config paths in a single String value.static String CONVERSION_SERVICE_BEAN_NAME// Name of the ConversionService bean in the factory.static String ENVIRONMENT_BEAN_NAME// Name of the Environment bean in the factory.static String LOAD_TIME_WEAVER_BEAN_NAME// Name of the LoadTimeWeaver bean in the factory.static String SYSTEM_ENVIRONMENT_BEAN_NAME// Name of the System environment bean in the factory.static String SYSTEM_PROPERTIES_BEAN_NAME// Name of the System properties bean in the factory.// Fields inherited from interface org.springframework.beans.factory.BeanFactoryFACTORY_BEAN_PREFIX// Fields inherited from interface org.springframework.core.io.support.ResourcePatternResolverCLASSPATH_ALL_URL_PREFIX // Fields inherited from interface org.springframework.core.io.ResourceLoaderCLASSPATH_URL_PREFIXvoid addApplicationListener(ApplicationListener<?> listener)// Add a new ApplicationListener that will be notified on context events such as context refresh and context shutdown.void addBeanFactoryPostProcessor(BeanFactoryPostProcessor beanFactoryPostProcessor)// Add a new BeanFactoryPostProcessor that will get applied to the internal bean factory of this application context on refresh, before any of the bean definitions get evaluated.void close()// Close this application context, releasing all resources and locks that the implementation might hold.ConfigurableListableBeanFactory getBeanFactory()// Return the internal bean factory of this application context.ConfigurableEnvironment getEnvironment()// Return the Environment for this application context in configurable form.boolean isActive()// Determine whether this application context is active, that is, whether it has been refreshed at least once and has not been closed yet.void refresh()// Load or refresh the persistent representation of the configuration, which might an XML file, properties file, or relational database schema.void registerShutdownHook()// Register a shutdown hook with the JVM runtime, closing this context on JVM shutdown unless it has already been closed at that time.void setEnvironment(ConfigurableEnvironment environment)// Set the Environment for this application context.void setId(String id)// Set the unique id of this application context.void setParent(ApplicationContext parent)// Set the parent of this application context.