Spring Boot study1 入门
https://spring.io/guides/gs/spring-boot-docker/ 官网摘要
-
jdk:1.8(1.7及其以上)
-
Maven:3.3及以上 settings.xml中 profiles下做配置 使其用jdk1.8编译
<profile>
<id>jdk-1.8</id>
<activation>
<jdk>1.8</jdk>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</repositories>
</profile>
-
IntelliIDEA2017:files——settings——Build,Execution,——Build Tools——Maven 可选择安装的maven版本
-
SpringBoot:1.5.9(2.0.5.RELEASE)pom.xml中依赖的parten
HelloWorld demo(jar工程)
1、创建maven工程:newProject-maven
2、导入springBoot依赖:pom.xml中引入
依赖了tomcat,启动jar就相当于启动了web服务
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3、写主函数main方法
类用 @SpringBootApplication 说明是主程序
运行:SpringApplication.run(HelloWorldApplication.class, args);启动sptingBoot
4、编写controller和services等业务逻辑类
@controller 标注是controller
@requestMapping
@responsetBody
5、运行主程序
运行main方法
6、简化部署,
引用打包插件打出jar包;运行 Lifecycle—package,然后运行java -jar运行jar包
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
SpringBoot分析
1、POM文件
1、依赖父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
这个父项目又已经依赖另一个父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
第二个父项目管理着springBoot各个依赖包的版本;
所以以后导包一般不需要写版本
2、导入了依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
spring-boot-starter-web:web启动器,导入了web模块正常运行的所有依赖组件
spring-boot-starter:是spring-boot场景启动器,将所有场景功能都抽取出来,做成了一各个starters,值需要在项目开发是要是引入一下相关场景的starter就可以了。
官网文档中的starters:https://docs.spring.io/spring-boot/docs/2.0.7.RELEASE/reference/htmlsingle/#using-boot-starter
3、主程序类
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {//psvm
//启动spring程序
SpringApplication.run(HelloWorldApplication.class, args);
}
}
@SpringBootApplication:Spring Boot 的主配置类,会运行这个类的main方法来启动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:SpringBoot的配置类,说明这是一个配置类,又依赖如下Spring配置类
-
@Configuration:Spring中的配置类
@EnableAutoConfiguration:开启自动配置,以前Spring需要配置的东西现在都不用配置了。主要依赖如下
-
@AutoConfigurationPackage:自动配置的包。将主配置类(@SpringBootApplication)所在的包下的所有组件(如 @Controller等)放置到spring容器中
-
@Import({AutoConfigurationImportSelector.class}):导入容器组件选择器。将所有需要导入的组件以全类名的方式返回;这些组件会被添加到容器中,会导入那些组件?会导入许多自动配置类(XXAutoConfiguration)就是各种场景需要的组件,就是类路径下META-INF/spring.factories中EnableAutoConfiguration的值
-
J2EE的自动配置主要是在jar包中配置:spring-boot-autoconfigure-2.0.5.RELEASE.jar
Spring Initializr 引导器快速创建项目
IDE都支持使用Spring的项目创建向导快速创建一个Spring Boot项目:
-
new Project—Spring Initializr(idea)
-
Spring start Project (Eclipese)
选择模块后,向导需要联网才能创建Spring Boot项目
默认生成的Spring Boot项目:
-
主程序:我们只需要业务逻辑
-
resources文件夹
-
static:静态资源,js css images
-
templates:保存所有的模板页面,默认不支持jsp,可用使用模板引擎(freemarker,thymeleaf)
-
application.properties:修改一些应用的默认配置,我server.port修改端口等
-
-

浙公网安备 33010602011771号