Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of Spring beand scope and Lifecycle principle

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "Spring beand scope and Lifecycle principle Analysis". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn the "Spring beand scope and life cycle principle analysis"!

Scope of Spring beand

When set to singleton, it means that there can be only one instance of a class. When applying again, the same instance will be returned.

You can see that the hashcode values of the two bean instances are the same, indicating that when you apply for the same instance and set the scope of bean to prototype, you can see that you are applying for two different bean instances.

So far, only prototype scope and singleton scope have been learned, and the rest will be learned later.

Life cycle of Spring bean

1. The process of defining the configuration xml file

2. Initialize the ioc container to instantiate bean,

3. Use

4. Destroy the ioc container when it stops, destroy all bean instances created by the bean container

There are three ways to initialize and destroy

1. Implement the interface, InitializingBean,DisposableBean

2, define init-method and destroy-method in each bean

3. Define default default-init and default-destroy methods in the xml configuration file

/ * * public void defautinit () {System.out.println ("Bean defaut Init." + * this.hashCode ());} * * public void defautdestroy () {System.out.println ("Bean defaut destroy." + * this.hashCode ());} * / * @ Override public void destroy () throws Exception {* System.out.println ("Bean destory." + this.hashCode ()) } * * @ Override public void afterPropertiesSet () throws Exception {* System.out.println ("Bean init." + this.hashCode ());} * / * public void start () {System.out.println ("Bean start." + this.hashCode ());} * * public void stop () {System.out.println ("Bean stop." + this.hashCode ());} * /

The initialization and destruction methods defined by the interface take precedence over the methods defined in bean

The default method of xml has the lowest priority and can not even be implemented in bean and will not report an error. If the other two methods are implemented, they will be overwritten.

If the init and destroy methods are set in bean, it must be implemented, otherwise the startup will fail

At this point, I believe you have a deeper understanding of "Spring beand scope and Lifecycle principle Analysis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report