本文作者:geek,一個聰明好學的同事
1. 簡介
開發中我們常用@Commpont,@Service,@Resource等註解或者配置xml去宣告一個類,使其成為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在呼叫AnnotationConfigApplicationContext建構函式的時候,AppConfig類會被註冊到AnnotatedBeanDefinitionReader,由這個reader把AppConfig解釋為beanDefination,從而被spring獲取到要例項化的類資訊,以下為bean生產的原始碼及其註釋。(原始碼基於springFramework 5.1.X)
3.1 建立入口
單例bean的建立的入口為DefaultListableBeanFactory.java#preInstantiateSingletons,下面原始碼可見建立的bean條件為非抽象,非@LazyInit註解,Scope為singleTon(預設為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.
//所有可能需要去例項化的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例項化
else {
getBean(beanName);
}
}
}
/**
* 例項化完成後觸發實現了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 建立前doGetBean程式碼邏輯
getBean方法進來後便是直接呼叫doGetBean,doGetBean執行的原始碼解釋如下:
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轉換為原來名字
*/
final String beanName = transformedBeanName(name);
Object bean;
// Eagerly check singleton cache for manually registered singletons.
/**
* 從DefaultSingletonBeanRegistry的singletonObjects
* (spring內部用來快取單例bean的currentHashMap)檢查是否存在該bean,不存在則建立
* 涉及單例模式下的迴圈依賴解決
*/
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例項的物件,如果是Factory Bean,
* 則可以是bean例項本身或其建立的物件。
*/
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}
else {
// Fail if we're already creating this bean instance:
// We're assembly within a circular reference.
/**
* 原型模式下的bean存在迴圈依賴則會拋異常
*/
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,則需要重新例項化
* 1,獲取要例項化bean的beanDefinition,
* 2,檢查bean的例項化需要依賴的其他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 已經註冊為依賴給定的bean
*/
if (isDependent(beanName, dep)) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
}
registerDependentBean(dep, beanName);
try {
/**
* 遞迴呼叫getBean,優先建立依賴的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()) {
/**
* 通過呼叫DefaultSingletonBeanRegistry的getSingleton,從而呼叫核心方法createBean建立
*/
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,執行transformedBeanName方法轉換beanName
傳遞的引數可能是bean的alias或者為FactoryBean,transformedBeanName執行的操作:(1)若傳進來的FactoryBean(FactoryBean以&作為字首標記),去掉&修飾符。(2)經過(1)的處理後,有alias的bean則從aliasMap中獲取bean的原始beanName。
2,從容器的快取中獲取bean
getSingleton先從spring三級快取中的第一級singletonObjects(Map結構)中獲取,若不存在,則檢查該bean是否正在建立isSingletonCurrentlyInCreation(beanName)) ,正在建立的bean會從二級快取earlySingletonObjects(Map結構)獲取。獲取到快取的bean後會呼叫AbstractBeanFactory#getObjectForBeanInstance轉換bean的例項本身返回。因為從快取中拿到的可能是factoryBean,所以getObjectForBeanInstance需要把是通過從快取factoryBeanObjectCache獲取或通過factory.getObject()獲得相應的bean返回。
3,bean例項化前檢查
(1)先檢查是否原型模式下的bean是否存在迴圈依賴,是則會拋異常。
(2)檢查父類工廠(parentBeanFactory)是否存在,存在則從parentBeanFactory中遞迴呼叫doGetBean。
(3)獲取改bean的beanDefinition,檢查該bean例項化過程中是否涉及依賴了其他的bean,若是則遞迴呼叫getBean,優先建立依賴的bean。(涉及單例下的迴圈以來解決,下篇文章詳細介紹)。
(4)對建立bean程式碼加synchronized和執行beforeSingletonCreation(beanName)前置處理。
3.3 建立前createBean邏輯
經過前面的doGetBean的一輪檢查與準備後,便在AbstractAutowireCapableBeanFactory#createBean中開始bean的建立。
/**
* Central method of this class: creates a bean instance,
* populates the bean instance, applies post-processors, etc.
* 解析指定 BeanDefinition 的 class
* 處理 override 屬性
* 例項化的前置處理
* 建立 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);
}
/**
* 對bean不存在lookup-method 和 replace-method
* 標記其方法的overloaded為false
*/
// Prepare method overrides.
try {
mbdToUse.prepareMethodOverrides();
}
catch (BeanDefinitionValidationException ex) {
throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
beanName, "Validation of method overrides failed", ex);
}
try {
/**
* 呼叫實現實現BeanPostProcessor的bean後置處理生成代理物件,
* 有代理物件則直接返回代理物件
*/
// 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 {
/**
* 無需代理的bean例項化
*/
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註解標記的方法,註解的方法返回的物件是原型)和 replace-method(
3,resolveBeforeInstantiation
呼叫實現實現BeanPostProcessor的bean後置處理生成代理物件,有代理物件則直接返回代理物件。(spring AOP則是基於此處實現)
3.4 bean的真正例項化createBeanInstance
在AbstractAutowireCapableBeanFactory#createBeanInstance中,真正建立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());
}
/**
* 通過提供supplier回撥方法建立
*/
Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
if (instanceSupplier != null) {
return obtainFromSupplier(instanceSupplier, beanName);
}
/**
* 通過工廠方法建立 bean 例項,可以是靜態工廠方法或者例項工廠
*/
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) {
/**
* 查詢已經bean已經快取解析的建構函式或者工廠方法
*/
if (mbd.resolvedConstructorOrFactoryMethod != null) {
resolved = true;
autowireNecessary = mbd.constructorArgumentsResolved;
}
}
}
/**
* 已經有快取的建構函式或者工廠方法,直接例項化
*/
if (resolved) {
if (autowireNecessary) {
return autowireConstructor(beanName, mbd, null, null);
}
else {
return instantiateBean(beanName, mbd);
}
}
/**
* 通過實現BeanPostProcessor的代理類autowired的建構函式例項化
*/
// 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);
}
/**
* 通過本身帶有autowired的建構函式例項化,通過呼叫反射newInstance實現
*/
// Preferred constructors for default construction?
ctors = mbd.getPreferredConstructors();
if (ctors != null) {
return autowireConstructor(beanName, mbd, ctors, null);
}
/**
* 無參建構函式例項化,通過呼叫反射newInstance實現
*/
// No special handling: simply use no-arg constructor.
return instantiateBean(beanName, mbd);
}
由上述原始碼可以看出,例項化bean操作流程如下:
1,如果存在 Supplier 回撥,則通過提供supplier回撥方法建立,如以下方式定義的bean:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Spring5Application.class)
public class BeanRegistrationTest {
@Autowired
private GenericWebApplicationContext context;
context.registerBean(A.class, () -> new A());
}
2,如果存在工廠方法,則通過工廠方法建立 bean 例項,可以是靜態工廠方法或者例項工廠,如以下方式定義的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,已經有快取的建構函式或者工廠方法,直接例項化。
4,以上三點都不存在,則使用帶參建構函式與無參建構函式例項化。如以下方式定義的bean:
@Commponet
public class A{}
4.總結
spring單例bean的例項化流程大概就是這樣,很多細節地方,包括迴圈依賴處理,bean屬性填充等細節點下一章介紹。
參考
檢視更多文章關注公眾號:好奇心森林