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 are spring and springMVC?

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what is spring and springMVC". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is spring and springMVC"?

What is Spring and why do you want to learn spring?

When did you first touch the spring framework? I believe many people, like me, know spring for the first time not when they are working on a project, but when they see or hear about a framework called spring, which is known as structs and structs2 before the explosion, and has attracted a lot of people's attention.

So, is spring magical after all? according to records, the spring framework appeared in 2002, when people criticized the cumbersome EJB framework and did not like frameworks like structs. At this time, spring rescued a large number of Java developers from heaven.

Let's first take a look at Baidu Encyclopedia's definition of spring, the Spring framework is created because of the complexity of software development. Spring uses basic JavaBean to do things that previously could only be done by EJB. However, the use of Spring is not limited to server-side development. In terms of simplicity, testability and loose coupling, most Java applications can benefit from Spring.

◆ purpose: to solve the complexity of enterprise application development

◆ function: use basic JavaBean instead of EJB, and provide more enterprise application functions

◆ scope: any Java application

Spring is a lightweight control inversion (IoC) and aspect oriented (AOP) container framework.

The spring framework solves two long-standing pain points for programmers in Java development, that is, bean management and unified aspects, and spring's IOC and AOP just solve these two problems.

Cdn.bcebos.com/pic/4610b912c8fcc3cea4c69f4f9245d688d43f2043?x-bce-process=image/watermark,g_7,image_d2F0ZXIvYmFpa2U4MA==,xp_5,yp_5 ">

In fact, the content in spring ecology is rich and colorful. In addition to the modules mentioned in the above figure, spring has developed into springboot and springcloud, providing very high-quality solutions for back-end technology stacks such as micro-services.

Now that the spring framework has become a must for Java programmers, how to start learning the spring framework has become the topic we are going to discuss today.

First, let's take a look at how the basics of spring should be learned.

Spring Foundation

The core of spring is IOC and AOP. By understanding these two points, you can say that you have learned spring (but not).

First of all, let's take a look at the essence of IOC,IOC is to leave the management of bean to the framework. Spring maintains a bean container and manages all bean uniformly, so that all scenarios that need to use instances do not need to write tedious and repetitive instantiation code, but simply complete bean declaration and injection dependency.

Give me a simple example.

Without spring's IOC, it may be inconvenient for us to manage instances, such as the following code. We must instantiate userService and bookservice in the constructor and inject corresponding dependencies, otherwise it will not be able to initialize itself. We even have to instantiate it ourselves when using controller.

Class QueryController {UserService userService; BookService bookservice; QueryController () {userService = new UserService; userService.setUserDao (new UserDap ()); bookservice = new BookService; bookservice.setBookDao (new BookDao ());} public static void main (Strings [] args) {QueryController queryController = new QueryController ();}}

Once we use the spring framework, we can save a lot of things by taking advantage of its dependency injection capabilities. As shown in the following code.

@ Controllerclass QueryController {@ Autowired UserService userService; @ Autowired BookService bookservice;}

Of course, we also use the annotation functions provided by spring, such as @ Controller, which can declare a class as a bean container for controller,spring and instantiate it, and do special handling according to controller, while @

With the Autowired annotation, you can inject both userService and bookservice instances into QueryController, all because spring's IOC helps you implement bean management.

Of course, userService and bookservice themselves can also inject their corresponding dao layer dependencies (persistence layer) through IOC.

After talking about IOC, let's take a look at how AOP is implemented.

The essence of AOP is to use dynamic agents to complete the unified aspect function. With AOP, we can reuse some horizontal code of the same type, such as login interception, identity verification, security management, etc., which do not need to be embedded in the business code, but are often used, we can use AOP to make an aspect, and then specify the method that needs to be intercepted AOP adds the aspect code to the proxy object through a dynamic agent, so when you execute the business code, it is equivalent to executing the proxy object, and the aspect method is called accordingly.

One of the simplest dynamic proxy implementations is as follows:

Import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;public class Main {public static void main (String [] args) {InvocationHandler handler = new InvocationHandler () {@ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {System.out.println (method) If (method.getName () .equals ("morning")) {System.out.println ("Good morning," + args [0]);} return null;}} Hello hello = (Hello) Proxy.newProxyInstance (Hello.class.getClassLoader (), / / pass in ClassLoader new Class [] {Hello.class}, / / pass in the interface handler to be implemented); / / pass in the InvocationHandler hello.morning ("Bob") that processes the calling method;}} interface Hello {void morning (String name);}

The result will be output

Public abstract void test.Hello.morning (java.lang.String) Good morning, Bob

Where InvocationHandler is the proxy class code, and AOP is the opportunity dynamic proxy to achieve, you are free to play, in the proxy class to use the code you want.

In fact, in addition to IOC and AOP, there are many components and modules that we often use in spring, such as transactions, such as JDBC, and JMS (message service). These commonly used technical components are integrated into the spring framework, which is very convenient for us to use.

Springmvc

After talking about spring, it's time to talk about springmvc.

According to Baidu encyclopedia, Spring MVC belongs to the follow-up product of SpringFrameWork and has been integrated into Spring Web Flow. The Spring framework provides a full-featured MVC module for building Web applications. Use Spring pluggable MVC architecture, so when using Spring for WEB development, you can choose to use Spring's Spring MVC framework or integrate other MVC development frameworks, such as Struts1 (generally not used now), Struts 2 (generally used by old projects), and so on.

As a web developer, AOP and IOC of spring alone may not support you to develop a page. The purpose of springmvc is to help you achieve the most efficient web development and bid farewell to the structs framework.

To put it simply, springmvc provides a typical mvc development model, m, that is, the model layer, which is responsible for providing the data model and interacting with the database, while C represents controller, which is responsible for distributing and processing web requests, which refers to HTTP requests. The general controller will process the web requests corresponding to url and return the corresponding content, while in this part of the function of returning content, v, that is, the view layer, is required to provide. After all, the rendering of the page is a troublesome thing, and the returned object may be text, json, or a html page, and the view layer is used to handle this work.

The development model of MVC does solve many problems of web engineers, and springmvc, as the forerunner of the mvc framework, naturally becomes the overlord of Java.

With the development and change of Java technology, what Java can do now is far more than web development. Now many popular micro services and middleware are implemented with Java, and spring ecology will naturally develop. As a result, springboot and springcloud have also become the benchmark of open source frameworks in the industry.

At this point, I believe you have a deeper understanding of "what is spring and springMVC". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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