SpringBoot简介,启动方式,SpringBoot常用注解,SpringBoot配置文件
- 1、SpringBoot 安装
- 2、SpringBoot作用
- 3、SpringBoot启动方式
- 4、常见注解
- 5、Springboot配置文件application.properties或者application.yml
- 5.1、application.properties
- 5.2、application.yml
- 5.3、Springboot默认加载配置文件的路径
- 6、SpringBoot 的核心配置文件
- 7、SpringBoot读取配置文件
- 8、SpringBoot Starter Web依赖
- 9、Springboot需要独立的容器运行吗?
- 10、如何在SpringBoot启动的时候运行一些特定的代码?
1、SpringBoot 安装
安装网址参考:https://wwwblogs/zjdxr-up/p/8617242.html
官网地址:https://spring.io/tools3/sts/all/
构建SpringBoot项目网址:http://start.spring.io/
2、SpringBoot作用
SpringBoot的目的是用来简化Spring应用的配置以及搭建过程。
- SpringBoot的Web组件集成了SpringMVC,特别适合构建微服务,另外还封装了经常使用的组件,如Mybatis,hibernate,Redis等
- 无需Tomcat,SpringBoot内嵌Tomcat
- 减少XML配置,直接配置.properties或者.yml文件即可。
3、SpringBoot启动方式
1.右键启动,Run
2.命令行方式,首先进入项目目录下面,然后执行mvn spring-boot:run
3.项目目录下,先运行mvn install 编译一下
进入target目录下,cd target
找到一个.jar文件,运行java -jar xxxxx.jar
4、常见注解
@SpringBootApplication
是SpringBoot的核心注解,主要目的是开启自动配置,@SpringBootApplication是一个组合注解
主要包含:
- @SpringBootConfiguration
- @EnableAutoConfiguration:启用自动配置,这注解会使Spring Boot根据项目中依赖的jar包自动配置项目的配置项。
如,我们添加了spring-boot-starter-web的依赖,项目中也就会引入SpringMVC的依赖,并且SpringBoot会自动配置Tomcat和SpringMVC - @ComponentScan默认扫描@SpringBootApplication所在类的同级目录以及它的子目录
@ComponentScan
扫描包,使注解生效
@RestController
用于Controller注解,相当于@Controller + @ResponseBody
@RequestMapping(value={" “,” "})
指定URL路径,里面URL路径可以写一个集合
@Autowired
按类型装配Bean,自动注入对象
@service
用于service层,加上注解可以被扫描到
@ImportResource
在入口类添加@ImportResource({“classpath:*.xml”,“classpath:**.xml”}),引入一些额外的配置
5、Springboot配置文件application.properties或者application.yml
5.1、application.properties
spring. datasource.url=jdbc:oracle:thin:@localhost:1521:XE
spring. datasource.username = admin
spring. datasource.password = admin
spring. datasource.driver-class-name = oracle.jdbc.driver.OracleDriver
5.2、application.yml
YAML是json的一个超集,是一种可以轻松定义层次结构的数据结构。
yml文件的格式规则,每一级之间必须换行,并且第二级应该以一个tab键缩进,赋值是在“:”后面进行赋值,“:”后面要跟一个空格的缩进。
spring:
datasource:
url: jdbc:oracle:thin:@localhost:1521:XE
username: admin
password: admin
driver-class-name: oracle.jdbc.driver.OracleDriver
5.3、Springboot默认加载配置文件的路径
按照优先级从高到低,
- 1.工程根目录的config目录:file:./config/
- 2.工程根目录:file:./
- 3.类路径的config目录:classpath:/config/
- 4.类路径:classpath:/
如果上述路径下面都配置了application.properties文件,则严格按照优先级从低到高进行覆盖。
6、SpringBoot 的核心配置文件
SpringBoot的核心配置文件有bootstrap和application文件。
application配置文件主要用于Springboot项目的自动化配置。
bootstrap配置文件有一下几个应用场景:
(1)使用Spring cloud config配置中心时,这时需要在bootstrap配置文件中添加连接到配置中心的配置属性来加载外部配置中心的配置信息。
(2)一些固定的不能被覆盖的属性
(3)一些加密/解密的场景
7、SpringBoot读取配置文件
1、@ConfigurationProperties读取属性文件
@ConfigurationProperties(prefix=“spring.dataSource”)
@Component
@ConfigurationProperties(prefix = "spring.datasource")
public class MyDataSource {
private String url;
private String username;
private String password;
private String driverClassName;
// 提供Setter 和 Getter 方法
}
2、@Value可以读取单个配置文件,@Value不支持注入静态变量
@Component
public class MyDataSource {
@Value("spring.datasource.url") 或者 @Value("${spring.datasource.url}")
private String url;
@Value("spring.datasource.username")
private String username;
@Value("spring.datasource.password")
private String password;
@Value("spring.datasource.driver-class-name")
private String driverClassName;
// 提供Setter 和 Getter 方法
}
3、@PropertySource读取指定配置文件
@PropertySource
(value ={"config/db-config.properties"})
8、SpringBoot Starter Web依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Spring Boot Starter Web预先打包了依赖项,这些依赖项可以分为:
- Spring-core,beans,context,aop
- webMVC-(SpringMVC)
- JackSon -for json Binding
- Validation - Hibernate ,Validation API
- Servlet Container - Tomcat
- Logging - logback,slf4j
9、Springboot需要独立的容器运行吗?
可以不需要,Springboot内置了Tomcat/Jetty等容器。
10、如何在SpringBoot启动的时候运行一些特定的代码?
可以实现接口ApplicationRunner或者CommandLineRunner,这两个接口实现方式一样,它们都只提供一个run方法。
如果启动的时候有多个ApplicationRunner和CommandLineRunner,想控制它们的启动顺序,可以实现org.Springframework.core.ordered或者使用Order注解。
更多推荐
SpringBoot简介,启动方式,SpringBoot常用注解
发布评论