1.Why learn Spring?
With continuous contact and understanding of Java EE, you will find that Spring plays an increasingly important role in various enterprises and projects. Mastering Spring has become one of the must-learn skills in our IT industry.
Spring Framework The current latest version is Spring Framework 5. When you open the official website, you should be able to see the promotional pictures of the official website.
There is a related news here that you are interested in. Click to view the original English version. Click to view the Chinese version.
According to the official website news and the information I know, the official website of Spring will continue to support Spring MVC because it has many improvements.
But I think the future trend will be Spring Boot+ SpringWeb Flux+ Spring Cloud .
So what is the difference between Spring MVC and Spring Web Flux?
The official website gives this comparison picture:
Translated below:
Summarize:
After seeing this, I believe that you are smart at this time, you should know why I said that before.
2. Introduction to Spring official website
Spring official website: https://spring.io/ Spring
Documentation: https://spring.io/guides
Spring IDE: https://spring.io/tools/sts
Spring Project: https://spring.io/projects
Project Quick Generator: https://start.spring.io/
I believe many people know the above links, but in fact, we often don’t know which link to use.
Spring official website: Follow the Spring official website news, and the latest Spring technology and version release announcements
Spring documentation: what you want to do? What kind of project do you want to develop? Related introductions and documentation can be found here quickly.
Spring IDE: If you plan to use the Eclipse version, then I recommend using the official website STS, because it should be the most friendly and supportive version of Spring. Of course, if the conditions are OK, I still highly recommend you to use Intellij Idea.
Spring Project: This is divided according to project modules, such as from configuration to security, web applications to big data, and you can learn which one you want to learn according to the classification.
Project Generator: This is a very convenient tool provided by Spring’s official website. Which dependencies are required and which version is required? You can configure it here and then download it.
3. Spring Framework
Spring Framework core supports dependency injection, transaction management, web applications, data access, messaging, testing and more
Tips: Here is the translation https://projects.spring.io/spring-framework/
3.1 Introduction
The Spring framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any type of deployment platform.
A key element of Spring is application-level infrastructure support: Spring focuses on the “pipeline” of enterprise applications so that teams can focus on application-level business logic without having to form unnecessary connections with specific deployment environments.
3.2 Functional Features
Tips: Here is a screenshot from the official website document, I believe it will help you understand better.
3.3 Minimum Requirements
Tips: So your computer now recommends using JDK1.8+
3.4 Quick Start
The recommended way to get started with spring-framework in your project is to use a dependency management system - the following code snippet can be copied and pasted into your build.
Need help? See our Getting Started Guide on Building with Maven and Gradle.
In fact, it is not just Spring official website. Most of our major companies today should also recommend that we use Maven and Gradle to manage project Jar package dependencies.
If you are using Maven:
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId> spring-context</artifactId> <version>5.1.0.BUILD-SNAPSHOT</version> </dependency></dependencies><repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/libs-snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository></repository>
If you're using Gradle
dependencies { compile 'org.springframework: spring-context:5.1.0.BUILD-SNAPSHOT'}repositories { maven { url 'https://repo.spring.io/libs-snapshot' }}Tips: Actually, I think Gradle should be a more advanced version dependency management tool than Maven. However, it seems that major companies don’t use Gradle many times. Perhaps it is because Eclipse’s support for Gradle is not as perfect as Intellij Idea.
The Spring framework contains many different modules. Here we show the spring-context that provides core functionality. For additional options, see the Getting Started Guide on the right.
Once you set up your build with the spring-context dependency, you can do the following:
After arriving here, the official website is not detailed, so add it.
Method 1: Use the STS tool to build this project with Spring-context context
Preparation:
Tips: There is a pitfall when downloading. If your JDK is 64-bit and the STS that is directly downloaded by default is 32-bit, this error will occur.
Therefore, when downloading, you must download the JDK matching version. Move: https://spring.io/tools/sts/all
After the download here is completed, we will right-click on the blank space of our IDE -> New -> Other...
Enter maven search, select Maven Project, and create a Maven project
Select the default workspace
Select the default type
Enter the basic project information and click Finish to complete it
Then we should be able to see the project structure like this
First modify pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xingyun</groupId> <artifactId>spring-context-sample</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-context-sample</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId> spring-context</artifactId> <version>5.1.0.BUILD-SNAPSHOT</version> </dependency> </dependency> </dependencies> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/libs-snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repository> </repository> </repository> </repository> </project>
Create a file
hello/MessageService.java
package com.xingyun.spring_context_sample.hello;public interface MessageService { String getMessage();}hello/MessagePrinter.java
package com.xingyun.spring_context_sample.hello;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Componentpublic class MessagePrinter { final private MessageService service; @Autowired public MessagePrinter(MessageService service) { this.service = service; } public void printMessage() { System.out.println(this.service.getMessage()); }}Tips: Pay attention to this annotation and don't forget
App.java
package com.xingyun.spring_context_sample;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.Annotation.AnnotationConfigApplicationContext;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import com.xingyun.spring_context_sample.hello.MessagePrinter;import com.xingyun.spring_context_sample.hello.MessageService;@Configuration@ComponentScanpublic class App { @Bean MessageService mockMessageService() { return new MessageService() { public String getMessage() { return "Hello World!"; } }; } public static void main( String[] args ) { ApplicationContext context = new AnnotationConfigApplicationContext(App.class); MessagePrinter printer = context.getBean(MessagePrinter.class); printMessage(); }}Tips: Note that there are two annotations on the class and one annotation on the method. Don’t forget. You can change the name of the class to the Application on the official website or retain the default App name.
The project structure should be like this after successful creation
Of course, you may feel uncomfortable when you look at the structure of this project, so you can also choose to change it.
The project structure becomes this:
Run App.main() main method
Project source code download: https://github.com/geekxingyun/JavaEE-Framework-Sample/tree/master/spring-context-sample
Appendix: Core Jar package dependencies
Tips: If you are not writing a java web application, you will not need the spring-web module.
GroupId | ArtifactId | Description |
org.springframework | spring-aop | Proxy-based AOP support |
org.springframework | spring-aspects | AspectJ based aspects |
org.springframework | spring-beans | Beans support, including Groovy |
org.springframework | spring-context | Application context runtime, including scheduled and remoting abstractions |
org.springframework | spring-context-support | Support classes for integrating common third-party libraries into a Spring application context |
org.springframework | spring-core | Core utilities, used by many other Spring modules |
org.springframework | spring-expression | Spring Expression Language (SpEL) |
org.springframework | spring-instrument | Instrumentation agent for JVM bootstrapping |
org.springframework | spring-instrument-tomcat | Instrumentation agent for Tomcat |
org.springframework | spring-jdbc | JDBC support package, including DataSource setup and JDBC access support |
org.springframework | spring-jms | JMS support package, including helper classes to send/receive JMS messages |
org.springframework | spring-messaging | Support for messaging architectures and protocols |
org.springframework | spring-orm | Object/Relational Mapping, including JPA and Hibernate support |
org.springframework | spring-oxm | Object/XML Mapping |
org.springframework | spring-test | Support for unit testing and integration testing Spring components |
org.springframework | spring-tx | Transaction infrastructure, including DAO support and JCA integration |
org.springframework | spring-web | Foundational web support, including web client and web-based remoting |
org.springframework | spring-webmvc | HTTP-based Model-View-Controller and REST endpoints for Servlet stacks |
org.springframework | spring-webmvc-portlet | MVC implementation to be used in a Portlet environment |
org.springframework | spring-websocket | WebSocket and SockJS infrastructure, including STOMP messaging support |
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.