Prior to the start
At the time of writing; Oneself actually very hesitant; Because of this Spring tutorial there are many tutorials available online. But I hope I can summarize in detail as far as possible at the bottom; The personal goal of the Spring tutorial is mainly low-level learning; Maybe this is a rough start; There may be some mistakes and I hope we can discuss them together.
Is Syria
Introduction to Spring:
Spring5.0 is currently available for the Spring FrameWork; The Spring framework is a full-stack application framework.
Spring features:
- Lightweight: The IoC container used by Spring is very lightweight compared to the EJB container, with the base version of the Spring framework only about 2MB. Spring enables developers to develop enterprise-level applications using only POJOs (Plain Old Java Objects, as opposed to EJBs). The advantage of this is that you don’t need a bloated EJB container (application server), you just need a lightweight servlet container (such as Tomcat). Especially in developments where memory and CPU resources are scarce, Spring is more resource-efficient than EJB in both developing and deploying applications.
- Inversion of Control (IOC) : Spring uses inversion of control technology to achieve loose coupling. Dependencies are injected into objects rather than creating or finding dependent objects. Aspect Oriented Programming (AOP) : Spring supports aspect oriented programming while separating the application’s business logic from the system’s services.
- MVC Framework: Spring MVC is a great MVC framework that can replace other Web frameworks such as Struts. Integration: Spring is easy to integrate with other popular frameworks, including ORM, Logging, JEE, Quartz, and presentation layer frameworks such as Struts.
- Transaction management: Spring’s powerful transaction management capabilities allow you to handle either local transactions (one database) or global transactions (multiple data, using JTA). Module separation: The Spring framework is made up of modules. There are already too many packages and classes, but they are grouped by module, and you only need to think about the module you will use, not the other modules.
- Exception handling: Due to Java’S JDBC, Hibernate, and other apis, there are many methods that throw checked exceptions, and many developers can’t handle exceptions very well. Spring provides a unified API for converting these Checked exception exceptions into Spring’s Unchecked exception.
- Unit testing: Spring code is easy to unit test, using Dependency Injection to inject test data into the program.
The history of the Spring
Direct article publishing has been updated to SpringFramwork5.0; If you want to see the features in other versions, I recommend the Spring History of releases and Today’s Eco-Empire blog post
- The JDK update to Spring Framework 5.0 was originally planned for release on Top of Java 9; Right now, the minimum requirement for the framework is Java 8.
- Core framework fixes based on new features introduced in Java 8:
- With reflection enhancements in Java 8, method parameters in Spring Framework 5.0 can be accessed more efficiently.
- The core Spring interface now provides alternative declarations built on Java 8’s default methods. with
- @nullable and @notnull annotations to display arguments and return values indicating nullability. This is enough to handle null values at compile time instead of throwing NullPointerExceptions at run time
Based on logging:
- Spring Framework 5.0 comes with the Commons Logging bridge module package, which is called Spring-JCL instead of standard Commons Logging. Of course, Log4j 2.x, SLF4J, JUL (java.util.logging) will also be automatically detected without any additional bridging.
- Core container Update
Spring Framework 5.0 now supports candidate component indexes as an alternative to classpath scanning. This feature has been added to the classpath scanner to simplify the step of adding candidate component identification. You can learn more about component indexes on Spring’s Jira
- Functional programming with Kotlin included
Support for the JetBrains Kotlin language was introduced. Kotlin is an object-oriented language that supports the functional programming style. Kotlin runs on the JVM, but the environment is not limited to the JVM.
I am not familiar with the Kotlin language; Don’t judge too much.
- Responsive programming model
One exciting feature of the Spring release is the new responsive stack WEB framework. This stack is fully responsive and non-blocking, suitable for event-looping style processing, and can be extended by a small number of threads.
A follow-up discussion on this topic; Spring WebFlux
- Testing improvement
Spring Framework 5.0 fully supports JUnit 5 Jupiter, so you can use JUnit 5 to write tests and extensions. A programming and extension model is also provided, and the Jupiter subproject provides a test engine to run Jupit-based tests on Spring. In addition, Spring Framework 5 provides extensions for parallel testing within the Spring TestContext Framework. For the responsive programming model, Spring-Test now also introduces support for WebTestClient integration tests that support Spring WebFlux, similar to MockMvc, without requiring a running server. Using a mock request or response, WebTestClient can be directly bound to the WebFlux server facility
- Library support
Spring Framework 5.0 currently supports the following versions of the upgrade libraries:
- Spring Framework 5.0 currently supports the following versions of the upgrade libraries:
- Jackson 2.6 +
- EhCache 2.10+ / 3.0 GA
- Hibernate 5.0 +
- JDBC 4.0 +
- XmlUnit 2.x+
- OkHttp 3.x+
- Netty 4.1 +
- Suspension support
At the API level, Spring Framework 5.0 no longer supports the following packages:
- beans.factory.access
- jdbc.support.nativejdbc
- Mock. Staticmock for the Spring-Aspects module
- Web.view.tiles2m. (Minimum required Tiles 3)
- The orm. Hibernate3 and orm. Hibernate4.
Hibernate 5 is currently the supported framework.
- Portlet.
- Velocity.
- JasperReports.
- XMLBeans.
- JDO.
- Guava. If you are using any of the above packages, it is recommended that you keep the Spring Framework version at 4.3.x.
Quick start
Spring officially recommends using SpringBoot to build applications for the Spring framework, but the focus here is on the content of the Spring framework; So the Spring framework is used here; Later, there is an opportunity to build SpringBoot tutorials and materials.
The development environment
- The eclipse or IDEA
- JDK8 (recommended) /7
- Spring Framwork5
start
-
Project to build Maven; The project catalog is as follows:
-
Helloworld with annotations:
Pom. XML file:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2. RELEASE</version>
</dependency>
</dependencies>
Copy the code
hello/MessageService.java
package hello.annotation;
public interface MessageService {
String getMessage(a);
}
Copy the code
hello/MessagePrinter.java
package hello.annotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MessagePrinter {
final private MessageService service;
@Autowired
public MessagePrinter(MessageService service) {
this.service = service;
}
public void printMessage(a) {
System.out.println(this.service.getMessage()); }}Copy the code
hello/Application.java
package hello.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
@Configuration
@ComponentScan
public class Application {
@Bean
MessageService mockMessageService(a) {
return new MessageService() {
public String getMessage(a) {
return "Hello World!"; }}; }public static void main(String[] args) {
ApplicationContext context =
newAnnotationConfigApplicationContext(Application.class); MessagePrinter printer = context.getBean(MessagePrinter.class); printer.printMessage(); }}Copy the code
- Use the XML file configuration: applicationContext.xml
<?xml version="1.0" encoding="UTF-8"? >
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="MessageService" class="hello.xml.MessageService">
<property name="message" value="Hello World!"/>
</bean>
</beans>
Copy the code
MessageService.java
package hello.xml;
public class MessageService {
private String message;
public String getMessage(a) {
return message;
}
public void setMessage(String message) {
this.message = message; }}Copy the code
Application.java
package hello.xml;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application {
public static void main(String[] args) {
ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml"); MessageService messageService = application.getBean(MessageService.class); System.out.println(messageService.getMessage()); }}Copy the code
4. Operating results:
Brief explanation:
- The configuration class initializes the ApplicationContet container based on the configuration file
- Inject Bean objects based on configuration (MessageService)
- Container instantiation dynamically creates Bean objects that actually do the above; The bottom layer uses the factory creation pattern; The Spring container creates unified Bean objects that manage Java; I’m going to talk more about this specific analysis.
References:
- Learn Spring 3(1) with me – Why learn Spring
- Official SPringle material
- The historical version of Spring changes and the current ecological empire
- New features in Spring Framework 5.0