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

Introduction to the definition and scope of Bean in Spring

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "the definition and scope introduction of Bean in Spring". In the daily operation, I believe that many people have doubts about the definition and scope of Bean in Spring. The editor 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 about "the definition and scope introduction of Bean in Spring". Next, please follow the editor to study!

What is Bean

1. Java object-oriented. Objects have methods and properties, so you need an object instance to call methods and properties (that is, instantiation).

2. All classes that have methods or properties need to be instantiated so that they can be materialized to use these methods and properties

3. Rule: all subclasses and classes with methods or attributes should be annotated by registering Bean to Spring IoC

4. Think of Bean as the agent or spokesman of the class (in fact, it is implemented through reflection, proxy), so that it can represent that the class has what it should have.

5. We all have @ so-and-so on Weibo, and the other person will see this message first and give you feedback, so in Spring, you mark an @ symbol, then Spring will come to see it and get a Bean or a Bean from here.

2. Notes are divided into two categories:

1. One is to use Bean, that is, to use the Bean that has been configured in the xml file to complete the assembly of properties and methods. For example, @ Autowired, @ Resource, you can obtain Bean through byTYPE (@ Autowired) and byNAME (@ Resource).

2. One is to register Bean,@Component, @ Repository, @ Controller, @ Service, @ Configration. These annotations convert the object you want to instantiate into a Bean and put it in the IoC container. When you need to use it, it will work with @ Autowired, @ Resource above to assemble the objects, properties and methods perfectly.

III. The definition of Bean

The element is the root element of the Spring configuration file, and the element is the child element of the element. The element can contain multiple child elements, and each element can define a Bean instance. Each Bean corresponds to a Java instance in the Spring container. You usually need to specify two attributes when defining Bean.

The Spring container centrally manages the instantiation of Bean, and Bean instances can be obtained through BeanFactory's getBean (Stringbeanid) method. BeanFactory is a factory, and the program only needs to get BeanFactory references to get references to all instances of Spring container management. The program does not need to be coupled with the implementation process of a specific example. In most Java EE applications, the Spring container is automatically created when the application is started, and the components are directly coupled by dependency injection, without even actively accessing the Spring container itself.

When we configure a Bean through a method in the configuration file, we need to have a no-parameter constructor in the Bean implementation class. Therefore, the bottom layer of Spring is equivalent to calling the following code:

Xxx = new xx.XxClass () if the Bean is created by construction injection in the configuration file:

Then Spring is equivalent to calling the following code:

Bean bean = new com.Test ("chenssy", "35-354"); IV. Scope of Bean in the container

When you create a Bean instance through the Spring container, you can not only instantiate the Bean instance, but also specify a specific scope for the Bean.

Spring supports 5 scopes:

1.Singleton: singleton mode. In the entire SpringIoC container, there will be only one instance of Bean defined with singleton.

2.Prototype: prototype pattern. Each time a prototype-defined Bean is obtained through the container's getBean method, a new Bean instance is generated.

3.request: for each HTTP request, the Bean defined with request produces a new instance, that is, a different Bean instance for each HTTP request. Of course, this scope is really effective only when Spring is used in WEB applications.

4.session: for each HTTPSession, a Bean defined with session will produce a new instance, that is, each HTTPSession will produce a different Bean instance. Like HTTP, it works only in WEB applications.

5.global session: each global HTTPSession corresponds to one Bean instance. It only works on portlet Context.

More commonly used singleton and prototype. If a Bean instance is set to singleton, the same instance will be obtained each time the Bean is requested. The container is responsible for tracking the state of the Bean instance and maintaining the lifecycle behavior of the Bean instance. If a Bean instance is set to prototype, each time the Bean,Spring requesting the di creates a new Bean instance to return to the program, in this case, the Spring container only uses the new keyword to create the Bean instance. Once created, the container will no longer track the instance and will not maintain the state of the Bean instance. If we do not specify the scope of the Bean, Spring will use the singleton scope by default. Java needs to make a memory request when creating a Java instance. Garbage collection needs to be completed when the instance is destroyed. All these tasks will lead to an increase in system overhead. Therefore, the creation and destruction of prototype scope Bean will be expensive. Once the Bean instance of singleton scope is created successfully, it can be reused. Therefore, unless necessary, try to avoid setting the scope of Bean to prototype.

At this point, the study of "introduction to the definition and scope of Bean in Spring" is over. I hope to be able to 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.

Share To

Internet Technology

Wechat

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

12
Report