In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to realize the priority loading method of Spring Bean". In the daily operation, I believe that many people have doubts about how to realize the priority loading method of Spring Bean. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to realize the priority loading method of Spring Bean". Next, please follow the editor to study!
In daily business development, most of us don't pay attention to the loading order of bean. However, in some scenarios, when we want one bean to be instantiated better than other bean, it is often not as simple as we thought.
Start the method of class specification
In actual SpringBoot development, we know that there will be a startup class. If you want a class to be loaded first, a simple implementation with the lowest cost is to add dependencies to the startup class.
@ SpringBootApplicationpublicclass Application {/ / use the constructor to load DemoBean public Application (DemoBean demoBean) {demoBean.print ();} public static void main (String [] args) {SpringApplication.run (Application.class) first }} Please note the construction method above. If we want demoBean to be loaded before the application starts, let Application force it to rely on it, so before initializing the bean of Application, we will definitely instantiate the demoBeanInstantiationAwareBeanPostProcessorAdapter mode first.
With the help of InstantiationAwareBeanPostProcessorAdapter, it is possible to load the target bean first before bean instantiation.
Publicclass ClientBeanProcessor extends InstantiationAwareBeanPostProcessorAdapter implements BeanFactoryAware {private ConfigurableListableBeanFactory beanFactory; @ Override public void setBeanFactory (BeanFactory beanFactory) {if (! (beanFactory instanceof ConfigurableListableBeanFactory)) {thrownew IllegalArgumentException ("AutowiredAnnotationBeanPostProcessor requires a ConfigurableListableBeanFactory:" >
The above implementation is relatively simple, with the help of beanFactory#getBean to manually trigger the instance of bean, and to obtain the BeanFactory by implementing the BeanFactoryAware interface, because the class that implements the InstantiationAwareBeanPostProcessor interface will take precedence over the Bean instance, so as to achieve our goal indirectly.
The next question is how to make it work. We use Import annotations to implement it here.
@ Target ({ElementType.TYPE}) @ Retention (RetentionPolicy.RUNTIME) @ Documented@Inherited@Import ({ClientAutoConfiguration.class, ClientBeanProcessor.class}) public@interface EnableOrderClient {} package com.spring.master.spring.bean.initbean;import lombok.Getter;import org.springframework.core.env.Environment;import javax.annotation.PostConstruct;/** * @ author Huan Lee * @ version 1.0 * @ date 2020-09-25 11:58 * @ describtion. * / public class DatasourceLoader {@ Getter private String mode; public DatasourceLoader (Environment environment) {this.mode = environment.getProperty ("config.save.mode"); System.out.println ("init DatasourceLoader for:" + mode);} @ PostConstruct public void loadResourcres () {System.out.println ("start initializing Resources");}} package com.spring.master.spring.bean.initbean;import org.springframework.stereotype.Component / * * @ author Huan Lee * @ version 1.0 * @ date 2020-09-25 12:01 * @ describtion What you do is done in thought, but it is destroyed by doing as you wish. * / @ Componentpublic class DemoBean {public DemoBean () {System.out.println ("demo bean init!");} public void print () {System.out.println ("print demo bean");}} package com.spring.master.spring.bean.initbean;import org.springframework.context.annotation.Import;import java.lang.annotation.* / * * @ author Huan Lee * @ version 1.0 * @ date 2020-09-25 11:59 * @ describtion What you do is done in thought, but it is destroyed by doing as you wish. * / @ Target ({ElementType.TYPE}) @ Retention (RetentionPolicy.RUNTIME) @ Documented@Inherited@Import ({ClientAutoConfiguration.class, ClientBeanProcessor.class}) public @ interface EnableOrderClient {} package com.spring.master;import com.spring.master.spring.bean.initbean.DemoBean;import com.spring.master.spring.bean.initbean.EnableOrderClient;import com.spring.master.spring.bean.lifecycle.Person;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ApplicationContext Import org.springframework.context.support.ClassPathXmlApplicationContext;@EnableOrderClient@SpringBootApplicationpublic class SpringMasterApplication {public SpringMasterApplication (DemoBean demoBean) {demoBean.print ();} public static void main (String [] args) {SpringApplication.run (SpringMasterApplication.class, args) }} start service output: init DatasourceLoader for:null starts initializing the resource com.spring.master.spring.bean.initbean.DatasourceLoader@458342d3demo bean initableness print demo bean. At this point, the study on "how to implement the priority loading method of Spring Bean" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.