本文作者:geek,一個(gè)聰明好學(xué)的同事
1. 簡(jiǎn)介
開(kāi)發(fā)中我們常用@Commpont,@Service,@Resource等注解或者配置xml去聲明一個(gè)類,使其成為spring容器中的bean,以下我將用從源碼角度看以AnnotationConfigApplicationContext為例看spring如何把帶有注解的類生成spring中bean。
2. 示例代碼
public class TestContext {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
SingleBean singleBean = context.getBean(SingleBean.class);
System.out.println("<=====>"+singleBean.getTestStr());
}
}
@ComponentScan("com.geek")
public class AppConfig {
}
@Component
public class SingleBean {
private String testStr = "testStr";
public String getTestStr() {
return testStr;
}
}
注意:以上代碼僅需要引入spring-context依賴即可。
3. 源碼分析
? 上面的demo在調(diào)用AnnotationConfigApplicationContext構(gòu)造函數(shù)的時(shí)候,AppConfig類會(huì)被注冊(cè)到AnnotatedBeanDefinitionReader,由這個(gè)reader把AppConfig解釋為beanDefination,從而被spring獲取到要實(shí)例化的類信息,以下為bean生產(chǎn)的源碼及其注釋。(源碼基于springFramework 5.1.X)
3.1 創(chuàng)建入口
? 單例bean的創(chuàng)建的入口為DefaultListableBeanFactory.java#preInstantiateSingletons,下面源碼可見(jiàn)創(chuàng)建的bean條件為非抽象,非@LazyInit注解,Scope為singleTon(默認(rèn)為singleTon)。
@Override
public void preInstantiateSingletons() throws BeansException {
if (logger.isTraceEnabled()) {
logger.trace("Pre-instantiating singletons in " + this);
}
// Iterate over a copy to allow for init methods which in turn register new bean definitions.
// While this may not be part of the regular factory bootstrap, it does otherwise work fine.
//所有可能需要去實(shí)例化的class(lazy,scope)
List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
// Trigger initialization of all non-lazy singleton beans...
for (String beanName : beanNames) {
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
/**
* 非抽象,非懶初始化,單例bean
*/
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
if (isFactoryBean(beanName)) {
Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
if (bean instanceof FactoryBean) {
final FactoryBean<?> factory = (FactoryBean<?>) bean;
boolean isEagerInit;
if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>)
((SmartFactoryBean<?>) factory)::isEagerInit,
getAccessControlContext());
}
else {
isEagerInit = (factory instanceof SmartFactoryBean &&
((SmartFactoryBean<?>) factory).isEagerInit());
}
if (isEagerInit) {
getBean(beanName);
}
}
}
//非工廠bean實(shí)例化
else {
getBean(beanName);
}
}
}
/**
* 實(shí)例化完成后觸發(fā)實(shí)現(xiàn)了SmartInitializingSingleton方法的bean
* 的afterSingletonsInstantiated方法
*/
// Trigger post-initialization callback for all applicable beans...
for (String beanName : beanNames) {
Object singletonInstance = getSingleton(beanName);
if (singletonInstance instanceof SmartInitializingSingleton) {
final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
smartSingleton.afterSingletonsInstantiated();
return null;
}, getAccessControlContext());
}
else {
smartSingleton.afterSingletonsInstantiated();
}
}
}
}
3.2 創(chuàng)建前doGetBean代碼邏輯
getBean方法進(jìn)來(lái)后便是直接調(diào)用doGetBean,doGetBean執(zhí)行的源碼解釋如下:
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
/**
* 獲取beanName
* 1,去掉factortoryBean前綴&
* 2,帶有別名的bean轉(zhuǎn)換為原來(lái)名字
*/
final String beanName = transformedBeanName(name);
Object bean;
// Eagerly check singleton cache for manually registered singletons.
/**
* 從DefaultSingletonBeanRegistry的singletonObjects
* (spring內(nèi)部用來(lái)緩存單例bean的currentHashMap)檢查是否存在該bean,不存在則創(chuàng)建
* 涉及單例模式下的循環(huán)依賴解決
*/
Object sharedInstance = getSingleton(beanName);
if (sharedInstance != null && args == null) {
if (logger.isTraceEnabled()) {
if (isSingletonCurrentlyInCreation(beanName)) {
logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +
"' that is not fully initialized yet - a consequence of a circular reference");
}
else {
logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
}
}
/**
*獲取給定bean實(shí)例的對(duì)象,如果是Factory Bean,
* 則可以是bean實(shí)例本身或其創(chuàng)建的對(duì)象。
*/
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}
else {
// Fail if we're already creating this bean instance:
// We're assembly within a circular reference.
/**
* 原型模式下的bean存在循環(huán)依賴則會(huì)拋異常
*/
if (isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
// Check if bean definition exists in this factory.
/**
* 找不到則從父容器中查找
*/
BeanFactory parentBeanFactory = getParentBeanFactory();
if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
// Not found -> check parent.
String nameToLookup = originalBeanName(name);
if (parentBeanFactory instanceof AbstractBeanFactory) {
return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
nameToLookup, requiredType, args, typeCheckOnly);
}
else if (args != null) {
// Delegation to parent with explicit args.
return (T) parentBeanFactory.getBean(nameToLookup, args);
}
else if (requiredType != null) {
// No args -> delegate to standard getBean method.
return parentBeanFactory.getBean(nameToLookup, requiredType);
}
else {
return (T) parentBeanFactory.getBean(nameToLookup);
}
}
if (!typeCheckOnly) {
markBeanAsCreated(beanName);
}
try {
/**
* 父容器中也找不到該bean,則需要重新實(shí)例化
* 1,獲取要實(shí)例化bean的beanDefinition,
* 2,檢查bean的實(shí)例化需要依賴的其他bean
*/
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
checkMergedBeanDefinition(mbd, beanName, args);
// Guarantee initialization of beans that the current bean depends on.
String[] dependsOn = mbd.getDependsOn();
if (dependsOn != null) {
for (String dep : dependsOn) {
/**
* 若給定的依賴 bean 已經(jīng)注冊(cè)為依賴給定的bean
*/
if (isDependent(beanName, dep)) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
}
registerDependentBean(dep, beanName);
try {
/**
* 遞歸調(diào)用getBean,優(yōu)先創(chuàng)建依賴的bean
*/
getBean(dep);
}
catch (NoSuchBeanDefinitionException ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"'" + beanName + "' depends on missing bean '" + dep + "'", ex);
}
}
}
// Create bean instance.
if (mbd.isSingleton()) {
/**
* 通過(guò)調(diào)用DefaultSingletonBeanRegistry的getSingleton,從而調(diào)用核心方法createBean創(chuàng)建
*/
sharedInstance = getSingleton(beanName, () -> {
try {
return createBean(beanName, mbd, args);
}
catch (BeansException ex) {
// Explicitly remove instance from singleton cache: It might have been put there
// eagerly by the creation process, to allow for circular reference resolution.
// Also remove any beans that received a temporary reference to the bean.
destroySingleton(beanName);
throw ex;
}
});
bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
else if (mbd.isPrototype()) {
// It's a prototype -> create a new instance.
Object prototypeInstance = null;
try {
beforePrototypeCreation(beanName);
prototypeInstance = createBean(beanName, mbd, args);
}
finally {
afterPrototypeCreation(beanName);
}
bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
}
else {
String scopeName = mbd.getScope();
final Scope scope = this.scopes.get(scopeName);
if (scope == null) {
throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
}
try {
Object scopedInstance = scope.get(beanName, () -> {
beforePrototypeCreation(beanName);
try {
return createBean(beanName, mbd, args);
}
finally {
afterPrototypeCreation(beanName);
}
});
bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
}
catch (IllegalStateException ex) {
throw new BeanCreationException(beanName,
"Scope '" + scopeName + "' is not active for the current thread; consider " +
"defining a scoped proxy for this bean if you intend to refer to it from a singleton",
ex);
}
}
}
catch (BeansException ex) {
cleanupAfterBeanCreationFailure(beanName);
throw ex;
}
}
/**
* 檢查需要的bean類型是否符合
*/
// Check if required type matches the type of the actual bean instance.
if (requiredType != null && !requiredType.isInstance(bean)) {
try {
T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType);
if (convertedBean == null) {
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
return convertedBean;
}
catch (TypeMismatchException ex) {
if (logger.isTraceEnabled()) {
logger.trace("Failed to convert bean '" + name + "' to required type '" +
ClassUtils.getQualifiedName(requiredType) + "'", ex);
}
throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
}
}
return (T) bean;
}
doGetBean的操作流程如下:
1,執(zhí)行transformedBeanName方法轉(zhuǎn)換beanName
? 傳遞的參數(shù)可能是bean的alias或者為FactoryBean,transformedBeanName執(zhí)行的操作:(1)若傳進(jìn)來(lái)的FactoryBean(FactoryBean以&作為前綴標(biāo)記),去掉&修飾符。(2)經(jīng)過(guò)(1)的處理后,有alias的bean則從aliasMap中獲取bean的原始beanName。
2,從容器的緩存中獲取bean
? getSingleton先從spring三級(jí)緩存中的第一級(jí)singletonObjects(Map結(jié)構(gòu))中獲取,若不存在,則檢查該bean是否正在創(chuàng)建isSingletonCurrentlyInCreation(beanName)) ,正在創(chuàng)建的bean會(huì)從二級(jí)緩存earlySingletonObjects(Map結(jié)構(gòu))獲取。獲取到緩存的bean后會(huì)調(diào)用AbstractBeanFactory#getObjectForBeanInstance轉(zhuǎn)換bean的實(shí)例本身返回。因?yàn)閺木彺嬷心玫降目赡苁莊actoryBean,所以getObjectForBeanInstance需要把是通過(guò)從緩存factoryBeanObjectCache獲取或通過(guò)factory.getObject()獲得相應(yīng)的bean返回。
3,bean實(shí)例化前檢查
? (1)先檢查是否原型模式下的bean是否存在循環(huán)依賴,是則會(huì)拋異常。
? (2)檢查父類工廠(parentBeanFactory)是否存在,存在則從parentBeanFactory中遞歸調(diào)用doGetBean。
? (3)獲取改bean的beanDefinition,檢查該bean實(shí)例化過(guò)程中是否涉及依賴了其他的bean,若是則遞歸調(diào)用getBean,優(yōu)先創(chuàng)建依賴的bean。(涉及單例下的循環(huán)以來(lái)解決,下篇文章詳細(xì)介紹)。
? (4)對(duì)創(chuàng)建bean代碼加synchronized和執(zhí)行beforeSingletonCreation(beanName)前置處理。
3.3 創(chuàng)建前createBean邏輯
? 經(jīng)過(guò)前面的doGetBean的一輪檢查與準(zhǔn)備后,便在AbstractAutowireCapableBeanFactory#createBean中開(kāi)始bean的創(chuàng)建。
/**
* Central method of this class: creates a bean instance,
* populates the bean instance, applies post-processors, etc.
* 解析指定 BeanDefinition 的 class
* 處理 override 屬性
* 實(shí)例化的前置處理
* 創(chuàng)建 bean
* @see #doCreateBean
*/
@Override
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException {
if (logger.isTraceEnabled()) {
logger.trace("Creating instance of bean '" + beanName + "'");
}
RootBeanDefinition mbdToUse = mbd;
// Make sure bean class is actually resolved at this point, and
// clone the bean definition in case of a dynamically resolved Class
// which cannot be stored in the shared merged bean definition.
/**
* 解釋bean的class,看beanDefinition是否有class,否則load class
*/
Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
mbdToUse = new RootBeanDefinition(mbd);
mbdToUse.setBeanClass(resolvedClass);
}
/**
* 對(duì)bean不存在lookup-method 和 replace-method
* 標(biāo)記其方法的overloaded為false
*/
// Prepare method overrides.
try {
mbdToUse.prepareMethodOverrides();
}
catch (BeanDefinitionValidationException ex) {
throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
beanName, "Validation of method overrides failed", ex);
}
try {
/**
* 調(diào)用實(shí)現(xiàn)實(shí)現(xiàn)BeanPostProcessor的bean后置處理生成代理對(duì)象,
* 有代理對(duì)象則直接返回代理對(duì)象
*/
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
if (bean != null) {
return bean;
}
}
catch (Throwable ex) {
throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
"BeanPostProcessor before instantiation of bean failed", ex);
}
try {
/**
* 無(wú)需代理的bean實(shí)例化
*/
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
if (logger.isTraceEnabled()) {
logger.trace("Finished creating instance of bean '" + beanName + "'");
}
return beanInstance;
}
catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {
// A previously detected exception with proper bean creation context already,
// or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry.
throw ex;
}
catch (Throwable ex) {
throw new BeanCreationException(
mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex);
}
}
createBean的操作流程如下:
1,resolveBeanClass
? 解釋beanDefinition的class,并且保存在beanDefinition中。
2,prepareMethodOverrides
? 處理bean中的lookup-method (在單例bean用 @Lookup注解標(biāo)記的方法,注解的方法返回的對(duì)象是原型)和 replace-method( <replaced-method name=A replacer=B/>標(biāo)記的方法,標(biāo)記bean中A的方法被實(shí)現(xiàn)被另外一個(gè)實(shí)現(xiàn)MethodReplacer接口的B方法替代)。
3,resolveBeforeInstantiation
? 調(diào)用實(shí)現(xiàn)實(shí)現(xiàn)BeanPostProcessor的bean后置處理生成代理對(duì)象,有代理對(duì)象則直接返回代理對(duì)象。(spring AOP則是基于此處實(shí)現(xiàn))
3.4 bean的真正實(shí)例化createBeanInstance
? 在AbstractAutowireCapableBeanFactory#createBeanInstance中,真正創(chuàng)建bean,源碼及注釋如下:
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
// Make sure bean class is actually resolved at this point.
Class<?> beanClass = resolveBeanClass(mbd, beanName);
if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Bean class isn't public, and non-public access not allowed: " + beanClass.getName());
}
/**
* 通過(guò)提供supplier回調(diào)方法創(chuàng)建
*/
Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
if (instanceSupplier != null) {
return obtainFromSupplier(instanceSupplier, beanName);
}
/**
* 通過(guò)工廠方法創(chuàng)建 bean 實(shí)例,可以是靜態(tài)工廠方法或者實(shí)例工廠
*/
if (mbd.getFactoryMethodName() != null) {
return instantiateUsingFactoryMethod(beanName, mbd, args);
}
// Shortcut when re-creating the same bean...
boolean resolved = false;
boolean autowireNecessary = false;
if (args == null) {
synchronized (mbd.constructorArgumentLock) {
/**
* 查找已經(jīng)bean已經(jīng)緩存解析的構(gòu)造函數(shù)或者工廠方法
*/
if (mbd.resolvedConstructorOrFactoryMethod != null) {
resolved = true;
autowireNecessary = mbd.constructorArgumentsResolved;
}
}
}
/**
* 已經(jīng)有緩存的構(gòu)造函數(shù)或者工廠方法,直接實(shí)例化
*/
if (resolved) {
if (autowireNecessary) {
return autowireConstructor(beanName, mbd, null, null);
}
else {
return instantiateBean(beanName, mbd);
}
}
/**
* 通過(guò)實(shí)現(xiàn)BeanPostProcessor的代理類autowired的構(gòu)造函數(shù)實(shí)例化
*/
// Candidate constructors for autowiring?
Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
return autowireConstructor(beanName, mbd, ctors, args);
}
/**
* 通過(guò)本身帶有autowired的構(gòu)造函數(shù)實(shí)例化,通過(guò)調(diào)用反射newInstance實(shí)現(xiàn)
*/
// Preferred constructors for default construction?
ctors = mbd.getPreferredConstructors();
if (ctors != null) {
return autowireConstructor(beanName, mbd, ctors, null);
}
/**
* 無(wú)參構(gòu)造函數(shù)實(shí)例化,通過(guò)調(diào)用反射newInstance實(shí)現(xiàn)
*/
// No special handling: simply use no-arg constructor.
return instantiateBean(beanName, mbd);
}
由上述源碼可以看出,實(shí)例化bean操作流程如下:
1,如果存在 Supplier 回調(diào),則通過(guò)提供supplier回調(diào)方法創(chuàng)建,如以下方式定義的bean:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Spring5Application.class)
public class BeanRegistrationTest {
@Autowired
private GenericWebApplicationContext context;
context.registerBean(A.class, () -> new A());
}
2,如果存在工廠方法,則通過(guò)工廠方法創(chuàng)建 bean 實(shí)例,可以是靜態(tài)工廠方法或者實(shí)例工廠,如以下方式定義的bean:
public class AFactory implements FactoryBean<A> {
@Override
public A getObject() throws Exception {
return new A();
}
@Override
public Class<?> getObjectType() {
return A.class;
}
}
//或:
@Configuration
public class BeanConfigration {
@Bean
public A a(){
return new A();
}
}
3,已經(jīng)有緩存的構(gòu)造函數(shù)或者工廠方法,直接實(shí)例化。
4,以上三點(diǎn)都不存在,則使用帶參構(gòu)造函數(shù)與無(wú)參構(gòu)造函數(shù)實(shí)例化。如以下方式定義的bean:
@Commponet
public class A{}
4.總結(jié)
? spring單例bean的實(shí)例化流程大概就是這樣,很多細(xì)節(jié)地方,包括循環(huán)依賴處理,bean屬性填充等細(xì)節(jié)點(diǎn)下一章介紹。
參考
查看更多文章關(guān)注我的公眾號(hào):好奇心森林
