Java development learning experience (1): SSM environment construction

Posted Jun 15, 20207 min read

There is a little development foundation of .NET. I have learned basic grammar in school and I am a little interested in JAVA. I simply learned a bit and recorded the aspects from which I started. I don t plan to go deep into the principles for now. So some places may be careless and accurate.

1 SSM framework

From the online discussion, the SSM framework seems to be slowly being replaced by Spring Cloud.

1.1 Spring Framework

Spring is an open source Java/Java EE full-function stack(full-stack) application framework. It is released in the form of an Apache License 2.0 open source license agreement and also has a ported version on the .NET platform. The framework is based on the code in the book Expert One-on-One Java EE Design and Development(ISBN 0-7645-4385-7), originally developed by Rod Johnson and Juergen Hoeller.

The Spring Framework provides an easy development method. This development method will avoid a large number of property files and helpers that may cause the underlying code to become complicated and confusing. In simple terms, Spring is a lightweight Control Inversion(IoC) and Aspect-oriented(AOP) container framework.
If you learn JAVA Spring, these two things should be inevitable, but for the time being, you don t have a deep understanding, and I plan to go deeper into the code implementation level and compare it with .NET.

Inversion of Control IoC(Inversion of Control)

Before the emergence of IoC, the coordination relationship between components was controlled by the internal code of the program, or in the past, we used the New keyword to achieve the dependency relationship between the two groups.
This approach causes mutual coupling between components. IoC(Control Inversion) is to solve this problem, it will realize the relationship between the components from the program to the external container to manage.
In other words, the container dynamically injects certain dependencies between components into components during runtime. **Dependency Injection(Dependency Injection):This is DI. Literally understand, Dependency Injection is to inject the service to the place where it is used. The object only provides a common method for the container to determine the dependency relationship. The container is solely responsible for the assembly of the component. It will pass the object that meets the dependency relationship to the required object through the property(setter in JavaBean) or the constructor.

Compared with IoC, Dependency Injection(DI) more accurately describes the design concept of IoC. The so-called dependency injection, that is, the dependency relationship between components is determined by the container during the application system runtime, that is, the container dynamically injects a target object instance of a certain dependency relationship into each associated component in the application system.

Spring core ideas, detailed explanation of IoC and DI

AOP(aspect-oriented programming)

POP oriented process programming

OOP object-oriented programming

Microservices

Everything you should know about Spring AOP(AspectJ)

1.2 Spring MVC

Spring MVC is a follow-up product of SpringFrameWork and has been integrated in Spring Web Flow. Spring MVC separates the roles of controller, model object, dispatcher, and handler object. This separation makes them easier to customize.

1.2.1 spring boot

Spring Boot is Spring, and it does the Spring Bean configuration that you would do without it. It uses the "habit is better than configuration"(a large number of configurations exist in the project, in addition to a built-in configuration, so you do not need to manually configure) concept to make your project run quickly. It is easy to create a stand-alone running(running jar, embedded Servlet container), quasi-production-level Spring framework-based project using Spring Boot. With Spring Boot, you can use little or no Spring configuration.

Normal Spring MVC build needs
  • A project structure, which contains a Maven or Gradle build file containing the necessary dependencies, at least the dependencies of Spring MVC and Servlet API.
  • A web.xml file(or a WebApplicationInitializer implementation) in which Spring's DispatcherServlet is declared.
  • A Spring configuration with Spring MVC enabled
  • A controller class that responds to HTTP requests with "hello World".
  • A web application server for deploying applications, such as Tomcat.
spring boot features
  • Automatic configuration:For common application functions of many Spring applications, Spring Boot can automatically provide related configurations
  • Starting dependency:tell Spring Boot what functions are needed, it can introduce the required libraries.
  • Command line interface:This is an optional feature of Spring Boot, by which you only need to write code to complete the complete application, without the need for traditional project construction.
  • Actuator:allows you to go deep into the running Spring Boot application, a set.
One of the ways to use Spring Boot:Spring Initializr

enter image description here

  • IDEA
  1. Currently IDEA only supports Java 8 JDK

2.Apache Maven is a software(especially Java software) project management and automatic build tool provided by the Apache Software Foundation. Based on the concept of the Project Object Model(POM), Maven uses a central piece of information to manage the steps(life cycle) of a project's construction, reporting, and documentation.

  1. Select the required dependencies

  1. pom.xml

    4.0.0
     <groupId>com.example</groupId>
     <artifactId>demo</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <packaging>jar</packaging>
    
     <name>demo</name>
     <description>Demo project for Spring Boot</description>
    
     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>2.1.0.RELEASE</version>
         <relativePath/> <!-- lookup parent from repository -->
     </parent>
    
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
         <java.version>1.8</java.version>
     </properties>
     <!-- Add classpath dependency -->
     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
         <!-- Developer tools, when there is a file update on the classpath, the application restart is automatically triggered -->
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
         </dependency>
     </dependencies>
     <!-- maven compiler plugin for creating executable jar packages -->
     <build>
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
         </plugins>
     </build>
Three ways to build and start
  1. Start the main function in the IDE(or java in the command line tool). Maven is usually included in the IDE, which can help us download and install Maven dependencies.

  1. Run the mvn spring-boot:run command, but this method requires you to install Maven in the local environment

There are two ways to use the command line, one is to directly find the spring boot startup command in the Maven project view and run it directly


The other is to enter the command directly

  1. Use the mvn package command to package and generate a JAR file that can be run directly. The path is the target directory in the project file. You can run it directly using the "java -jar" command.

No matter which method is used, you can start the project, and then visit http://localhost :8080 to open the website to see the contents of the controller

Application entrance
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class DemoApplication {
    @RequestMapping("/")
    public String index(){
        return "Hello Spring Boot";
    }
    public static void main(String[]args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

Modify the class where the main function is located, and be able to receive Http requests and return content.

  • The main() method starts an HTTP server program, which by default listens on port 8080 and forwards the HTTP request to our application for processing
  • The @Controller annotation indicates that the Application class is a controller that handles HTTP requests. All the methods annotated with @RequestMapping in this class will be used to process the corresponding URL requests. The @ResponseBody annotation tells Spring MVC to directly use the string as a Web response(Reponse Body) returns, if the method marked by @ResponseBody returns an object, the object will be automatically converted into a JSON string and returned
  • The index() method includes @GetMapping("/") annotation, which means to visit http://localhost :8080/in the browser(without considering the protocol, host and port information The path is "/") After the request sent by the browser will be handed over to this method for processing
  • The index() method directly returns a string, which is equivalent to directly returning the string "Hello World" to the browser as a response to the HTTP request, so we can see the corresponding return value in the browser
  • A Spring configuration with Spring MVC enabled
  • A controller class that responds to HTTP requests with "hello World".
  • A web application server for deploying applications, such as Tomcat.