正文
springboot - 应用实践(1)认识springboot
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
1、为什么要推出springboot
springboot设计的目的是用来简化新spring应用的初始搭建以及开发过程。springboot遵循“ 约定优于配置 ”原则。
2、springboot默认的配置文件application.properties
3、日志依赖模块spring-boot-starter-logging,自动使用的是logback作为项目的日志框架
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<!--使用log4j2,还需要一些配置,参考logback.xml在application.properties中的应用-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
4、Web开发依赖模块spring-boot-starter-web
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
一些约定:
1》项目结构层面的约定
静态文件和页面统一放在src/main/resources对应的子目录下。src/main/resources/static用于放置静态资源文件,如css、js、images等;src/main/resources/templates
目录用于放置页面模板文件,如html、jsp等。
2》springMVC框架层面的约定
spring-boot-starter-web依赖模块默认自动配置一些springMVC必要的组件:
1>将ViewResolver自动注册到spring容器
2>将Converter和Formatter等bean自动注册到spring容器。
3>将对Web请求的支持和相应的类型转换的HttpMessageConverter自动注册到spring容器。
4>将MessageCodesResolver自动注册到spring容器。
3》嵌入式Web容器层面的约定
spring-boot-starter-web依赖模块默认使用嵌入式Tomcat作为Web容器对外提供服务,默认使用8080端口对外监听和提供服务。如果不想使用默认的嵌入式Tomcat,可以引入jetty
或者undertow作为替代方案。
如果不想使用默认的8080端口,可以通过application.properties配置文件中的server.port使用自己制定的端口。如:server.port=8088
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
或者
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
5、修改 maven项目默认创建的项目jre不是用户环境变量中配置的jre
<!--修改maven的settings.xml文件,找到profiles节点-->
<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.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
6、构建springboot应用,设置<parent.../>。
pom.xml必须设置<parent.../>元素设置为springboot的spring-boot-starter-parent,spring-boot-starter-parent是springboot的核心启动器,包含了自动配置(如starter-web的版本选择等)、日志和YAML等大量默认的配置,大大简化了开发工作。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>