- 浏览: 10213 次
- 性别:
- 来自: 北京
最新评论
声明:现在已经有很多有关Spring的文章,无论是转载还是原创。这一系列文章只是我对Spring学习的一些感悟。如果错误,不吝赐教。
首先,在Spring IOC容器中,有两个相关接口一个是BeanFactory,另外一个ApplicationContext。BeanFactory 提供一个进入Spring Bean容器的根途径,也就是说BeanFactory是Spring Ioc容器中的最根本的容器。BeanFactory中一共就10个方法,分别是:
BeanFactory有若干个实现类。Spring文档中给出了10个子接口。其实,可以分为几条路线
1:BeanFactory ->
HierarchicalBeanFactory(取得双亲IOC容器管理功能,也就是说取得父类BeanFactory接口类型)->
ConfigurableBeanFactory(带有配置功能的)
另外一条设计路线是:
BeanFactory->
ListableBeanFactory->
ApplicationContext(这个接口不仅继承了ListableBeanFactory,同时还继承了MessageResource、ResourceLoader、AppliactionEventPublisher等接口,强化了非常多的功能,可以说是BeanFactory的高级容器,后面我们用到的一些容器,如WebAppliactionContext和ConfigurableApplicationContext,都是继承这个接口)
第三条:
BeanFactory ->
AutowireCapableBeanFactory(Extension of the BeanFactory interface to be implemented by bean factories that are capable of autowiring, provided that they want to expose this functionality for existing bean instances.)
AppliactionContext->
WebApplicationContext
以及ConfigurableAppliactionContext
首先,在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)
BeanFactory有若干个实现类。Spring文档中给出了10个子接口。其实,可以分为几条路线
1:BeanFactory ->
HierarchicalBeanFactory(取得双亲IOC容器管理功能,也就是说取得父类BeanFactory接口类型)->
// 返回本地Bean工厂是否包含给定名字的Bean,忽略bean在祖先上下文中的定义 boolean containsLocalBean(String name) // 返回父类bean工厂实例,如果没有返回空 BeanFactory getParentBeanFactory()
ConfigurableBeanFactory(带有配置功能的)
// 单例的标示符,可以通过registerScope()方法设置. static String SCOPE_SINGLETON // 原型的标示符,可以通过registerScope()方法设置. static String SCOPE_PROTOTYPE void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) // 为嵌入的值添加一个字符串解析器,比如annotation 属性 void addEmbeddedValueResolver(StringValueResolver valueResolver) // 为所有bean的创建过程添加一个PropertyEditorRegistrar void 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,包括已经注册为废弃的内部bean void 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)
另外一条设计路线是:
BeanFactory->
ListableBeanFactory->
// 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)
ApplicationContext(这个接口不仅继承了ListableBeanFactory,同时还继承了MessageResource、ResourceLoader、AppliactionEventPublisher等接口,强化了非常多的功能,可以说是BeanFactory的高级容器,后面我们用到的一些容器,如WebAppliactionContext和ConfigurableApplicationContext,都是继承这个接口)
第三条:
BeanFactory ->
AutowireCapableBeanFactory(Extension of the BeanFactory interface to be implemented by bean factories that are capable of autowiring, provided that they want to expose this functionality for existing bean instances.)
// 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 strategy Object 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)
AppliactionContext->
WebApplicationContext
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.BeanFactory FACTORY_BEAN_PREFIX Fields 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
以及ConfigurableAppliactionContext
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.BeanFactory FACTORY_BEAN_PREFIX // Fields 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 void 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.
相关推荐
spring ioc指的是控制反转,IOC容器负责实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。交由Spring容器统一进行管理,从而实现松耦合
spring ioc容器初始化流程图 spring ioc容器依赖注入流程图 spring aop实现原理流程图
springIOC手写框架分析springIOC手写框架分析springIOC手写框架分析springIOC手写框架分析springIOC手写框架分析springIOC手写框架分析springIOC手写框架分析springIOC手写框架分析springIOC手写框架分析springIOC...
spring ioc模块手写demospring ioc模块手写demospring ioc模块手写demospring ioc模块手写demospring ioc模块手写demospring ioc模块手写demospring ioc模块手写demospring ioc模块手写demospring ioc模块手写demo...
SpringIoc示例代码,SpringIoc示例代码,SpringIoc示例代码,SpringIoc示例代码
spring Ioc容器配置 IOC容器数据源配置 <!-- 配置数据源 --> destroy-method="close"> <value>org.gjt.mm.mysql.Driver <value>jdbc:mysql://localhost:3306/demo <value>root ...
Spring核心学习IOC部分:从最简单的BeanFactory开始一步步完善类似Spring的功能
Spring ioc Spring ioc
spring IOC学习源码
spring ioc思维导图,让自己快速、详尽地了解spring ioc知识。
Java-Spring-SpringIoC容器-SpringIoC的学习 SpringIoC容器的学习笔记 主要介绍了IoC容器工作原理以及如何配置IoC容器 通过标签和注解配置容器
详细讲解了springioc的各种注入方式以及对应的java方式。讲解了springioc和DI的区别,以及注入特殊复杂的属性
主要为大家详细解析了Spring中IoC优点与缺点,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
以注解方式模拟Spring IoC AOP,了解Spring是如何实现的.
迷你书是《Spring揭秘》的精选版,节选了原书中介绍Spring IoC容器的6个章节。《Spring揭秘》以幽默生动的语言、辅以有趣的故事和典故,循循善诱地阐述了Spring框架的方方面面。针对Spring框架的主要功能以及开发者...
这是个模拟spring ioc的学习例子,是个java工程。有助于对aop和ioc的理解
Spring IoC加载流程讲解以及IoC思想和依赖倒置原则
Spring IOC Annotation 注入 学习实例
SpringIOC经典Demo,绝对的经典.
Spring Ioc 注解 依赖注入