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

What is spring IOC and what are the design patterns?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what is spring IOC and what design patterns it has". In daily operation, I believe many people have doubts about what spring IOC is and what design patterns it has. 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 "what is spring IOC and what design patterns are there?" Next, please follow the editor to study!

SpringIOC and Design patterns I. the concept of IOC:

Inversion of control and dependency injection are actually the same concept. When a method needs the assistance of another object, the traditional method is to have a caller to create an instance of the callee through new, but the task of creating the callee in spring is no longer done by the caller, which is called ioc. The job of creating the callee is done by spring, which is then injected into the caller, which becomes dependency injection.

The purpose of this, of course, is to decouple and reduce the coupling between classes, and the design idea is to get the factory pattern in the design pattern. When the spring container starts, spring initializes all the configured bean in the configuration item. When the call is needed, the initialized bean is assigned to the calling class without the need to manually create an object instance.

For springIOC, there are two most important places, one is to create the bean container, and the other is to initialize the bean.

2. Top-level interface of SpringIOC:

As an interface class at the top level in the architecture of Sping IoC, BeanFactory defines the basic functional specification of the IoC container. In order to distinguish between the transfer of objects and the transformation of objects in Spring, and to restrict the data access of objects, a multi-layer interface ListableBeanFactory interface is used to indicate that these Bean can be listed. What HierarchicalBeanFactory means is that these Bean are inherited, that is, each Bean may have a parent Bean. The AutowireCapableBeanFactory interface defines the automatic assembly rules for Bean.

The default implementation class is DefaultListableBeanFactory, which implements all interfaces.

There are four design patterns used in springIOC: factory pattern, singleton pattern, strategy pattern, and decorator pattern. one。 Factory model

The Spring IOC container is like a factory. When we need to create an object, we only need to configure the configuration file / annotations, regardless of how the object is created. The IOC container is responsible for creating objects, connecting objects together, configuring them, and processing the entire lifecycle of these objects from creation (multiple object spring does not manage the lifecycle) until they are completely destroyed.

Spring uses factory mode to create bean objects through BeanFactory or ApplicationContext.

BeanFactory: delay injection (BeanFactory defaults to lazy loading, which is only injected when a certain bean is used, and can be set to non-lazy loading through @ Lazy (false)). It takes up less memory than ApplicationContext.

ApplicationContext: when the container is started, whether it is used or not, all the bean,ApplicationContext created at one time inherits BeanFactory, and there are more functions in addition to the functions of BeanFactory, so it is generally used more.

Two. singleton mode

The default scope of bean in Spring is singleton (singleton). There are two ways to implement a singleton in Spring:

Xml: note: @ Scope (value = "singleton")

In addition to singleton scopes, bean in Spring has the following scopes:

Prototype: a new bean instance is created for each request.

Request: each HTTP request generates a new bean, which is valid only within the current HTTP request.

Session: each HTTP request generates a new bean, which is valid only within the current HTTP session.

Global-session: global session scope, which only makes sense in portlet-based web applications, Spring5 is no longer available. Portlet is a small Java Web plug-in that can generate fragments of semantic code (for example: HTML). They are based on portlet containers and can handle HTTP requests like servlet. However, unlike servlet, each portlet has a different session.

three。 Strategy mode

Obtain instances of Resource through ApplicationContext in spring, including different resource types such as urlResource,classPathResource,fileSystenResource. Spring adopts different access policies for different resource types. ApplicationContext will be responsible for selecting the implementation class of Resource, that is, determining the specific resource access policy, thus separating the application from the specific resource access policy.

four。 Decorator mode

The decorator pattern can dynamically add additional properties or behaviors to the object. The decorator pattern is more flexible than using inheritance. To put it simply, when we need to modify the original function, but we do not want to modify the original code directly, we design a Decorator to cover the original code. In fact, in JDK, there are many places to use the decorator mode, such as the InputStream family, the InputStream class has FileInputStream (read files), BufferedInputStream (increase cache, greatly improve the speed of reading files) and other subclasses extend its function without modifying the InputStream code.

Classes in Spring that have Wrapper are wrapper classes.

At this point, the study of "what is spring IOC and what design patterns there are" 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

Development

Wechat

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

12
Report