- springboot:可以说是一个框架,但是不是全新的框架,是基于spring封装的一个框架,可以理解为spring boot。表示spring配置
- springboot作用:在开发过程中精简配置,提高开发效率, 简化编码、简化配置、简化部署、简化监控
创建项目
创建springboot项目步骤:
点击idea中左上角File–》new --》Projects–》Spring initializer—》修改groupId、artifactId 继续下一步
springboot目录结构:
在maven项目结构基础之上,又多了一个SpringBootDemoApplication类,这个类被称为项目启动类
该类的命名规则:artifactId+Application
resources下面的目录结构:
static:放置静态文件,例如前端的js、css等
templates:放置前端的页面,jsp、html
application.properties:是属性文件,可以在该文件中对属性进行重写,例如端口:server.port=8090
备注:属性文件扩展名有两种格式:properties和yml 如果使用yml格式需要注意空格
勾选后会自动添加boot依赖
创建一个controller
在主程序入口SpringbootdemoApplication右键run运行 ,浏览器http://localhost:8080/test 即可运行使用
注解:
@SpringBootApplication:是springboot的核心注解,是一个组合注解,包括:
@SpringBootConfiguration :标识该类是配置类
@EnableAutoConfiguration:开启自动配置
@ComponentScan:把路径内文件所用的@注解自动加入到容器内,不用单独设置,
如果启动类和其他需要扫描的类不在同一个目录下,需要在启动类中重写注解:@ComponentScan(basePackages = “cn.demo.springbootdemo”)
主程序不在默认目录,则要修改@ComponentScan 主程序扫描的是同级+同级子目录
尽量放到包的最顶层
读取属性文件有两种方式:
方式一:(只支持propiters)
例:将属性文件中的user.name 、user.age的内容读取到User对象中
在User对象中加入对应的注解:
@Component //注册为组件
@ConfigurationProperties(prefix = “user”) 备注:prefix是配置文件中属性值的前缀
- @ConfigurationProperties 将本类中的所有属性与配置文件中相关的配置进行绑定;
- @ConfigurationProperties (prefix = “user”) 默认从全局配置中获取user配置
- prefix = “user” 表示 将配置文件中key为user的下面所有的属性与本类属性进行一一映射注入值,如果配置文件中,有则配入无则为空
- @Component 将本来标识为一个Spring 组件,因为只有是容器中的组件,容器才会为@ConfigurationProperties提供此注入功能
- @PropertySource (value = {“classpath:user.properties”}) 配置文件加载路径
方式二:
@Value("${teacher.name}")
private String name
@RestController
public class TestController {
@Autowired
private User user;
//通过value方式读取配置文件参数
@Value("${teacher.tname}")
private String name;
@RequestMapping("/test")
public String test(){
return "aaa";
}
/**
* 获取属性文件中的值
* @return
*/
@RequestMapping("/getUser")
public String getUser(){
return "username:"+user.getUsername()+", age:"+user.getAge()+"teacherName:"+name;
}
}
运行
banner
http://patorjk.com/software/taag
在resoueces内创建banner.txt
springboot多环境部署:
自己的笔记本上开发成为开发环境(简称:dev)、测试环境(简称:test)、生产环境:(简称:pro)
使用多环境部署需要在application.properties中指定当前的环境:spring.profiles.active=pro
比如各个环境的地址都不一样
排包
想停止哪个包就鼠标右键
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 排包 不使用哪个-->
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
springboot项目中添加的依赖可以不指定版本,因为项目是继承 会自动加载对应的依赖包
使用jetty容器
jetty容器和tomcat容器:都是Servlet容器,jetty容器是轻量级的
在项目中要是使用jetty容器,首先在pom中将tomcat容器排除掉,使用进行排包,在加入jetty的依赖包
<dependencies>
<!-- web依赖包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 排包 不使用哪个-->
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
</dependencies>
Spring Boot 的核心注解是哪个?它主要由哪几个注解组成的?
启动类上面的注解是@SpringBootApplication,它也是 Spring Boot 的核心注解,主要组合包含了以下 3 个注解:
@SpringBootConfiguration:组合了 @Configuration 注解,实现配置文件的功能。
@EnableAutoConfiguration:打开自动配置的功能,也可以关闭某个自动配置的选项,如关闭数据源自动配置功能: @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })。
@ComponentScan:Spring组件扫描。
开启 Spring Boot 特性
- 继承spring-boot-starter-parent项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
- 导入spring-boot-dependencies项目依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependencies>
</dependencyManagement>