In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you an example of Java Spring Bean life cycle management analysis, I hope you will learn something after reading this article, let's discuss it together!
Life cycle Management of Spring Bean I. Life cycle of Spring Bean
Specify the initialization and destruction method of Bean in the following ways. When Bean is singleton, Bean is managed by Spring container, and Spring container is closed, the destruction method of Bean is called. When Bean is multiple, Bean is not managed by Spring container, Spring container is closed, and Bean destruction method is not called. 2. Specify Bean initialization and destruction method 1 and project structure through @ Bean parameter (initMethod, destroyMethod)
2. Personpublic class Person {public Person () {System.out.println ("Person created...");} public void init () {System.out.println ("Person initialized...");} public void destroy () {System.out.println ("Person destroyed...");}} 3, Bean registration configuration class (single instance) import com.dashu.bean.Person Import org.springframework.context.annotation.*;@Configurationpublic class BeanConfig {@ Bean (initMethod = "init", destroyMethod = "destroy") public Person person () {return new Person ();}} 4, test class import com.dashu.bean.Person;import com.dashu.config.BeanConfig;import org.springframework.context.annotation.AnnotationConfigApplicationContext Public class Main {public static void main (String [] args) {/ / load configuration class to get container AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext (BeanConfig.class); / / get Bean Person bean = annotationConfigApplicationContext.getBean (Person.class); / / close container annotationConfigApplicationContext.close ();}} 5, test results
6. Bean registration configuration class (multi-instance) import com.dashu.bean.Person;import org.springframework.context.annotation.*;@Configurationpublic class BeanConfig {@ Scope ("prototype") @ Bean (initMethod = "init", destroyMethod = "destroy") public Person person () {return new Person ();}} 7, test results
3. Bean implements interfaces InitializingBean, DisposableBean1, Personimport org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;public class Person implements InitializingBean, DisposableBean {public Person () {System.out.println ("Person created...");} @ Override public void afterPropertiesSet () throws Exception {System.out.println ("Person initialized...") } @ Override public void destroy () throws Exception {System.out.println ("Person has been destroyed...");} 2, Bean registration configuration class import com.dashu.bean.Person;import org.springframework.context.annotation.*;@Configurationpublic class BeanConfig {@ Bean public Person person () {return new Person ();}} 3, test results
4. By annotating @ PostConstruct and @ PreDestroy@PostConstruct: marked on the initialization method of Bean @ PreDestroy: marked on the destruction method of Bean 1, Personimport javax.annotation.PostConstruct;import javax.annotation.PreDestroy;public class Person {public Person () {System.out.println ("Person created...");} @ PostConstruct public void init () {System.out.println ("Person initialized...") } @ PreDestroy public void destroy () {System.out.println ("Person has been destroyed...");}} 2, test results
Use the interface BeanPostProcessor to implement the class (post processor) 1. Project structure
2. Personimport org.springframework.stereotype.Component;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;@Componentpublic class Person {public Person () {System.out.println ("Person created...");} @ PostConstruct public void init () {System.out.println ("Person initialized...");} @ PreDestroy public void destroy () {System.out.println ("Person was destroyed...") }} 3. Bean registration configuration class import org.springframework.context.annotation.*;@Configuration@ComponentScan ({"com.dashu"}) public class BeanConfig {} 4, BeanPostProcessor implementation class (post processor) import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;import org.springframework.lang.Nullable;import org.springframework.stereotype.Component / * * Post processor: processing before and after initialization * / @ Componentpublic class MyBeanPostProcessor implements BeanPostProcessor {/ * work before initialization * @ param bean * @ param beanName * @ return * @ throws BeansException * / @ Override public Object postProcessBeforeInitialization (Object bean, String beanName) throws BeansException {System.out.println ("before initialization." + beanName+ "= [" + bean+ "]") Return bean;} / * work after initialization * @ param bean * @ param beanName * @ return * @ throws BeansException * / @ Override public Object postProcessAfterInitialization (Object bean, String beanName) throws BeansException {System.out.println ("after initialization..." + beanName+ "= [" + bean+ "]"); return bean;}} 5, test results
After reading this article, I believe you have some understanding of "sample Analysis of Java Spring Bean Lifecycle Management". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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.