01 preface
For a Java developer, Spring is very familiar. Whether it is the Spring framework, or the spring-led IOC and AOP style, it has a profound impact on subsequent Java development. At the same time, the Spring community can always respond to the needs of developers in time, and launch new features to adapt to the trend of development. For most developers, Spring MVC and Spring Boot are usually the most exposed. This paper will give a general overview of Spring, Spring MVC and Spring Boot respectively, and analyze and elaborate the problems they want to solve. So that beginners can better understand Spring and related concepts
2 Structure
This paper will be roughly divided into the following four parts
What is Spring? What problem does it solve?
What is Spring MVC? What problem does it solve?
What is Spring Boot? What problem does it solve?
Spring, Spring MVC, and Spring Boot
3 What is Spring? What problem does it solve?
When we say Spring, we generally refer to the Spring Framework, which is an open source application Framework that provides an easy way to develop without the large number of business/tool objects that can clutter your code. In more general terms, the framework manages these objects for you, including their creation, destruction, etc. For example, beans are commonly seen in Spring-based projects, which represent objects governed by Spring.
Spring Bridges the use of managed objects and business logic through IOC (Inversion of Control). IOC can also be regarded as the core and most important idea of Spring. What benefits can be gained from IOC? Let’s first look at a typical application scenario in actual development. Suppose we have an application based on MVC hierarchical structure, which provides interfaces through the Controller layer and implementation through the Service layer. In the Service layer, there is a WelcomeService service interface. WelcomeService service = new WelcomeServiceImpl() Create an instance and call:
public class WelcomeController { private WelcomeService service = new WelcomeServiceImpl(); @RequestMapping(“/welcome”) public String welcome() { return service.retrieveWelcomeMessage(); }}
The WelcomeServiceImpl is a MockWelcomeServiceImpl for testing purposes because the actual application environment is different from the test environment. There is no other option but to change the code:
public class WelcomeController { private WelcomeService service = new MockWelcomeServiceImpl(); . }
Testing OK and then changing the code back is too cumbersome and intrusive;
Here’s how to implement WelcomeService via Spring’s IOC, starting with Spring management:
<beanname=”WelcomeService”class=”XXX.XXX.XXX.service.impl.WelcomeServiceImpl”/>
Then get the object in the business code via Spring IOC:
public class WelcomeController { @Autowired private WelcomeService service; @RequestMapping(“/welcome”) public String welcome() { return service.retrieveWelcomeMessage(); }}
To test, simply change the configuration file and change the WelcomeService implementation to MockWelcomeServiceImpl:
<beanname=”WelcomeService”class=”XXX.XXX.XXX.service.impl.MockWelcomeServiceImpl”/>
This approach does not intrude on business code, it effectively implements loose coupling, which everyone knows is a nightmare for business development; At the same time, Spring IOC offers much more, such as reducing the creation of useless objects through singletons, optimizing initialization costs through lazy loading, and so on
Of course, Spring’s core features are far from obvious, such as:
Spring AOP
Spring JDBC
Spring MVC
Spring ORM
Spring JMS
Spring Test
It is still possible to implement these features without the Spring framework, but Spring provides a more elegant abstract interface to facilitate the assembly of these features, while giving each implementation flexibility in configuration; In addition, based on Spring, you can easily integrate with other frameworks, such as Hibernate, IBatis, etc. The official principle of Spring is never to duplicate the wheel, a good solution only needs to integrate through Spring. If you look at the Spring structure, you’ll find that the Spring Framework itself doesn’t offer much in the way of specific functionality. It focuses on making your project code more elegant, making it extremely flexible and extensible, while integrating industry-leading solutions through Spring. See the Tiny Spring project for the core implementation mechanism of Spring.
4 What is Spring MVC? What problem does it solve?
Spring MVC is a part of Spring. After Spring came out, we thought it was easy to use, so we designed an MVC framework (some components decoupled by Spring) according to this pattern, which is mainly used for developing WEB applications and network interfaces. It is a module of Spring. With Dispatcher servlets, ModelAndView and View Resolver, application development is easy. A typical Spring MVC application development is divided into the following steps:
First declare the Dispatcher Servlet through the configuration file:
springmvc com.qgd.oms.web.common.mvc.OmsDispatcherServlet contextConfigLocation /WEB-INF/applicationContext.xml 1 springmvc /
Declare servlet details, such as MVC Resource, data source, bean, etc., through configuration files
<mvc:resources mapping=”/css//*” location=”/static/css/” cache-period=”21600″/> <mvc:resources mapping=”/js//” location=”/static/js/” cache-period=”21600″/> <mvc:resources mapping=”/views/**/.html” location=”/static/views/” cache-period=”21600″/> <mvc:resources mapping=”/fonts//*” location=”/static/fonts/”/> <mvc:resources mapping=”/ueditor//” location=”/static/js/lib/ueditor/”/> <mvc:resources mapping=”/img/**/” location=”/static/img/”/>
If you need to add other functions, such as security, you need to add corresponding configurations:
Add business code, such as Controller, service, model, etc., and finally generate a WAR package and start it through the container.
05
What is Spring Boot? What problem does it solve?
In the early stage, Spring provided good flexibility and expansibility for the project in the form of code and configuration. However, as Spring became larger and larger, its configuration files became more and more complicated, and too many complex XML files have always been criticized by Spring, especially in recent years when other simple WEB solutions emerged one after another. For example, based on Python or Node.js, a few lines of code can implement a WEB server, by contrast, people gradually feel that the Spring set is too cumbersome, at this time, the Spring community launched Spring Boot, which aims to achieve automatic configuration, reduce the complexity of project construction. If you need to set up an interface service, a few lines of code can be implemented through Spring Boot. Please see the code example:
// Introduce the spring-boot-starter-web dependency
org.springframework.boot spring-boot-starter-web
// Declare the Spring Boot application and write the service logic directly
@Controller@SpringBootApplicationpublic class MockServerApplication { @RequestMapping(“/hi”) @ResponseBody String home() { return “how are you!” ; } public static void main(String[] args) { SpringApplication.run(MockServerApplication.class, args); }}
You don’t even need an extra WEB container, just generate jar packages to execute, because the Spring-boot-starter-Web module includes a built-in Tomcat that can be used directly by the container; Based on Spring Boot, it does not mean that the original configuration is gone, but Spring Boot has a default configuration. We can regard it as a relatively general convention, and Spring Boot follows the principle of convention over configuration. At the same time, If you need to use a variety of complex but powerful configuration features Spring has traditionally provided, Spring Boot also supports them
In Spring Boot, you’ll find that all packages you introduce are in starter form, such as:
Spring-boot-starter-web-services, for SOAP Web services
Spring-boot-starter-web: applies to Web applications and network interfaces
Spring-boot-starter-jdbc, for JDBC
Spring-boot-starter-data-jpa, a hibernate-based persistence layer framework
Spring-boot-starter-cache, for cache support
, etc.
Spring Boot explains starter as follows:
Starters are a set of convenient dependency descriptors that you can include in your application. You get a one-stop-shop for all the Spring and related technology that you need, without having to hunt through sample code and copy paste loads of dependency descriptors. For example, if you want to get started using Spring and JPA for database access, just include the spring-boot-starter-data-jpa dependency in your project, and you are good to go
The translation of this sentence is:
By including Starters in your project, you can go one stop and get exactly what you need, instead of having to copy sample configurations and code and then debug them and really go straight out of the box. For example, if you want to use Spring JPA for data manipulation, just introduce spring-boot-starter-data-jPA into your project dependencies
06
Spring, Spring MVC, and Spring Boot
As many readers know by now, the three focus on different areas and solve different problems; In general, Spring is like a big family, with many derivative products such as Boot, Security, JPA and so on. But their foundation is Spring’s IOC, which provides a dependency injection container, and AOP, which addresses section-oriented programming and then builds on that foundation to implement the advanced functions of other derivatives; Spring MVC is an MVC framework based on servlets, mainly to solve the problems of WEB development, because the configuration of Spring is very complex, various XML and properties processing is cumbersome. Therefore, in order to simplify the use of developers, the Spring community creatively launched the Spring Boot, which follows convention over configuration, greatly reducing the barrier to use Spring, but without sacrificing the original flexible and powerful functions of Spring. Here is a diagram to illustrate the relationship between the three:
Spring.png
Spring MVC and Spring Boot both belong to Spring. Spring MVC is an MVC framework based on Spring, while Spring Boot is a set of rapid development integration packages based on Spring.
PDF, covering THE JVM, locking, high concurrency, reflection, Mybatis, Spring principle, micro services, Zookeeper, database, data structure, etc., please pay attention to the private message this number: Java can be obtained!