前言
在上一篇文章中,我們深入探討了 Spring 框架中 Bean 的實(shí)例化過(guò)程,該過(guò)程包括從 Bean 定義中加載當(dāng)前類、尋找所有實(shí)現(xiàn)了 InstantiationAwareBeanPostProcessor 接口的類并調(diào)用實(shí)例化前的方法、進(jìn)行實(shí)例化、調(diào)用 applyMergedBeanDefinitionPostProcessors 方法等多個(gè)步驟,最終生成了一個(gè)真正的 Bean 實(shí)例。但是,這個(gè) Bean 實(shí)例還沒(méi)有被初始化和注入屬性,還不能真正發(fā)揮作用。
在今天的文章中,我們將深入探討 Bean 的屬性注入和初始化流程,從而使其成為一個(gè)真正意義上的 Bean。這個(gè)過(guò)程包括屬性注入、Aware 接口回調(diào)、BeanPostProcessor 的前置和后置處理等多個(gè)步驟,通過(guò)本文的學(xué)習(xí),讀者將能夠更深入地了解 Spring 框架中 Bean 的屬性注入和初始化過(guò)程,為后續(xù)的學(xué)習(xí)和實(shí)踐打下堅(jiān)實(shí)的基礎(chǔ)。
populateBean
在 Spring 框架中,屬性注入是 Bean 初始化過(guò)程中的一個(gè)重要環(huán)節(jié)。在 Bean 實(shí)例化完成后,Spring 框架會(huì)根據(jù) Bean 定義中的屬性設(shè)置進(jìn)行屬性注入,同時(shí)還會(huì)調(diào)用一些 Aware 接口回調(diào)方法,以及一些 BeanPostProcessor 的前置和后置處理方法,最終完成 Bean 的初始化過(guò)程。好的,拋去不用看的,我們來(lái)看下剩下的源碼:
protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {
}
......
PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);
int resolvedAutowireMode = mbd.getResolvedAutowireMode();
if (resolvedAutowireMode == AUTOWIRE_BY_NAME || resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
// MutablePropertyValues是PropertyValues具體的實(shí)現(xiàn)類
MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
// Add property values based on autowire by name if applicable.
if (resolvedAutowireMode == AUTOWIRE_BY_NAME) {
autowireByName(beanName, mbd, bw, newPvs);
}
// Add property values based on autowire by type if applicable.
if (resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
autowireByType(beanName, mbd, bw, newPvs);
}
pvs = newPvs;
}
boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
boolean needsDepCheck = (mbd.getDependencyCheck() != AbstractBeanDefinition.DEPENDENCY_CHECK_NONE);
PropertyDescriptor[] filteredPds = null;
if (hasInstAwareBpps) {
if (pvs == null) {
pvs = mbd.getPropertyValues();
}
for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {
// 這里會(huì)調(diào)用AutowiredAnnotationBeanPostProcessor的postProcessProperties()方法,會(huì)直接給對(duì)象中的屬性賦值
// AutowiredAnnotationBeanPostProcessor內(nèi)部并不會(huì)處理pvs,直接返回了
PropertyValues pvsToUse = bp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName);
if (pvsToUse == null) {
if (filteredPds == null) {
filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
}
pvsToUse = bp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
if (pvsToUse == null) {
return;
}
}
pvs = pvsToUse;
}
}
......
// 如果當(dāng)前Bean中的BeanDefinition中設(shè)置了PropertyValues,那么最終將是PropertyValues中的值,覆蓋@Autowired
if (pvs != null) {
applyPropertyValues(beanName, mbd, bw, pvs);
}
}
PropertyValues
在 Spring 框架中,PropertyValues 對(duì)象是從 Bean 定義中獲取的,而我們自己定義的 Bean 并沒(méi)有這個(gè)屬性值。一般情況下,這一步會(huì)被跳過(guò),但如果需要注入屬性值,我們可以通過(guò)實(shí)現(xiàn) MergedBeanDefinitionPostProcessor 接口的 postProcessMergedBeanDefinition 方法來(lái)對(duì) Bean 定義進(jìn)行修改,從而添加需要注入的屬性值。
具體來(lái)說(shuō),我們可以定義一個(gè)實(shí)現(xiàn)了 MergedBeanDefinitionPostProcessor 接口的類,比如下面這個(gè)例子::
@Component
public class MyInstantiationAwareBeanPostProcessors implements InstantiationAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor {
@Override
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
if (beanName.equals("userService")) {
beanDefinition.setPropertyValues(new MutablePropertyValues().add("orderService", new First()));
}
}
}
在這個(gè)例子中,我們判斷如果 Bean 的名稱是 "userService",則添加一個(gè)名為 "orderService" 的屬性,并將其值設(shè)置為 First 類的一個(gè)實(shí)例。需要注意的是,為了能夠正常注入屬性值,我們需要在 Bean 中定義一個(gè)名為 "setOrderService" 的 setter 方法,這樣就可以注入進(jìn)去,當(dāng)然我寫的這個(gè)是報(bào)錯(cuò)的狀態(tài),這樣大家可以找到他是在哪里進(jìn)行調(diào)用的。
autowireByName/autowireByType
講解之前,我先聲明一下他跟我們的@autowired注解沒(méi)有半毛錢關(guān)系,除了上面一種我們?nèi)藶楦深A(yù)的,還有一種Spring自帶的方式,在我們配置類中:
@Bean(autowire = Autowire.BY_NAME)
public UserService userService(){
return new UserService();
}
這樣定義時(shí),他就會(huì)自動(dòng)掃描你這個(gè)當(dāng)前類中所有的set方法,是所有的、而且不區(qū)分的。這里以autowireByName為例講解,autowireByType類似:
protected void autowireByName(
String beanName, AbstractBeanDefinition mbd, BeanWrapper bw, MutablePropertyValues pvs) {
// 當(dāng)前Bean中能進(jìn)行自動(dòng)注入的屬性名
String[] propertyNames = unsatisfiedNonSimpleProperties(mbd, bw);
// 遍歷每個(gè)屬性名,并去獲取Bean對(duì)象,并設(shè)置到pvs中
for (String propertyName : propertyNames) {
if (containsBean(propertyName)) {
Object bean = getBean(propertyName);
pvs.add(propertyName, bean);
// 記錄一下propertyName對(duì)應(yīng)的Bean被beanName給依賴了
registerDependentBean(propertyName, beanName);
if (logger.isTraceEnabled()) {
logger.trace("Added autowiring by name from bean name '" + beanName +
"' via property '" + propertyName + "' to bean named '" + propertyName + "'");
}
}
else {
if (logger.isTraceEnabled()) {
logger.trace("Not autowiring property '" + propertyName + "' of bean '" + beanName +
"' by name: no matching bean found");
}
}
}
}
- unsatisfiedNonSimpleProperties:找到所有set方法
- getBean:按照set方法名字獲取bean
- pvs.add(propertyName, bean):設(shè)置到MutablePropertyValues屬性中,不是對(duì)我們的bean進(jìn)行屬性注入
那有些同學(xué)可能會(huì)想到了,為什么Spring已經(jīng)默認(rèn)提供了一套注入方式還有弄一個(gè)@autowired注解呢?主要是因?yàn)樗鼈兏髯杂胁煌膬?yōu)點(diǎn)和適用場(chǎng)景。
默認(rèn)的注入方式非常靈活,它會(huì)遍歷 Bean 中所有的 setter 方法,對(duì)每個(gè)屬性進(jìn)行注入,從而實(shí)現(xiàn)自動(dòng)裝配。這種方式適用于大多數(shù)情況,因?yàn)樗軌蜃詣?dòng)識(shí)別并注入所有需要的依賴項(xiàng),并且不需要進(jìn)行任何額外的配置。
而 @Autowired 注解則提供了更加精細(xì)的控制,它可以指定需要注入的屬性或方法,并且還可以指定注入的方式、名稱、是否必須等屬性。這種方式適用于需要更加精細(xì)的控制和配置的情況,@Autowired 注解是一個(gè)可插拔的組件,它只有在 Spring 容器啟動(dòng)時(shí)掃描到該注解時(shí)才能夠進(jìn)行自動(dòng)裝配。如果我們使用 XML 配置的方式啟動(dòng) Spring 容器,需要在配置文件中添加 context:component-scan 元素來(lái)開(kāi)啟自動(dòng)掃描功能,否則即使寫了 @Autowired 注解也不會(huì)進(jìn)行注入。
postProcessProperties
這一步將會(huì)對(duì)@autowired注解進(jìn)行屬性注入,其他的不看,這里只看下AutowiredAnnotationBeanPostProcessor對(duì)屬性或者方法的注入:
private InjectionMetadata buildAutowiringMetadata(final Class<?> clazz) {
// 如果一個(gè)Bean的類型是String...,那么則根本不需要進(jìn)行依賴注入
if (!AnnotationUtils.isCandidateClass(clazz, this.autowiredAnnotationTypes)) {
return InjectionMetadata.EMPTY;
}
List<InjectionMetadata.InjectedElement> elements = new ArrayList<>();
Class<?> targetClass = clazz;
do {
final List<InjectionMetadata.InjectedElement> currElements = new ArrayList<>();
// 遍歷targetClass中的所有Field
ReflectionUtils.doWithLocalFields(targetClass, field -> {
// field上是否存在@Autowired、@Value、@Inject中的其中一個(gè)
MergedAnnotation<?> ann = findAutowiredAnnotation(field);
if (ann != null) {
// static filed不是注入點(diǎn),不會(huì)進(jìn)行自動(dòng)注入
if (Modifier.isStatic(field.getModifiers())) {
if (logger.isInfoEnabled()) {
logger.info("Autowired annotation is not supported on static fields: " + field);
}
return;
}
// 構(gòu)造注入點(diǎn)
boolean required = determineRequiredStatus(ann);
currElements.add(new AutowiredFieldElement(field, required));
}
});
// 遍歷targetClass中的所有Method
ReflectionUtils.doWithLocalMethods(targetClass, method -> {
Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
return;
}
// method上是否存在@Autowired、@Value、@Inject中的其中一個(gè)
MergedAnnotation<?> ann = findAutowiredAnnotation(bridgedMethod);
if (ann != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
// static method不是注入點(diǎn),不會(huì)進(jìn)行自動(dòng)注入
if (Modifier.isStatic(method.getModifiers())) {
if (logger.isInfoEnabled()) {
logger.info("Autowired annotation is not supported on static methods: " + method);
}
return;
}
// set方法最好有入?yún)? if (method.getParameterCount() == 0) {
if (logger.isInfoEnabled()) {
logger.info("Autowired annotation should only be used on methods with parameters: " +
method);
}
}
boolean required = determineRequiredStatus(ann);
PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
currElements.add(new AutowiredMethodElement(method, required, pd));
}
});
elements.addAll(0, currElements);
targetClass = targetClass.getSuperclass();
}
while (targetClass != null && targetClass != Object.class);
return InjectionMetadata.forElements(elements, clazz);
}
- 如果一個(gè)Bean的類型是String...,那么則根本不需要進(jìn)行依賴注入
- 遍歷targetClass中的所有Field,static filed不是注入點(diǎn),不會(huì)進(jìn)行自動(dòng)注入
- 遍歷targetClass中的所有Method,static method不是注入點(diǎn),不會(huì)進(jìn)行自動(dòng)注入
- 上面的注入點(diǎn)構(gòu)造好后,會(huì)在外層直接invoke調(diào)用注入
這里強(qiáng)調(diào)一下在對(duì)方法注入點(diǎn)進(jìn)行注入時(shí),會(huì)先判斷一下是否有PropertyValues,如果有的話則跳過(guò)注入,AutowiredMethodElement源碼如下:
protected void inject(Object bean, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
// 如果pvs中已經(jīng)有當(dāng)前注入點(diǎn)的值了,則跳過(guò)注入
if (checkPropertySkipping(pvs)) {
return;
}
......
}
applyPropertyValues
直接應(yīng)用PropertyValues注入屬性,可以看到這一步在我們的@autowired解析注入之后,如果你有的屬性字段已經(jīng)被@autowired注入了,但是又有一個(gè)PropertyValues那么這個(gè)set方法會(huì)把你的@Autowired之前注入進(jìn)去的對(duì)象值覆蓋,源碼很多為了篇幅就不看了。知道這個(gè)方法是干啥的就行。
initializeBean
屬性填充完之后,終于進(jìn)入到了初始化階段,為什么需要初始化這一步呢?這是對(duì)bean的最終處理,該方法返回的對(duì)象才是Spring管理的最終對(duì)象,Spring AOP就是對(duì)初始化這一步做 的擴(kuò)展。
protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
invokeAwareMethods(beanName, bean);
return null;
}, getAccessControlContext());
}
else {
invokeAwareMethods(beanName, bean);
}
Object wrappedBean = bean;
// 初始化前
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
// 初始化
try {
invokeInitMethods(beanName, wrappedBean, mbd);
}
catch (Throwable ex) {
throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null),
beanName, "Invocation of init method failed", ex);
}
// 初始化后 AOP
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
return wrappedBean;
}
invokeAwareMethods
該方法就是Aware接口的實(shí)現(xiàn)
private void invokeAwareMethods(String beanName, Object bean) {
if (bean instanceof Aware) {
if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(beanName);
}
if (bean instanceof BeanClassLoaderAware) {
ClassLoader bcl = getBeanClassLoader();
if (bcl != null) {
((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);
}
}
if (bean instanceof BeanFactoryAware) {
((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
}
}
}
applyBeanPostProcessorsBeforeInitialization
初始化前的類處理,我們主講兩個(gè)類:ApplicationContextAwareProcessor、
InitDestroyAnnotationBeanPostProcessor通過(guò)這兩個(gè)類看看可以初始化前我們可以做哪些內(nèi)容:
ApplicationContextAwareProcessor
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (!(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||
bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||
bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware ||
bean instanceof ApplicationStartupAware)) {
return bean;
}
......
// 執(zhí)行aware方法
invokeAwareInterfaces(bean);
}
return bean;
}
初始化前會(huì)判斷當(dāng)前是否是某個(gè)Aware類,那么則執(zhí)行aware方法進(jìn)行回調(diào)。
InitDestroyAnnotationBeanPostProcessor
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
LifecycleMetadata metadata = findLifecycleMetadata(bean.getClass());
try {
metadata.invokeInitMethods(bean, beanName);
}
catch (InvocationTargetException ex) {
throw new BeanCreationException(beanName, "Invocation of init method failed", ex.getTargetException());
}
catch (Throwable ex) {
throw new BeanCreationException(beanName, "Failed to invoke init method", ex);
}
return bean;
}
- findLifecycleMetadata:好奇的小伙伴可以看下這個(gè)方法,他會(huì)構(gòu)造@PostConstruct、@PreDestroy執(zhí)行點(diǎn)
- metadata.invokeInitMethods:執(zhí)行帶有@PostConstruct方法
invokeInitMethods
protected void invokeInitMethods(String beanName, Object bean, @Nullable RootBeanDefinition mbd)
throws Throwable {
boolean isInitializingBean = (bean instanceof InitializingBean);
if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
......
((InitializingBean) bean).afterPropertiesSet();
}
}
if (mbd != null && bean.getClass() != NullBean.class) {
String initMethodName = mbd.getInitMethodName();
if (StringUtils.hasLength(initMethodName) &&
!(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
!mbd.isExternallyManagedInitMethod(initMethodName)) {
invokeCustomInitMethod(beanName, bean, mbd);
}
}
}
- 如果當(dāng)前類實(shí)現(xiàn)了InitializingBean接口,那么執(zhí)行afterPropertiesSet方法進(jìn)行初始化
- initMethodName:如果當(dāng)前類指定了初始方法,那么直接invoke執(zhí)行
applyBeanPostProcessorsAfterInitialization
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
throws BeansException {
Object result = existingBean;
for (BeanPostProcessor processor : getBeanPostProcessors()) {
Object current = processor.postProcessAfterInitialization(result, beanName);
if (current == null) {
return result;
}
result = current;
}
return result;
}
執(zhí)行完postProcessAfterInitialization方法后,那么這個(gè)對(duì)象終于初始化成功了
總結(jié)
今天我們主講bean的初始化,主要流程如下:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-445438.html
- 屬性注入,執(zhí)行@autowired、PropertyValues注入等
- 初始化前置方法,執(zhí)行@PostConstruct方法、回調(diào)Aware接口等
- 初始化,調(diào)用afterPropertiesSet或者initMethod
- 初始化后置方法
最后一節(jié)我們會(huì)講bean的銷毀,那么bean的生命周期系列文章會(huì)結(jié)束,實(shí)際上 Spring 框架還有很多其他的功能和特性,例如 AOP、事務(wù)管理、Web 開(kāi)發(fā)等等,博主還會(huì)進(jìn)行對(duì)Spring系列繼續(xù)更新,請(qǐng)大家繼續(xù)跟緊學(xué)習(xí)。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-445438.html
到了這里,關(guān)于Spring源碼:Bean生命周期(五)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!