In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what the concept of bean in Spring is. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Bean is one of the two core concepts in the Spring framework (the other is aspect-oriented programming AOP).
1 definition
The official Spring documentation interprets bean as:
In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.
The translation is:
In Spring, the objects that make up the backbone of the application and are managed by the Spring IoC container are called bean. Bean is an object that is instantiated, assembled, and managed by the Spring IoC container.
We summarize as follows:
1.bean is an object, one or more are unqualified
2.bean is managed by something called IoC in Spring
3. Our application is made up of bean.
2 inversion of control (IoC)
Control inversion English full name: Inversion of Control, abbreviated to IoC.
Control inversion realizes the loosely coupled relationship between objects by dependency injection (DI).
When the program is running, the dependent object is dynamically generated and injected into the dependent object by the helper, and the usage relationship between the two is dynamically bound.
The Spring IoC container is such a helper program, which is responsible for object generation and dependency injection, which will be handed over to us later.
In short, IoC is the process by which an object defines its dependencies without creating them.
Here we can subdivide it into two points.
2.1 Private attributes save dependencies
Point 1: save dependent objects using private properties, and can only be passed in through constructor arguments
The argument to the constructor can be a factory method, a property of a saved class object, or a value returned by a factory method.
Suppose we have a Computer class:
Public class Computer {private String cpu; / / CPU model private int ram; / / RAM size, unit GB public Computer (String cpu, int ram) {this.cpu = cpu; this.ram = ram;}}
We have another Person class that depends on the Computer class, which conforms to IoC as follows:
Public class Person {private Computer computer; public Person (Computer computer) {this.computer = computer;}}
The practices that do not comply with IoC are as follows:
/ / instantiate the Computer class public class Person {private Computer computer = new Computer ("AMD", 3) directly in Person;} / / pass the dependency public class Person {private Computer computer; public void init (Computer computer) {this.computer = computer;} 2.2 through [non-constructor] and let Spring control the class construction process.
Point 2: instead of using new, let Spring control the new process.
In Spring, we basically don't need to new a class, which is what Spring does.
When Spring starts, it instantiates the required class into an object, and if a dependency is needed, it instantiates the dependency first, and then the current class.
Because dependencies must be passed in through the builder, when instantiated, the current class receives and saves all dependent objects.
This step is called dependency injection.
2.3This is IoC
In Spring, instantiation of classes, instantiation of dependencies, and passing in dependencies are all controlled by the Spring Bean container
Instead of using new to instantiate objects and pass in dependencies through non-constructor methods.
The actual control has been handed over to the program management, not the programmer management, so it is called control inversion.
3 Bean?
As for bean, there are several concepts.
The conceptual 1:Bean container, or spring ioc container, is mainly used to manage objects and dependencies, as well as dependency injection.
The concept 2:bean is a Java object, and the class written according to the bean specification and the object generated by the bean container is a bean.
Conceptual 3:bean specification.
The bean specification is as follows:
1. All properties are private
two。 Provides a default construction method
3. Provide getter and setter
4. Implement the serializable interface
This is the end of this article on "what is the concept of bean in Spring". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.