1 Bean のライフサイクル
Spring フレームワークでは、Bean オブジェクトにもライフ サイクルがあります。ただし、Spring は Bean オブジェクトの管理に役立つため、Bean オブジェクトのライフ サイクルについてはあまり明確ではありません。したがって、Bean のライフ サイクルをマスターする必要があります。このステージが私たちにもたらしてくれたことは、とても必要なことです。
Bean のライフサイクルは実際には非常に単純で、オブジェクトの作成から破棄までのプロセスにすぎませんが、Spring は拡張可能なフレームワークとして、Bean の作成と破棄のプロセスに多くの拡張ポイントを追加しています。春が今日まで繁栄することができた理由の 1 つは。Bean のライフサイクルは、次の段階に大まかに要約できます。
- Bean定義
- Bean登録
- Beanの作成
- Beanインジェクション
- Beanの初期化
- 豆の破壊
2 Beanの定義、登録、作成プロセス
Bean の定義は非常に簡単で、私たちが行います。たとえば、Spring 構成ファイルで Bean を構成します。
<bean id="user" class="com.wwj.entity.User">
<property name="name" value="zs"/>
<property name="age" value="22"/>
</bean>
またはメモ:
@Component
public class User{
private String name;
private Integer age;
}
この時点で Bean が定義されます。次に、Spring は起動時にこれらの定義された Bean を登録します。設定ファイルの起動では、Spring は設定ファイル内の設定を解析して Bean を登録します。これは具体的には、refresh メソッドに反映されます。
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons. 实例化Bean
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
}
このfinishBeanFactoryInitialization(beanFactory)
メソッドはすべてのシングルトン Bean をインスタンス化するために使用されます。このメソッドのソース コードは次のとおりです。
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
// Initialize conversion service for this context.
if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
beanFactory.setConversionService(
beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
}
// Register a default embedded value resolver if no bean post-processor
// (such as a PropertyPlaceholderConfigurer bean) registered any before:
// at this point, primarily for resolution in annotation attribute values.
if (!beanFactory.hasEmbeddedValueResolver()) {
beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
}
// Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
for (String weaverAwareName : weaverAwareNames) {
getBean(weaverAwareName);
}
// Stop using the temporary ClassLoader for type matching.
beanFactory.setTempClassLoader(null);
// Allow for caching all bean definition metadata, not expecting further changes.
beanFactory.freezeConfiguration();
// Instantiate all remaining (non-lazy-init) singletons.
beanFactory.preInstantiateSingletons();
}
すべてのメソッドを詳細に分析するわけではありませんが、重要な部分のみを見ていきます。メソッドはbeanFactory.preInstantiateSingletons()
すべてのシングルトン Bean をインスタンス化します。Bean が作成される場所はわかったので、Spring はどの Bean を作成する必要があるかをどのように知るのでしょうか? 言い換えれば、設定ファイルはどこで解析されるのでしょうか? 元の更新メソッドに戻りましょう. そのうちの 1 つはConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory()
、構成ファイルを解析するために使用されます. ソース コードは次のとおりです:
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
refreshBeanFactory();
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (logger.isDebugEnabled()) {
logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
}
return beanFactory;
}
これはrefreshBeanFactory()
メソッドを呼び出します。
@Override
protected final void refreshBeanFactory() throws BeansException {
if (hasBeanFactory()) {
destroyBeans();
closeBeanFactory();
}
try {
DefaultListableBeanFactory beanFactory = createBeanFactory();
beanFactory.setSerializationId(getId());
customizeBeanFactory(beanFactory);
loadBeanDefinitions(beanFactory);
synchronized (this.beanFactoryMonitor) {
this.beanFactory = beanFactory;
}
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}
このメソッドは次に次のloadBeanDefinitions(beanFactory)
メソッドを呼び出します。
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
// Create a new XmlBeanDefinitionReader for the given BeanFactory.
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
// Configure the bean definition reader with this context's
// resource loading environment.
beanDefinitionReader.setEnvironment(this.getEnvironment());
beanDefinitionReader.setResourceLoader(this);
beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
// Allow a subclass to provide custom initialization of the reader,
// then proceed with actually loading the bean definitions.
initBeanDefinitionReader(beanDefinitionReader);
loadBeanDefinitions(beanDefinitionReader);
}
次に、loadBeanDefinitions(beanDefinitionReader)
メソッドを呼び出します。
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws{
Resource[] configResources = getConfigResources();
if (configResources != null) {
reader.loadBeanDefinitions(configResources);
}
String[] configLocations = getConfigLocations();
if (configLocations != null) {
reader.loadBeanDefinitions(configLocations);
}
}
これでほぼ完了です。コール スタックは非常に深いので、これ以上は説明しません。ここでは、XML ファイルを解析して Bean インスタンスを作成しています。
3 Beanインジェクションプロセス
オブジェクトを作成するプロセスでは、オブジェクトのプロパティに値を割り当てる必要もありますが、Spring ではそれをどのように実装するのでしょうか?
protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) throws BeanCreationException {
BeanWrapper instanceWrapper = null;
if (mbd.isSingleton()) {
instanceWrapper = (BeanWrapper)this.factoryBeanInstanceCache.remove(beanName);
}
if (instanceWrapper == null) {
instanceWrapper = this.createBeanInstance(beanName, mbd, args);
}
......
try {
// 看这里
this.populateBean(beanName, mbd, instanceWrapper);
exposedObject = this.initializeBean(beanName, exposedObject, mbd);
} catch (Throwable var18) {
}
}
this.populateBean(beanName, mbd, instanceWrapper)
このメソッドは属性割り当てを実装するために使用されます。
protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {
if (bw == null) {
if (mbd.hasPropertyValues()) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance");
}
else {
// Skip property population phase for null instance.
return;
}
}
// Give any InstantiationAwareBeanPostProcessors the opportunity to modify the
// state of the bean before properties are set. This can be used, for example,
// to support styles of field injection.
if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
return;
}
}
}
}
PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);
int resolvedAutowireMode = mbd.getResolvedAutowireMode();
if (resolvedAutowireMode == AUTOWIRE_BY_NAME || resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
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
if (bp instanceof
PropertyValues pvsToUse = ibp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName);
if (pvsToUse == null) {
if (filteredPds == null) {
filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
}
pvsToUse = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
ifnull) {
return;
}
}
pvs = pvsToUse;
}
}
}
if (needsDepCheck) {
ifnull) {
filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
}
checkDependencies(beanName, mbd, filteredPds, pvs);
}
if (pvs != null) {
applyPropertyValues(beanName, mbd, bw, pvs);
}
}
この方法は非常に長いので、段階的には説明しませんが、興味のある学生は自分でソース コードを読んでください。
4 Beanの破棄処理
破棄プロセスは非常に単純で、コンテナの close メソッドが呼び出されると、Spring は自動的に Bean の破棄メソッドを呼び出して破棄ロジックを実装します。
5 Beanのライフサイクル
上記の内容は Bean ライフサイクルの一般的な紹介にすぎません. 実際、Spring はライフサイクル全体を通じて多くの拡張ポイントを提供します. 具体的なプロセスは次のとおりです:
- Beanインスタンスの作成
- Bean の setter() メソッドを呼び出して属性値を設定します。
- Bean が Aware インターフェースを実装しているかどうかを確認し、実装されている場合は、対応するインターフェースのメソッドを呼び出します。
- コンテナ内に BeanPostProcessor がある場合は、その postProcessAfterInitialization を呼び出します。
- Bean が InitializingBean を実装しているかどうかを確認し、実装されている場合は、その afterPropertiesSet メソッドを呼び出します。
- Beanのinit-method属性が指定されているか確認し、指定されている場合は指定されたメソッドを呼び出します。
- コンテナ内に BeanPostProcessor がある場合は、その postProcessorAfterInitialization を呼び出します。
- Bean が DisposableBean を実装しているかどうかを確認し、実装している場合はそのメソッドを呼び出します
- Beanの destroy-method 属性が指定されているか確認し、指定されている場合は指定されたメソッドを呼び出します。
それをテストできます:
public class User implements ApplicationContextAware, InitializingBean, DisposableBean {
private
private
public User() {
System.out.println("1--》创建User实例");
}
public void setName(String name) {
this.name = name;
"2--》设置User的name属性");
}
public void setAge(Integer age) {
this.age = age;
"2--》设置User的age属性");
}
public void init() {
"6--》调用init-method属性指定的方法");
}
public void myDestroy() {
"9--》调用destroy-method属性指定的方法");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
"3--》调用对应Aware接口的方法");
}
@Override
public void afterPropertiesSet() throws Exception {
"5--》调用InitializingBean接口的afterPropertiesSet方法");
}
@Override
public void destroy() throws Exception {
"8--》调用DisposableBean接口的destroy方法");
}
}
この Bean は、ApplicationContextAware、InitialzingBean、DisposableBean など、Spring によって提供されるいくつかの拡張ポイントを実装するため、Bean ポストプロセッサを作成しましょう。
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("7--》调用MyBeanPostProcessor的postProcessBeforeInitialization方法");
return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("4--》调用MyBeanPostProcessor的postProcessAfterInitialization方法");
return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
}
}
最後に、それらをコンテナに登録し、Bean に対応する初期化メソッドと破棄メソッドを指定します。
@Configuration
public class MyBeanConfig {
@Bean(initMethod = "init", destroyMethod = "myDestroy")
public User user() {
User user = new User();
user.setName("zs");
user.setAge(30);
return user;
}
@Bean
public BeanPostProcessor beanPostProcessor() {
return new MyBeanPostProcessor();
}
}
実行結果は次のとおりです。
1--》创建User实例
2--》设置User的name属性
2--》设置User的age属性
3--》调用对应Aware接口的方法
4--》调用MyBeanPostProcessor的postProcessAfterInitialization方法
5--》调用InitializingBean接口的afterPropertiesSet方法
6--》调用init-method属性指定的方法
7--》调用MyBeanPostProcessor的postProcessBeforeInitialization方法
8--》调用DisposableBean接口的destroy方法
9--》调用destroy-method属性指定的方法
予想通り、Spring は各拡張ポイントを順番に呼び出し、Bean のライフサイクル全体と拡張ポイントを理解した後、各段階でやりたいことを実行し、ビジネスのカスタマイズを実現します。