First, SpringBoot entry

1. Basic introduction

  • To simplify theSpringApplication development framework, the wholeSpringA big consolidation of technology stacks;
  • J2EEOne-stop solution for development;

Advantages:

  • Quickly create a standalone runningSpringProjects and integration with mainstream frameworks;
  • Using embeddedServletContainer, application does not need to be typedWARThe package;
  • startersAutomatic dependencies and version control;
  • A lot of automatic configuration, easy development, also can change the default values;
  • Don’t need to configureXML, no code generation, out of the box;
  • Run time application monitoring for quasi-production environment;
  • Natural integration with cloud computing;

2. Micro services

  • Martin Fowler in his paper.
  • Microservices: Architectural Style (Service Miniaturization)
  • An application should be a set of small services; Can be achieved byHTTPTo communicate with each other;
  • Single application:ALL IN ONE
  • Microservices: Each functional element is ultimately a unit of software that can be independently replaced and upgraded;

3. MAVEN Settings

Add the following code to the Profiles tag in Maven’s settings. XML configuration file:

< profile > < id > JDK 1.8 < / id > < activation > < activeByDefault >true</activeByDefault> < JDK >1.8</ JDK > </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> < maven.com piler target > 1.8 < / maven.com piler. Target > < maven.compiler.com pilerVersion > 1.8 < / maven.compiler.com pilerVersion > </properties> </profile>Copy the code

Maven uses JDk1.8.

Spring Boot HelloWorld

Functions:

Browser send
helloRequest, the server accepts the request, processes it, responds
Hello Springboot! The string; Browser input
localhost:8080/helloYou can see the browser display
Hello SpringBoot!The string;

① Create amavenEngineering (jar);

(2) the importspring bootRelated dependencies;

"Parent" > < groupId > org. Springframework. Boot < / groupId > < artifactId > spring ‐ boot ‐ starter ‐ parent < / artifactId > <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> The < artifactId > spring ‐ boot ‐ starter ‐ web < / artifactId > < / dependency > < / dependencies >Copy the code

③ Write a main program and startSpring Bootapplication

/** * @springBootApplication to annotate a main program class, Application shows that this is a Spring Boot * / @ SpringBootApplication public class HelloWorldMainApplication {public static void main (String [] The args) {/ / Spring application start up SpringApplication. Run (HelloWorldMainApplication. Class, args); }}Copy the code

④ Write relevantController

@Controller
public class HelloController {

    @ResponseBody
    @RequestMapping("/hello")
    public String hello() {return "Hello SpringBoot!"; }}Copy the code

⑤ Run the main program test

⑥ Simplify deployment

Jar the application into a jar package, directly using the java-jar command to execute;

<! -- This plugin can package the application into an executable JAR package; --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>Copy the code

Results:

5, Hello World

(1) Pom. XML file

The parent project

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> < version > 1.5.9. RELEASE < / version > < / parent >Copy the code

His father project:

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> < version > 1.5.9. RELEASE < / version > < relativePath >). /.. /spring-boot-dependencies</relativePath> </parent>Copy the code

He’s really in chargeSpring BootAll dependent versions within the application;

That isSpring BootVersion arbitration center;

By default, we don’t need to write versions to import dependencies; (Dependencies that are not managed in Dependencies naturally require version declarations.)

Starter:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>Copy the code

Spring – the boot – starter – web:

  • spring-boot-starter:spring-bootScenario initiator; They imported it for uswebComponents on which a module depends for its proper operation;

Spring Boot extracts all of the functional scenarios into one specific starter, simply by introducing all of the dependencies related to those starter scenarios into the project. Import the scenario initiator for whatever functionality you want to use.

(2) Main program class, main entry class

@ SpringBootApplication public class HelloWorldMainApplication {public static void main (String [] args) {/ / Spring up  SpringApplication.run(HelloWorldMainApplication.class,args); }}Copy the code

SpringBootApplication: The SpringBoot application annotation indicates that this class is the main configuration class of SpringBoot, SpringBoot should run the main method of this class to start the SpringBoot application; This is a composite annotation.

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
	...
}Copy the code

Here’s what each note means:

  • @SpringBootConfiguration: Spring BootAnnotated on a class to indicate that this is a configuration classSpring BootConfiguration class, which contains this@Configuration(i.e.SpringConfig classes);
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}Copy the code

Config class — > Config File; The configuration class is also a Component in the container: @Component

@target ({elementtype.type}) @Retention(RetentionPolicy.runtime) @documented @Component // Component annotation @Interface Configuration { @AliasFor( annotation = Component.class ) String value() default"";
}Copy the code

② @enableAutoConfiguration: Enables the automatic configuration function.

Spring Boot automatically configured things we needed to configure before; @enableAutoConfiguration tells SpringBoot to enable automatic configuration so that the automatic configuration takes effect.

@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
}Copy the code

  • @AutoConfigurationPackage: Automatic configuration package
    • @Import(AutoConfigurationPackages.Registrar.class):SpringThe underlying annotation of@ImportImport a component into the container. Components imported byAutoConfigurationPackages.Registrar.classSpecified. The main configuration class (@SpringBootApplicationAnnotated class)All components in the package and all subpackages below are scanned to the Spring container; So if the top onecontroller If it is not in the same package (or subpackage) as the main configuration class, it cannot be scanned.


  • @Import(EnableAutoConfigurationImportSelector.class): Import components into containers (not under the same package)EnableAutoConfigurationImportSelector: selector for which components to import; Return all components that need to be imported with full class names, and they will be added to the container; A large number of auto-configuration classes are imported into the container (xxxAutoConfiguration); Import and configure all the components needed for the scenario into the container;

With the automatic configuration class, we do not need to manually write configuration injection function components; The inside of the getCandidateConfigurations calls one way below: SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader)Spring The bootloader obtains the values specified by EnableAutoConfiguration from the meta-INF/Spring. factories under the classpar and imports the values into the container. Before we need to configure things ourselves, automatic configuration classes help us;

The entire J2EE integration solution and automatic configuration are in spring-boot-autoconfigure-xxx. release.jar;


6. Use Spring Initializer to quickly create a Spring Boot project

Both ides support the quick creation of a Spring Boot project using Spring’s project creation wizard. Select the module we need, the wizard will network to create a Spring Boot project, the default generated Spring Boot project; With the main program generated, we just need our own logic.

  • resources: Directory structure in a folder
    • static: Saves all static resources;js css images;
    • templatesSave all template pages. (Spring Boot defaultjarPackages use embeddedTomcatIs not supported by defaultJSPPage); You can use a template engine (freemarker,thymeleaf);
    • application.properties: Spring Boot application configuration file, you can modify some default Settings;



Structure directory:


Simple Controller, notice the @restController annotation.

package com.zxin.springboot.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; // @responseBody // All methods of this class return data directly to the browser (if it is an object -> convert to JSON) // @controller@restController // This annotation does the same thing as the previous two annotations (i.e Public class HelloController {@responseBody // If each class needs to write, please ask @requestMapping ("/hello")
    public String hello() {return "hello quick SpringBoot!"; }}Copy the code

Original: Java Architecture Notes