Spring source code reading notes

Before the report navigation

Spring Source reading notes (1) Spring source reading notes (2) Spring source reading notes (3) Spring source reading notes (4)

Before the review

In the previous chapter section, due to the back of the need to, we with an article, in terms of BeanDefinition creation and Definition into DefaultListableBeanFactory. Finally, you put BeanDefinitionMap in BeanDefinitionMap.

It’s worth mentioning here that in the BeanDefinition Object, there’s a property, BeanClass, which is an Object, and in the createBeanDefinition method is the className property that’s set, and in the internal implementation, He assigns the String directly to the BeanClass object, which is instanceof when used.

The official start of the

The next article will begin in earnest, where we will begin to interpret the prepareBeanFactory method and, in the case of the Refresh method, the third method.

// Prepare the bean factory for use in this context.
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
Copy the code

Based on the comments above, I’m wondering what Bean factory he’s preparing, because in the previous article, he already had a Bean factory and set the BeanDefinition init. This is when I look down with a question.

protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
   // Tell the internal bean factory to use the context's class loader etc.
   // Tells the inner bean factory to use the context's classloader, etc.
   beanFactory.setBeanClassLoader(getClassLoader());
   if(! shouldIgnoreSpel) { beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
   }
   beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

   // Configure the bean factory with context callbacks.
   // Configure the bean factory using context callbacks.
   beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
   beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
   beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
   beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
   beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
   beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
   beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
   beanFactory.ignoreDependencyInterface(ApplicationStartupAware.class);

   // BeanFactory interface not registered as resolvable type in a plain factory.
   // The BeanFactory interface is not registered as a resolvable type in a normal factory.
   // MessageSource registered (and found for autowiring) as a bean.
   MessageSource is registered as a bean (and found for autowiring).
   beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
   beanFactory.registerResolvableDependency(ResourceLoader.class, this);
   beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
   beanFactory.registerResolvableDependency(ApplicationContext.class, this);

   // Register early post-processor for detecting inner beans as ApplicationListeners.
   // Register the early post-handler used to detect the internal bean as ApplicationListener.
   beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

   // Detect a LoadTimeWeaver and prepare for weaving, if found.
   // Check LoadTimeWeaver and prepare to weave (if found).
   if(! NativeDetector.inNativeImage() && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) { beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
      // Set a temporary ClassLoader for type matching.
      // Set up a temporary ClassLoader for type matching.
      beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
   }

   // Register default environment beans.
   // Register the default environment bean.
   if(! beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) { beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment()); }if(! beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) { beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME,  getEnvironment().getSystemProperties()); }if(! beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) { beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment()); }if (!beanFactory.containsLocalBean(APPLICATION_STARTUP_BEAN_NAME)) {
      beanFactory.registerSingleton(APPLICATION_STARTUP_BEAN_NAME, getApplicationStartup());
   }
}
Copy the code

In this code block, you can find a lot of comments in the code, which means we have less difficulty reading

However, after a brief reading, there is only one reaction in my mind, which is what this is all about, a bunch of Settings information, no wonder the beginning is called prepare Bean factory. We’re going to read this code in section mode like we did before.

First code

beanFactory.setBeanClassLoader(getClassLoader()); if (! shouldIgnoreSpel) { beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader())); } beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));Copy the code

In the above code, we first set up a Classloader in the Beanfactory, which is used to create beans.

I’m going to go to if, which literally means, should I ignore the el expression? / / Properties file to find the spring.spel.ignore property. We did not set it to false. An ExpressionResolver is set up. Inside is SpelExpressionParser StandardBeanExpressionResolver.

The last line adds an PropertyEditorRegister to the Bean factory, which is of type resourceEditorRegister. I don’t know if it’s a chain of responsibility or an observer. Take a look at where it’s used,

Second code

// Configure the bean factory using context callbacks.
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
beanFactory.ignoreDependencyInterface(ApplicationStartupAware.class);
Copy the code

This code is basically the same except for the first line

See the first line of code, used Spring should all know, BeanPostProcessor, here is to add a ApplicationContextAwareProcessor Bean plant. This class is also very simple. Let me read it briefly. We may have had contact.

episode

private void invokeAwareInterfaces(Object bean) { // ... If if (bean instanceof ApplicationContextAware) {((ApplicationContextAware) bean).setApplicationContext(this.applicationContext); }}Copy the code

When I was writing a Spring project, I had to get a Bean from the ApplicationContext. So how to get the ApplicationContext, most of us just create a class that implements the ApplicationContextArare interface, And then you can get it. What’s the reason? This is it. After Spring instantiates the ApplicationContextAware interface, a PostProcessor calls its setApplicationContext method.

Get back on track

After in setting up the ApplicationContextAwareProcessor, call a heap of ignoreDependencyInterface method, Incoming method of entity classes are involved in the ApplicationContextAwareProcessor interface, specific this way just to add value, a HashSet met again behind this attribute were discussed.

The third code

// BeanFactory interface not registered as resolvable type in a plain factory.
// The BeanFactory interface is not registered as a resolvable type in a normal factory.
// MessageSource registered (and found for autowiring) as a bean.
MessageSource is registered as a bean (and found for autowiring).
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);
Copy the code

Call a heap of registerResolvableDependency method, record here, maybe use later. May be used as a container.

The fourth code

beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));
Copy the code

Here we’ve added a BeanPostProcess, and the New class name is related to the observer mode in the first method in Refresh.

The fifth code

if(! NativeDetector.inNativeImage() && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) { beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
   // Set a temporary ClassLoader for type matching.
   // Set up a temporary ClassLoader for type matching.
   beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
Copy the code

This code has to do with loadTimeWeaver, and you can learn about loadTimeWeaver for yourself, or I’ll talk about it later in the opening article. We can skip this because we didn’t set it.

Code 6.

// Register default environment beans.
// Register the default environment bean.
if(! beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) { beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment()); }if(! beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) { beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME,  getEnvironment().getSystemProperties()); }if(! beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) { beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment()); }if(! beanFactory.containsLocalBean(APPLICATION_STARTUP_BEAN_NAME)) { beanFactory.registerSingleton(APPLICATION_STARTUP_BEAN_NAME, getApplicationStartup()); }Copy the code

Look at this very simple code, and I’m wondering what the containsLocalBean method means. To explain here, BeanFactory has an inheritance relationship. In the Bean interface he has the following explanation.

Return whether the local bean factory contains a bean of the given name, ignoring beans defined in ancestor contexts.

Check to see if the Bean exists in the current Facotry, regardless of whether it exists in its ancestor factory.

If the Bean does not already exist in the current Factory, register one in the current Bean Factory as a singleton.

Come to an end

This is the end of the article, as noted at the beginning of the method.

Prepare the bean factory for use in this context. Prepare the bean factory for use in this context.

conclusion

The purpose of writing an article is to help you consolidate your knowledge. You can point out your bad or wrong writing in the comments section. If you read the article and think it is helpful to you, you can like it. If you find some questions and have doubts, or do not understand, you can comment. Of course, I also hope to make friends with you and learn from each other.