4.AutowireCapableBeanFactory 自动装配工厂
AutowireCapableBeanFactory 根据名称:自动装配的BeanFactory,其实也是对BeanFactory的增强
源代码:
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ package org.springframework.beans.factory.config; import java.util.Set; import org.springframework.beans.BeansException;
import org.springframework.beans.TypeConverter;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.NoUniqueBeanDefinitionException; /**
* Extension of the {@link org.springframework.beans.factory.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.
*
* <p>This subinterface of BeanFactory is not meant to be used in normal
* application code: stick to {@link org.springframework.beans.factory.BeanFactory}
* or {@link org.springframework.beans.factory.ListableBeanFactory} for
* typical use cases.
*
* <p>Integration code for other frameworks can leverage this interface to
* wire and populate existing bean instances that Spring does not control
* the lifecycle of. This is particularly useful for WebWork Actions and
* Tapestry Page objects, for example.
*
* <p>Note that this interface is not implemented by
* {@link org.springframework.context.ApplicationContext} facades,
* as it is hardly ever used by application code. That said, it is available
* from an application context too, accessible through ApplicationContext's
* {@link org.springframework.context.ApplicationContext#getAutowireCapableBeanFactory()}
* method.
*
* <p>You may also implement the {@link org.springframework.beans.factory.BeanFactoryAware}
* interface, which exposes the internal BeanFactory even when running in an
* ApplicationContext, to get access to an AutowireCapableBeanFactory:
* simply cast the passed-in BeanFactory to AutowireCapableBeanFactory.
*
* @author Juergen Hoeller
* @since 04.12.2003
* @see org.springframework.beans.factory.BeanFactoryAware
* @see org.springframework.beans.factory.config.ConfigurableListableBeanFactory
* @see org.springframework.context.ApplicationContext#getAutowireCapableBeanFactory()
*/
//自动装配的Bean 工厂
public interface AutowireCapableBeanFactory extends BeanFactory { /**
* Constant that indicates no externally defined autowiring. Note that
* BeanFactoryAware etc and annotation-driven injection will still be applied.
* @see #createBean
* @see #autowire
* @see #autowireBeanProperties
*/
//工厂没有自动装配的Bean
int AUTOWIRE_NO = 0; /**
* Constant that indicates autowiring bean properties by name
* (applying to all bean property setters).
* @see #createBean
* @see #autowire
* @see #autowireBeanProperties
*/
//根据名称自动装配的Bean
int AUTOWIRE_BY_NAME = 1; /**
* Constant that indicates autowiring bean properties by type
* (applying to all bean property setters).
* @see #createBean
* @see #autowire
* @see #autowireBeanProperties
*/
//表明根据类型自动装配
int AUTOWIRE_BY_TYPE = 2; /**
* Constant that indicates autowiring the greediest constructor that
* can be satisfied (involves resolving the appropriate constructor).
* @see #createBean
* @see #autowire
*/
//根据构造方法快速装配的Bean
int AUTOWIRE_CONSTRUCTOR = 3; /**
* Constant that indicates determining an appropriate autowire strategy
* through introspection of the bean class.
* @see #createBean
* @see #autowire
* @deprecated as of Spring 3.0: If you are using mixed autowiring strategies,
* prefer annotation-based autowiring for clearer demarcation of autowiring needs.
*/
//Bean的class内部来进行装配,Spring 3.0开始被弃用
@Deprecated
int AUTOWIRE_AUTODETECT = 4; //-------------------------------------------------------------------------
// Typical methods for creating and populating external bean instances
//典型的方法来创建和填充外部bean实例
//------------------------------------------------------------------------- /**
* Fully create a new bean instance of the given class.
* <p>Performs full initialization of the bean, including all applicable
* {@link BeanPostProcessor BeanPostProcessors}.
* <p>Note: This is intended for creating a fresh instance, populating annotated
* fields and methods as well as applying all standard bean initialization callbacks.
* It does <i>not</> imply traditional by-name or by-type autowiring of properties;
* use {@link #createBean(Class, int, boolean)} for those purposes.
* @param beanClass the class of the bean to create
* @return the new bean instance
* @throws BeansException if instantiation or wiring failed
*/
//根据bena的类型来创建Bean实例
<T> T createBean(Class<T> beanClass) throws BeansException; /**
* Populate the given bean instance through applying after-instantiation callbacks
* and bean property post-processing (e.g. for annotation-driven injection).
* <p>Note: This is essentially intended for (re-)populating annotated fields and
* methods, either for new instances or for deserialized instances. It does
* <i>not</i> imply traditional by-name or by-type autowiring of properties;
* use {@link #autowireBeanProperties} for those purposes.
* @param existingBean the existing bean instance
* @throws BeansException if wiring failed
*/
//给定对象,在后处理bean,进行自动装配
void autowireBean(Object existingBean) throws BeansException; /**
* Configure the given raw bean: autowiring bean properties, applying
* bean property values, applying factory callbacks such as {@code setBeanName}
* and {@code setBeanFactory}, and also applying all bean post processors
* (including ones which might wrap the given raw bean).
* <p>This is effectively a superset of what {@link #initializeBean} provides,
* fully applying the configuration specified by the corresponding bean definition.
* <b>Note: This method requires a bean definition for the given name!</b>
* @param existingBean the existing bean instance
* @param beanName the name of the bean, to be passed to it if necessary
* (a bean definition of that name has to be available)
* @return the bean instance to use, either the original or a wrapped one
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
* if there is no bean definition with the given name
* @throws BeansException if the initialization failed
* @see #initializeBean
*/
//根据Bean的BeanDefinition,来装配这个未加工的Object
Object configureBean(Object existingBean, String beanName) throws BeansException; //-------------------------------------------------------------------------
// Specialized methods for fine-grained control over the bean lifecycle
//------------------------------------------------------------------------- /**
* Fully create a new bean instance of the given class with the specified
* autowire strategy. All constants defined in this interface are supported here.
* <p>Performs full initialization of the bean, including all applicable
* {@link BeanPostProcessor BeanPostProcessors}. This is effectively a superset
* of what {@link #autowire} provides, adding {@link #initializeBean} behavior.
* @param beanClass the class of the bean to create
* @param autowireMode by name or type, using the constants in this interface
* @param dependencyCheck whether to perform a dependency check for objects
* (not applicable to autowiring a constructor, thus ignored there)
* @return the new bean instance
* @throws BeansException if instantiation or wiring failed
* @see #AUTOWIRE_NO
* @see #AUTOWIRE_BY_NAME
* @see #AUTOWIRE_BY_TYPE
* @see #AUTOWIRE_CONSTRUCTOR
*/
//传入指定的Bean的类型,指定的装配的策略,是否依赖检查 来创建一个完全新的Bean
Object createBean(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException; /**
* Instantiate a new bean instance of the given class with the specified autowire
* strategy. All constants defined in this interface are supported here.
* Can also be invoked with {@code AUTOWIRE_NO} in order to just apply
* before-instantiation callbacks (e.g. for annotation-driven injection).
* <p>Does <i>not</i> apply standard {@link BeanPostProcessor BeanPostProcessors}
* callbacks or perform any further initialization of the bean. This interface
* offers distinct, fine-grained operations for those purposes, for example
* {@link #initializeBean}. However, {@link InstantiationAwareBeanPostProcessor}
* callbacks are applied, if applicable to the construction of the instance.
* @param beanClass the class of the bean to instantiate
* @param autowireMode by name or type, using the constants in this interface
* @param dependencyCheck whether to perform a dependency check for object
* references in the bean instance (not applicable to autowiring a constructor,
* thus ignored there)
* @return the new bean instance
* @throws BeansException if instantiation or wiring failed
* @see #AUTOWIRE_NO
* @see #AUTOWIRE_BY_NAME
* @see #AUTOWIRE_BY_TYPE
* @see #AUTOWIRE_CONSTRUCTOR
* @see #AUTOWIRE_AUTODETECT
* @see #initializeBean
* @see #applyBeanPostProcessorsBeforeInitialization
* @see #applyBeanPostProcessorsAfterInitialization
*/
//类似上面
Object autowire(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException; /**
* Autowire the bean properties of the given bean instance by name or type.
* Can also be invoked with {@code AUTOWIRE_NO} in order to just apply
* after-instantiation callbacks (e.g. for annotation-driven injection).
* <p>Does <i>not</i> apply standard {@link BeanPostProcessor BeanPostProcessors}
* callbacks or perform any further initialization of the bean. This interface
* offers distinct, fine-grained operations for those purposes, for example
* {@link #initializeBean}. However, {@link InstantiationAwareBeanPostProcessor}
* callbacks are applied, if applicable to the configuration of the instance.
* @param existingBean the existing bean instance
* @param autowireMode by name or type, using the constants in this interface
* @param dependencyCheck whether to perform a dependency check for object
* references in the bean instance
* @throws BeansException if wiring failed
* @see #AUTOWIRE_BY_NAME
* @see #AUTOWIRE_BY_TYPE
* @see #AUTOWIRE_NO
*/
//自动装配
void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck)
throws BeansException; /**
* Apply the property values of the bean definition with the given name to
* the given bean instance. The bean definition can either define a fully
* self-contained bean, reusing its property values, or just property values
* meant to be used for existing bean instances.
* <p>This method does <i>not</i> autowire bean properties; it just applies
* explicitly defined property values. Use the {@link #autowireBeanProperties}
* method to autowire an existing bean instance.
* <b>Note: This method requires a bean definition for the given name!</b>
* <p>Does <i>not</i> apply standard {@link BeanPostProcessor BeanPostProcessors}
* callbacks or perform any further initialization of the bean. This interface
* offers distinct, fine-grained operations for those purposes, for example
* {@link #initializeBean}. However, {@link InstantiationAwareBeanPostProcessor}
* callbacks are applied, if applicable to the configuration of the instance.
* @param existingBean the existing bean instance
* @param beanName the name of the bean definition in the bean factory
* (a bean definition of that name has to be available)
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
* if there is no bean definition with the given name
* @throws BeansException if applying the property values failed
* @see #autowireBeanProperties
*/
//初始化之前执行BeanPostProcessors
void applyBeanPropertyValues(Object existingBean, String beanName) throws BeansException; /**
* Initialize the given raw bean, applying factory callbacks
* such as {@code setBeanName} and {@code setBeanFactory},
* also applying all bean post processors (including ones which
* might wrap the given raw bean).
* <p>Note that no bean definition of the given name has to exist
* in the bean factory. The passed-in bean name will simply be used
* for callbacks but not checked against the registered bean definitions.
* @param existingBean the existing bean instance
* @param beanName the name of the bean, to be passed to it if necessary
* (only passed to {@link BeanPostProcessor BeanPostProcessors})
* @return the bean instance to use, either the original or a wrapped one
* @throws BeansException if the initialization failed
*/
Object initializeBean(Object existingBean, String beanName) throws BeansException; /**
* Apply {@link BeanPostProcessor BeanPostProcessors} to the given existing bean
* instance, invoking their {@code postProcessBeforeInitialization} methods.
* The returned bean instance may be a wrapper around the original.
* @param existingBean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one
* @throws BeansException if any post-processing failed
* @see BeanPostProcessor#postProcessBeforeInitialization
*/
Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
throws BeansException; /**
* Apply {@link BeanPostProcessor BeanPostProcessors} to the given existing bean
* instance, invoking their {@code postProcessAfterInitialization} methods.
* The returned bean instance may be a wrapper around the original.
* @param existingBean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one
* @throws BeansException if any post-processing failed
* @see BeanPostProcessor#postProcessAfterInitialization
*/
Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
throws BeansException; /**
* Destroy the given bean instance (typically coming from {@link #createBean}),
* applying the {@link org.springframework.beans.factory.DisposableBean} contract as well as
* registered {@link DestructionAwareBeanPostProcessor DestructionAwareBeanPostProcessors}.
* <p>Any exception that arises during destruction should be caught
* and logged instead of propagated to the caller of this method.
* @param existingBean the bean instance to destroy
*/
//销毁指定的Bean
void destroyBean(Object existingBean); //-------------------------------------------------------------------------
// Delegate methods for resolving injection points
//------------------------------------------------------------------------- /**
* Resolve the bean instance that uniquely matches the given object type, if any,
* including its bean name.
* <p>This is effectively a variant of {@link #getBean(Class)} which preserves the
* bean name of the matching instance.
* @param requiredType type the bean must match; can be an interface or superclass.
* {@code null} is disallowed.
* @return the bean name plus bean instance
* @throws NoSuchBeanDefinitionException if no matching bean was found
* @throws NoUniqueBeanDefinitionException if more than one matching bean was found
* @throws BeansException if the bean could not be created
* @since 4.3.3
* @see #getBean(Class)
*/
//
<T> NamedBeanHolder<T> resolveNamedBean(Class<T> requiredType) throws BeansException; /**
* Resolve the specified dependency against the beans defined in this factory.
* @param descriptor the descriptor for the dependency (field/method/constructor)
* @param requestingBeanName the name of the bean which declares the given dependency
* @return the resolved object, or {@code null} if none found
* @throws NoSuchBeanDefinitionException if no matching bean was found
* @throws NoUniqueBeanDefinitionException if more than one matching bean was found
* @throws BeansException if dependency resolution failed for any other reason
* @since 2.5
* @see #resolveDependency(DependencyDescriptor, String, Set, TypeConverter)
*/
//分解指定的依赖
Object resolveDependency(DependencyDescriptor descriptor, String requestingBeanName) throws BeansException; /**
* Resolve the specified dependency against the beans defined in this factory.
* @param descriptor the descriptor for the dependency (field/method/constructor)
* @param requestingBeanName the name of the bean which declares the given dependency
* @param autowiredBeanNames a Set that all names of autowired beans (used for
* resolving the given dependency) are supposed to be added to
* @param typeConverter the TypeConverter to use for populating arrays and collections
* @return the resolved object, or {@code null} if none found
* @throws NoSuchBeanDefinitionException if no matching bean was found
* @throws NoUniqueBeanDefinitionException if more than one matching bean was found
* @throws BeansException if dependency resolution failed for any other reason
* @since 2.5
* @see DependencyDescriptor
*/
//同上
Object resolveDependency(DependencyDescriptor descriptor, String requestingBeanName,
Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException; }
英语翻译之前的都删了,也许有些地方自己理解的不对,可以指正下,
解析下:
1.常量:5个常量,1个是判断是工厂是否自动装配bean,其他常量是对自动装配的策略。其中常量等于4的这个在Spring3.0时候进行抛弃
2.6个自动装配bean的方法,3个和BeanPostProcessors有关的处理,2个指定的分解依赖的方法,1个销毁bean的方法,1个初始化Bean的方法
这个接口其实是扩张 了Bean的自动装配方法和前后处理器BeanPostProcessors
4.AutowireCapableBeanFactory 自动装配工厂的更多相关文章
- AutowireCapableBeanFactory 根据名称:自动装配的BeanFactory,其实也是对BeanFactory的增强
//自动装配的Bean 工厂 public interface AutowireCapableBeanFactory extends BeanFactory { //工厂没有自动装配的Bean int ...
- java:Spring框架2(bean的作用域,静态工厂和实例工厂,自动装配,动态代理)
1.bean的作用域,静态工厂和实例工厂: bean.xml: <?xml version="1.0" encoding="UTF-8"?> < ...
- Autofac 组件、服务、自动装配 《第二篇》
一.组件 创建出来的对象需要从组件中来获取,组件的创建有如下4种(延续第一篇的Demo,仅仅变动所贴出的代码)方式: 1.类型创建RegisterType AutoFac能够通过反射检查一个类型,选择 ...
- Spring学习笔记之 Spring IOC容器(一)之 实例化容器,创建JavaBean对象,控制Bean实例化,setter方式注入,依赖属性的注入,自动装配功能实现自动属性注入
本节主要内容: 1.实例化Spring容器示例 2.利用Spring容器创建JavaBean对象 3.如何控制Bean实例化 4.利用Spring实现bean属性sett ...
- Autofac 组件、服务、自动装配(2)
一.组件 创建出来的对象需要从组件中来获取,组件的创建有如下4种(延续第一篇的Demo,仅仅变动所贴出的代码)方式: 1.类型创建RegisterType AutoFac能够通过反射检查一个类型,选择 ...
- java之Spring(IOC)装配Bean(手动装配、自动装配、注解装配)
在上一篇控制反转中我们看到了依靠一个Bean文件来实现对代码的控制,可谓十分便捷,再也不用去实例化对象了,2333~~~ 1.手动装配 <bean id="todo" cla ...
- 第四章 Spring.Net 如何管理您的类___对象的自动装配
由于这几天都比较忙,所以对笔记暂时没有更新. Spring.NET具有自动装配的能力,也就是说,Spring.NET可以通过对象的定义自动分辨某个对象的协作对象.自动装配是针对单个对象(按:针对每个协 ...
- SpringBoot SpringApplication底层源码分析与自动装配
目录 抛出问题 @SpringBootApplication注解剖析 SpringApplication类剖析 第一步:配置SpringBoot Bean来源 第二步 :自动推断SpringBoot的 ...
- Spring Bean 注入 1 - 构造方法注入,属性注入,自动装配
1.代码结构图 xxx 2.bean代码 package com.xxx.bean; /** * Created with IntelliJ IDEA. * User: zhenwei.liu * D ...
随机推荐
- 飘逸的python - __get__ vs __getattr__ vs __getattribute__以及属性的搜索策略
差别: __getattribute__:是无条件被调用.对不论什么对象的属性訪问时,都会隐式的调用__getattribute__方法,比方调用t.__dict__,事实上运行了t.__getatt ...
- easyui datagrid 批量编辑和提交数据
easyui datagrid 行编辑和提交方,废话就不多说了,直接上代码 <div style="margin: 5px;"> <table id=" ...
- android-SQLite数据库MVC关联实例源码(三层架构)
前两天布置下了一个期末练习,其中的重点是两个表之间的SQLite关联操作. 拿到题目,首先需要建库和关联表,下面是代码. 我使用简单的表插入,将数据的提交卸载onCreate方法中,这样不会发生写在主 ...
- WCF configure
1. maxBufferSize 一个正整数,指定内存中用于存储消息的缓冲区的最大大小(字节). 如果 transferMode 属性等于 Buffered,则此属性应等于 maxReceivedMe ...
- Git提交时提示‘The file will have its original line endings in your working directory’
Git提交时提示'The file will have its original line endings in your working directory' Git出现错误 git add -A ...
- javascript 数组 find
find() 方法返回通过测试(函数内判断)的数组的第一个元素的值. let arr = [1,2,3,4] console.log(arr.find(i => {return i>1}) ...
- STL学习笔记(第五章 STL组件)
STL组件 若干精心勾画的组件共同合作,构筑起STL的基础.这些组件最关键的是容器.迭代器和算法. 下图演示了STL组件之间的合作 容器(Containers) 容器类别(简称容器)用来管理一组元素. ...
- Android——点击对话框上button不关闭对话框
有时候我没可能须要在点击button进行一些检測.可是并不想关闭次对话框(系统默认点击不论什么一个button则关闭对话框),处理方法例如以下:在点击事件下加入例如以下代码: try { Field ...
- CSS各属性选择符区别
CSS2.1: ele[attribute] 匹配具有属性attribute的ele元素. ele[attribute = value] 匹配具有属性attribute且值为value的元素. ele ...
- cocos2d-x 3.0 Android环境搭建(亲測通过)
网上一大堆讲述coco2d-x 3.0 版本号的android环境搭建.真是不忍直視.讲的不清不楚,真是不知道他们自己有没有測试过.今天正好忙完项目有点时间去部署了下android环境. cocos2 ...