About MVC framework and spring

Introduction

MVC originally existed in desktop programs. M refers to business model, V refers to user interface, and C refers to controller. The purpose of using MVC is to separate the implementation code of M and V, so that the same program can use different Manifestation. For example, a batch of statistical data can be represented by histogram and pie chart respectively. The purpose of C is to ensure the synchronization of M and V. Once M changes, V should be updated synchronously.

MVC refers to a certain framework of the MVC pattern, which compulsorily separates the input, processing and output of the application. Applications using MVC are divided into three core components: model, view, and controller. They each handle their own tasks. The most typical MVC is the JSP + servlet + javabean model

The view is the interface that the user sees and interacts with. For old web applications, the view is an interface composed of HTML elements. In modern web applications, HTML still plays an important role in the view, but some new technologies have emerged in an endless stream, including Adobe Flash and Some markup languages ​​and Web services like XHTML, XML/XSL, WML, etc.

The model represents enterprise data and business rules. Among the three components of MVC, the model has the most processing tasks. For example, it may use component objects such as EJBs and ColdFusion Components to process the database. The data returned by the model is neutral, that is, the model has nothing to do with the data format. Such a model can provide data for multiple views. The code can be reused by multiple views only once, so the code duplication is reduced

The controller
accepts the user's input and calls the model and view to complete the user's needs, so when clicking a hyperlink in a Web page and sending an HTML form, the controller itself does not output anything or do any processing. It just receives the request and decides which model component to call to process the request, and then determines which view to use to display the returned data.

spring

Spring is a very good Java framework. Its goal is to simplify Java enterprise-level development. Spring has been out for more than ten years. During this period, it has been working on this goal. Springmvc, springboot, springcloud, these technologies are also around We are striving for the goal of simplifying development. In addition to spring cannot help us realize business logic code, spring tries to help us simplify other things. Using spring can help us save a lot of development time.

1. IOC inversion of control is a design concept. The active control right of object creation and assembly is handed over to the spring container. The control
action is reversed, which reduces the coupling degree of the system and facilitates system maintenance and expansion. , Mainly refers to the assembly
control of the objects that need to be used is reversed. It was done by yourself before, but now it is done by the spring container.
2. DI dependency injection means the way to set dependent objects when creating objects in the spring container. Some injection methods can make the system more flexible. For example, automatic injection can make the system very flexible
. 3. Spring container: Mainly responsible Operations such as creation, assembly, object search, and object life cycle management of objects in the container

Bean object

The objects managed by the spring container are collectively called Bean objects. Beans are ordinary java objects, which are actually the
same as our own new objects , but these objects are created and managed by spring. We need to tell the spring container which bean objects need to be created in the configuration file, so we need to configure it first The bean objects that need to be created are defined in the file. These configurations are collectively referred to as bean definition configuration metadata information. The spring container constructs and assembles the objects we need by reading these bean configuration metadata information.

Spring container use steps

  1. Introduce spring related maven configuration
  2. Create bean configuration files, such as bean xml configuration files
  3. Define the bean objects that need to be managed by the spring container in the bean xml file
  4. Create a spring container and specify the bean configuration files that need to be loaded for the container. When the spring container starts, these configuration files will be loaded, and then the bean objects defined in the configuration file will be created, and these objects will be placed in the container for use
  5. Get the object in the container through the method provided by the container, and then use
  6. The representative container in the spring container is the BeanFactory interface, which is the top-level interface of the spring container and provides the most basic functions of the container.

BeanFactory interface

Several commonly used methods
//Find the bean in the container by bean id or alias
Object getBean(String name) throws BeansException
//This is a generic method that searches for a specified type of bean according to the bean id or alias, and returns the specified type The bean object
T getBean(String name, Class requiredType) throws BeansException;
//Returns the bean object of the specified type in the container
T getBean(Class requiredType) throws BeansException;
//Get the obtainer of the specified type of bean object
ObjectProvider getBeanProvider(Class requiredType) ;

ApplicationContext interface

This interface inherits the BeanFactory interface, so it contains all the functions of BeanFactory inside, and has been extended on it, adding many enterprise-level functions, such as AOP, internationalization, event support, and so on.

ClassPathXmlApplicationContext类

This class implements the ApplicationContext interface. Note that the class name contains ClassPath Xml, indicating that this container class can load the bean xml configuration file from the classpath, and then create the bean object in the xml

AnnotationConfigApplicationContext类

This class also implements the ApplicationContext interface. The definition of beans supports xml and annotations. When we use annotations to define beans, we need to use this container to load, and the container will parse the annotations to build the build And manage the required beans.
The annotation method is more convenient than the xml method.

Guess you like

Origin blog.csdn.net/qq_41358574/article/details/111878764