In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what are the advanced features of Spring-IOC". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Lazy-Init delayed loading
Delayed loading of Bean (deferred creation)
The default behavior of the ApplicationContext container is to instantiate all singleton bean in advance when the server is started. Instantiating in advance means that as part of the initialization process, the ApplicationContext instance creates and configures all singleton bean.
For example:
The default settings for this bean are:
Lazy-init= "false", which is loaded immediately, means that it is instantiated immediately when spring starts. If you don't want the singleton bean to be instantiated in advance when the ApplicationContext implementation is initialized, you can set the bean to delay instantiation.
A bean with lazy-init set to true will not be instantiated in advance when the ApplicationContext starts, but will be instantiated the last time the container requests a bean through getBean.
If an immediate loaded bean1 is set and a delayed loaded bean2 is referenced, then the bean1 is instantiated when the container starts, and the bean2 is also instantiated because it is referenced by the bean1, which is also in line with the rule that the deferred loaded bean is instantiated only on the last call.
You can also control delay initialization at the container level by using the "default-lazy-init" attribute on the element. As configured below:
If the scope property of a bean is scope= "pototype", even if lazy-init= "false" is set, the container will not start.
The bean is instantiated, but is instantiated by calling the getBean method.
Application scenario
(1) opening the delayed loading stability to improve the start-up and operation performance of the container.
(2) delayed loading is set for infrequently used Bean, so that when it is used occasionally, it is not necessary for the Bean to take up resources since it is used.
FactoryBean and BeanFactory
The BeanFactory interface is the top-level interface of the container, which defines some basic behaviors of the container. It is responsible for producing and managing the factories of Bean, and specifically uses the subinterface types under it, such as ApplicationContext;. Here we focus on FactoryBean.
There are two kinds of Bean in Spring, ordinary Bean and factory Bean (FactoryBean). FactoryBean can generate some type of Bean instance (returned to us), which means we can customize the creation process of Bean with it.
The static and instantiation methods in the three ways created by Bean are similar to those of FactoryBean. FactoryBean is widely used, especially in some components of the Spring framework, and in the integration of other frameworks and Spring frameworks.
/ / Let us define the creation process of Bean (complete the definition of complex Bean) public interface FactoryBean {@ Nullable / / return the Bean instance created by FactoryBean. If isSingleton returns true, the instance will be placed in the singleton object cache pool of the Spring container Map T getObject () throws Exception; @ Nullable / / returns the Bean type Class getObjectType () created by FactoryBean / / return whether the default boolean isSingleton () {return true;}} is a singleton.
Company class
Package com.bxc.edu.pojo; / * * @ author bixc * / public class Company {private String name; private String address; private int scale; public String getName () {return name;} public void setName (String name) {this.name = name } public String getAddress () {return address;} public void setAddress (String address) {this.address = address;} public int getScale () {return scale;} public void setScale (int scale) {this.scale = scale } @ Override public String toString () {return "Company {" + "name='" + name +'\'+ ", address='" + address +'\'+ ", scale=" + scale +'}';}}
CompanyFactoryBean class
Package com.bxc.edu.factory; import com.bxc.edu.pojo.Company; import org.springframework.beans.factory.FactoryBean; / * * @ author bixc * / public class CompanyFactoryBean implements FactoryBean {private String companyInfo; / / Company name, address, size public void setCompanyInfo (String companyInfo) {this.companyInfo = companyInfo } @ Override public Company getObject () throws Exception {/ / simulate the creation of complex objects Company Company company = new Company (); String [] strings = companyInfo.split (","); company.setName (strings [0]); company.setAddress (strings [1]); company.setScale (Integer.parseInt (strings [2])); return company } @ Override public Class getObjectType () {return Company.class;} @ Override public boolean isSingleton () {return true;}}
Xml configuration
Test to get the objects generated by FactoryBean
Object companyBean = applicationContext.getBean ("companyBean"); System.out.println ("bean:" + companyBean); / / the results are as follows: bean:Company {name=' Ali', address=' Zhongguancun', scale=500}
To test and get FactoryBean, you need to add "&" before id
Object companyBean = applicationContext.getBean ("& companyBean"); System.out.println ("bean:" + companyBean); / / the result is as follows: bean:com.bxc.edu.factory.CompanyFactoryBean@53f6fd09 post processor
Spring provides two extension interfaces for post-processing bean, which are BeanPostProcessor and BeanFactoryPostProcessor, which are different in use.
Factory initialization (BeanFactory)-> Bean object
After BeanFactory initialization, you can use BeanFactoryPostProcessor for post processing to do some things.
After the Bean object is instantiated (not the entire life cycle of Bean is complete), you can use BeanPostProcessor for post-processing to do some things
Note: an object is not necessarily a springbean, but a springbean must be an object
BeanPostProcessor
BeanPostProcessor is for Bean-level processing, which can be targeted at a specific Bean.
This interface provides two methods, which are executed before and after the initialization method of Bean. The specific initialization method refers to what method it refers to, similar to the method specified by init-method when we define bean.
The definition class implements BeanPostProcessor, and by default all bean in the entire Spring container will be processed. If you want to deal with a specific bean, you can judge it by the method parameters. The two type parameters are Object and String, respectively.
The number is the instance of each bean, and the first parameter is the value of the name or id property of each bean. So we can determine the specific bean we are going to deal with by the first parameter.
Note: processing occurs after the instantiation and dependency annotations of the Spring container.
BeanFactoryPostProcessor
BeanFactory level processing, is for the entire Bean factory processing, typical application: PropertyPlaceholderConfigurer
This interface only provides several methods, and the method parameter is ConfigurableListableBeanFactory, which defines some methods.
There is a method called getBeanDefinition, based on which we can find the BeanDefinition object that we define bean. Then we can modify the defined properties. Here are the methods in BeanDefinition
The method name is similar to the attribute of our bean tag, and the setBeanClassName corresponds to the class attribute in the bean tag, so when we get the BeanDefinition object, we can manually modify the attribute value defined in the bean tag.
BeanDefinition object: the bean tag we defined in XML. Spring parses the bean tag into a JavaBean. This JavaBean is BeanDefinition.
Note: when calling the BeanFactoryPostProcessor method, bean has not yet been instantiated, and bean has just been parsed into a BeanDefinition object
This is the end of the content of "what are the advanced features of Spring-IOC". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.