In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
1 how to define bean
Common ways to define Bean are:
By means of xml, for example:
Through annotations, use annotations such as @ Component on Class, such as
@ Componentpublic class xxxServicer {....}
Through @ Bean under the @ Configuration class, for example
@ Configurationpublic class xxxConfiguration {@ Bean public myBean myBean () {return new myBean ();}}
Although the three ways of defining Bean are different and the corresponding processing details are not the same, from a large logical point of view, they are all the same. The main process is shown in the following figure: the most important thing is that the problem is to find a way to define Bean, then generate BeanDefinition and register it in the context of Spring, and Spring automatically creates an instance of Bean.
2 BeanDefinition
BeanDefinition is an interface that describes an Bean instance, such as SINGLETON or PROTOTYPE, what the value of the property is, what the parameters of the constructor are, and so on. To put it simply, we can instantiate a Bean through a BeanDefinition. BeanDefinition and its main subclasses:
Let's briefly talk about each subcategory:
RootBeanDefinition and ChildBeanDefinition: these two BeanDefinition are relative and have been replaced by GenericBeanDefinition since the advent of Spring 2.5. Because it forces us to know the relationship between them when we write code.
GenericBeanDefinition: compared to RootBeanDefinition and ChildBeanDefinition, which have to be hard-coded at the time of definition, GenericBeanDefinition has the advantage of dynamically setting parent for GenericBeanDefinition.
AnnotatedBeanDefinition: to look at the name is to know that it is used to read and define Bean through annotations.
3 define Bean through xml file
Defining Bean through xml is the earliest way for Spring to define Bean. Therefore, how to parse the xml tag to BeanDefinition (), the entry is in the class org.springframework.beans.factory.xml.XmlBeanDefinitionReader, but the actual work is in org.springframework.beans.factory.xml.BeanDefinitionParserDelegate. There is a lot of code, but the actual logic is very simple, which is to parse such tags as defined by Spring. I wrote an article about how to customize Spring tags, parse them and register them in Spring-portal.
4 load Bean through annotations supported by Spring such as @ Component
If you want to define Bean with annotations such as @ Component, a prerequisite is that you have or @ ComponentScan annotations. But there is still a slight difference between the two ways:
4.1
Because it is a xml tag, you are parsing xml, the generated class org.springframework.context.annotation.ComponentScanBeanDefinitionParser, and the key code:
@ Overridepublic BeanDefinition parse (Element element, ParserContext parserContext) {/ / get the base-package tag String basePackage = element.getAttribute (BASE_PACKAGE_ATTRIBUTE); basePackage = parserContext.getReaderContext (). GetEnvironment (). ResolvePlaceholders (basePackage); String [] basePackages = StringUtils.tokenizeToStringArray (basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS); / / the actual processing class is ClassPathBeanDefinitionScanner ClassPathBeanDefinitionScanner scanner = configureScanner (parserContext, element) / / scan all classes under basePackage. If there is a @ Component tag, register in Spring Set beanDefinitions = scanner.doScan (basePackages); registerComponents (parserContext.getReaderContext (), beanDefinitions, element); return null;}
4.2 @ ComponentScan
The annotations correspond to whether the generated class is org.springframework.context.annotation.ComponentScanAnnotationParser. In fact, it is ClassPathBeanDefinitionScanner that actually works. The generation of the ComponentScanAnnotationParser class is accompanied by the annotation processing of @ Configuration (meaning @ ComponentScan must be used with @ Configuration). Processing @ Configuration is actually org.springframework.context.annotation.ConfigurationClassPostProcessor. Doesn't it feel a little roundabout.
In fact, to put it simply, when you find @ ComponentScan annotations when dealing with @ Configuration, you will generate ComponentScanAnnotationParser to scan @ Component annotations.
4.3 ClassPathBeanDefinitionScanner
As mentioned above, both annotations and tags will eventually be handed over to the ClassPathBeanDefinitionScanner class, which does 1. 5%. Scan all class under basePackage, if there are @ Component and other annotations, read the relevant attributes of @ Component, generate ScannedGenericBeanDefinition, and register in Spring.
5 through @ Bean mode
As mentioned earlier, @ ComponentScan is part of the @ Configuration process. Since the @ Bean annotation must also be used with @ Configuration, it means that the @ Bean processing is also in @ Configuration. In fact, it is finally handed over to the ConfigurationClassBeanDefinitionReader class to handle the key code:
Private void loadBeanDefinitionsForConfigurationClass (ConfigurationClass configClass, TrackedConditionEvaluator trackedConditionEvaluator) {/ / if you define yourself through the @ Import annotation, you need to register yourself with Spring if (configClass.isImported ()) {registerBeanDefinitionForImportedConfigurationClass (configClass);} / / this is the @ Bean for (BeanMethod beanMethod: configClass.getBeanMethods ()) {loadBeanDefinitionsForBeanMethod (beanMethod) on the processing method. } / / process @ ImportResource, where parsing xml is the XmlBeanDefinitionReader loadBeanDefinitionsFromImportedResources (configClass.getImportedResources ()) of parsing xml mentioned above; loadBeanDefinitionsFromRegistrars (configClass.getImportBeanDefinitionRegistrars ());}
6 instantiate BeanDefinition
I talked about how to convert the different ways to define Bean into BeanDefinition and add it to Spring (to be exact, keep it in BeanFactory's BeanDefinitionMap). The instantiation of these BeanDefinition is in the final stage of ApplicationContext, and the key code is in DefaultListableBeanFactory.
PreInstantiateSingletons () = (! bd.isAbstract () & & bd.isSingleton () & &! FactoryBean factory = (FactoryBean) getBean (FACTORY_BEAN_PREFIX + (System.getSecurityManager ()! = & & factory = AccessController.doPrivileged (PrivilegedAction ((SmartFactoryBean= (factory SmartFactoryBean & &))
Instantiate BeanFactory through getBean, the code is in AbstractAutowireCapableBeanFactory
Protected Object initializeBean (final String beanName, final Object bean, RootBeanDefinition mbd) {/ / process xxAware interface if (System.getSecurityManager ()! = null) {AccessController.doPrivileged (new PrivilegedAction () {@ Override public Object run () {invokeAwareMethods (beanName, bean); return null;}}, getAccessControlContext ()) } else {invokeAwareMethods (beanName, bean);} Object wrappedBean = bean; if (mbd = = null | |! mbd.isSynthetic ()) {/ / call BeanPostProcessors#postProcessBeforeInitialization wrappedBean = applyBeanPostProcessorsBeforeInitialization (wrappedBean, beanName);} try {/ / initialize, first determine whether it is InitializingBean, invokeInitMethods (beanName, wrappedBean, mbd) } catch (Throwable ex) {throw new BeanCreationException ((mbd! = null?) Mbd.getResourceDescription (): null), beanName, "Invocation of init method failed", ex);} if (mbd = = null | |! mbd.isSynthetic ()) {/ / call BeanPostProcessors#postProcessAfterInitialization wrappedBean = applyBeanPostProcessorsAfterInitialization (wrappedBean, beanName);} return wrappedBean;}
From the initialization and comments above, we can see that the method of the InitializeBean interface (and Init-method) and the calling order of BeanPostProcessors are summarized in 7.
To sum up, the idea of Spring loading Bean is the same, first read the relevant information to generate BeanDefinition, and then initialize Bean through BeanDefinition. If you know the above routines, you can clearly know how to customize Xml tags or custom annotations to inject Bean into Spring.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.