A, Spring Boot entry
1.Spring Boot Profile
A simplified framework for Spring application development
Spring a large integration of the entire technology stack
J2EE development of one-stop solution
2. Micro Services
2014,martin fowler
Micro services: architecture style
An application should be a set of small service; can communicate by way of Http
Each functional element is an independent final alternative and independent means software upgrade
3. Environmental constraints
-jdk1.7 and above 1.8.0_112
-maven3.3 3.3.9 and above
-IntelliJIDEA2017
-Spring Boot1.5.9
1.MAVEN settings.xml
<profile>
<id>jdk1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
2.IDEA settings
maven home directory
user settings files
4.Spring Boot Helloword
1. Create a Maven project
2. Related dependence introduced Spring Boot
<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> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
3. Write a main program: Start Spring Boot
/**
* @SpringBootApplication 来标注一个主程序,说明这是一个Spring Boot应用
*/
@SpringBootApplication
public class HelloApp {
public static void main(String[] arge){
//Spring 应用启动起来
SpringApplication.run(HelloApp.class,arge);
}
}
4. Write Controler, Service
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World";
}
}
5. Test run the main program
6. Simplify deployment
<!-- 这个插件,可将应用打包成一个可执行的jar包 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
This application will be labeled jar package, the direct use of java -jar command execution
5.Hello World Exploration
1.POM file
1. Parent Project
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<!-- 它的父项目是 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
<!-- 它来真正管理Spring Boot应用里面的所有依赖版本 -->
Spring Boot version Arbitration Center
After we import dependence is no need to write the default version; (there is no need to declare the dependencies naturally inside the administration in)
2. Import dependence
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter-web:
spring-bot-starter: spring-boot scene initiator; help us introducing the web assembly module is operating normally depend
Spring-Boot all the features scenes are extracted, made a number of starters (starter), only need to import the starter in the project, all depend on the relevant scenes are imported come
2 of the main program, the main entrance class
/**
* @SpringBootApplication 来标注一个主程序,说明这是一个Spring Boot应用
*/
@SpringBootApplication
public class HelloApp {
public static void main(String[] arge){
//Spring 应用启动起来
SpringApplication.run(HelloApp.class,arge);
}
}
@SpringBootApplication: Spring Boot application is marked on a class, indicating that this class is SpringBoot master class configuration, SpringBoot you should run the main method of this class of applications to start SpringBoot
@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 {
@ SpringBootConfiguration : the Spring the Boot configuration class:
Marked on a class, he said it was a SpingBoot configuration class
@ The Configuration : configuration class marked up this comment
Configuration class ...... Profiles: class is a container set up: @Component
@ EnableAutoConfiguration : turn on auto-configuration feature;
Things before we need to configure, SpringBoot help us configure; @ EnableAutoConfiguration tell us SpringBoot open the automatic configuration feature; this command will take effect automatically;
@AutoConfigurationPackage
@Import({EnableAutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
@ AutoConfigurationPackage : Automatic configuration package
@Import({Registrar.class})
Spring's annotation @import, introduced into a container to assembly; import component Registrar.class
Where all components of all primary package configuration class (@SpringBootApplication class label) and the lower sub-packets of scanned Spring container inside;
@Import({EnableAutoConfigurationImportSelector.class}):
Import component to the container
EnableAutoConfigurationImportSelector: which introduced the selector assembly;
All of the components needed to return the introduced full class name manner; these components will be added to the container;
It will import container configuration comes with a lot of class (xxxAutoConfiguration); this scene is to import all the components required for container and configure these components;
With automatic configuration class, eliminating the need to manually write the configuration work we inject functional components;
SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class, ClassLoader);
SpringBoot at boot time, specified EnableAutoConfiguration obtain the value from the META-INF / spring.factories class path, the autoconfiguration these values as class into a container, automatically configured based on force, work for us automatically configured; we need to own something before configuration, automatic configuration class to help us;
J2EE overall integration solutions and automatic configuration are spring-boot-autoconfigure-1.5.9.RELEASE.jar in;
6. Spring Boot quickly create a project using Spring Initislizer
IDE support the creation wizard to quickly create a project using Spring Spring Boot project;
Select the module we need; networking wizard created SpringBoot project;
Farmer generated Spring Boot project:
- The main program has generated good, just need to write their own logic
- resources folder directory structure
- static: Save all static resources; js css images;
- templates: Save all template pages; (Spring Boot default jar package using an embedded tomcat, the default does not support JSP page); you can use a template engine (freemarker, thymeleaf)
- application.properties:SpringBoot application configuration file; you can modify some of the default settings