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

Example Analysis of implementing Bean Container

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

The implementation of Bean container example analysis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

What is the Spring Bean container?

Spring contains and manages the configuration and lifecycle of application objects, in this sense it is a container for hosting objects, you can configure how each of your Bean objects is created, these Bean can create a separate instance or generate a new instance each time it is needed, and how they are built and used interrelated.

If a Bean object is managed by a Spring container, then the Bean object should be disassembled and stored in the definition of Bean in a part-like manner, which is equivalent to an operation to decouple the object, which can be more easily managed by Spring, like dealing with circular dependencies and other operations.

When a Bean object is defined and stored, and then assembled by Spring, this process includes Bean initialization, attribute filling, etc., and finally we can use a Bean instantiated object completely.

The goal of our case in this chapter is to define a simple Spring container for defining, storing, and fetching Bean objects.

Second, design

Any specific data structure implementation that can store data can be called a container. For example: ArrayList, LinkedList, HashSet, etc., but in the case of Spring Bean container, we need a data structure that can be used for storage and name indexing, so HashMap is the most appropriate choice.

This paper briefly introduces that HashMap,HashMap is a zipper addressing data structure based on disturbance function, load factor, red-black tree conversion and other technical contents, which can make the data more hashed on the hash bucket and the linked list and red-black tree formed during collision. Its data structure will try its best to make the complexity of reading the whole data between O (1) ~ O (Logn) ~ O (n). Of course, in extreme cases, there will be more data in O (n) linked list. However, after the 100000 data disturbance function re-addressing verification test, the data will be evenly hashed on each hash bucket index, so HashMap is very suitable for the container implementation of Spring Bean.

Another simple Spring Bean container implementation requires three basic steps of Bean definition, registration and acquisition. The simplified design is as follows

Definition: BeanDefinition, maybe this is a class you often see when looking up the Spring source code, for example, it will include singleton, prototype, BeanClassName and so on. But at present, our preliminary implementation will be easier to deal with, defining only one Object type for storing objects.

Registration: this process is equivalent to storing data in HashMap, but now HashMap stores the object information of the defined Bean.

Get: the last step is to get the object. The name of Bean is key,Spring container. After initializing the Bean, you can get it directly.

Next we will follow this design and do a simple Spring Bean container code implementation. The coding process is often less complex, but it is more important to know the design process!

III. Realization

1. Engineering structure

Small-spring-step-01 └── src ├── main │ └── java │ └── cn.bugstack.springframework │ ├── BeanDefinition.java │ └── BeanFactory.java └── test └── java └── cn.bugstack.springframework.test ├── bean │ └── UserService.java └── ApiTest.java

Project source code: https://github.com/small-spring/small-spring-step-01 (official account: bugstack wormhole stack, reply: Spring column, get the whole source code)

Spring Bean container class relationship, as shown in figure 2-2

Figure 2-2

The whole implementation of the Spring Bean container is very simple, and it only includes a simple BeanFactory and BeanDefinition, where the class name is the same as that in the Spring source code, but the current class implementation will be relatively simpler and will continue to add content in the subsequent implementation process.

BeanDefinition, used to define Bean instantiation information, is now implemented as an Object to store objects

BeanFactory, which represents the factory of Bean objects, can store Bean definitions in Map and retrieve them.

2. Bean definition

Public class BeanDefinition {private Object bean; public BeanDefinition (Object bean) {this.bean = bean;} public Object getBean () {return bean;}}

In the current Bean definition, there is only one Object for storing Bean objects. If you are interested, you can refer to the information of this class in the Spring source code. The names are all the same.

However, later implementations will gradually improve the filling of BeanDefinition-related properties, such as SCOPE_SINGLETON, SCOPE_PROTOTYPE, ROLE_APPLICATION, ROLE_SUPPORT, ROLE_INFRASTRUCTURE, and Bean Class information.

3. Bean Factory

Public class BeanFactory {private Map beanDefinitionMap = new ConcurrentHashMap (); public Object getBean (String name) {return beanDefinitionMap.get (name). GetBean ();} public void registerBeanDefinition (String name, BeanDefinition beanDefinition) {beanDefinitionMap.put (name, beanDefinition);}}

The registration of Bean is included in the implementation of the Bean factory, where the definition information of Bean is registered. The operation to get Bean is also included in this class.

At present, BeanFactory is still a very simplified implementation, but this simplified implementation is also the final result of the use of Bean in the entire Spring container, but the implementation process only shows the basic core principles. This will continue to grow in subsequent supplementary implementations.

IV. Testing

1. Prepare in advance

Public class UserService {public void queryUserInfo () {System.out.println ("query user information");}}

A UserService object is simply defined here so that we can test the Spring container later.

two。 Test case

Test public void test_BeanFactory () {/ / 1. Initialize BeanFactory BeanFactory beanFactory = new BeanFactory (); / / 2. Register bean BeanDefinition beanDefinition = new BeanDefinition (new UserService ()); beanFactory.registerBeanDefinition ("userService", beanDefinition); / / 3. Get bean UserService userService = (UserService) beanFactory.getBean ("userService"); userService.queryUserInfo ();}

In the single test, it mainly includes three steps: initializing the Bean factory, registering Bean and obtaining Bean, which is close to Spring in effect, but it will be more simplified.

In the registration of Bean, the instantiated UserService is directly passed to BeanDefinition as an input parameter. In the subsequent implementation, we will put this part into the Bean factory to implement.

3. Test result

Query user information Process finished with exit code 0

As can be seen from the test results, the current Spring Bean container case has taken shape a little.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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