1.YML基本语法
修改端口,在resources中写配置文件(applicationconfig.yml)
server:
port: ...
多个级之间用:分类,并且换行缩进 , 值不用换行 , 值前面有个空格
冒号后要空格
2.springBoot读取配置文件
2.1yml中写(读取datasource)
jdbc:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql:///crm
username: *****
password: *****
2.2写相关的类
/**
* @ConfigurationProperties: 创建一个假的datesource,通过该注解,赋值给对应的属性(配置文件值的注入,如果配置文件的
* 属性少则用value)
*/
@ConfigurationProperties(prefix = "jdbc")
@Component
public class MyDateSource {
private String driverCLassName;
private String url;
private String username;
private String password;
public String getDriverCLassName() {
return driverCLassName;
}
public void setDriverCLassName(String driverCLassName) {
this.driverCLassName = driverCLassName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public static Logger getLog() {
return log;
}
@Override
public String toString() {
return "MyDateSource{" +
"driverCLassName='" + driverCLassName + '\'' +
", url='" + url + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
2.3控制层注入,并打印
2.4配置文件中只有一个属性时 ——value
yml中
server:
port: 8888
控制层中(不需要单独写一个类)
@Value("${server.port}")
private String port;
@RequestMapping("/")
@ResponseBody
String home(){
System.out.println(port);
return "hello world!!!";
}
3.多环境配置切换
3.1方式一
必须加三个虚线
spring:
profiles:
active: test #激活(选择)环境test
---
spring:
profiles: dev #指定环境名字dev
server:
port: 9999
---
spring:
profiles: test #指定环境名字test
server:
port: 8888
3.2方式二
写三个yml,分别放置配置
通过配置文件的名字来识别环境
application-dev.yml
server:
port: 9999
application-test.yml
server:
port: 8888
application.yml
spring:
profiles:
active: test
#根据文件名字配置 application-dev.properties
4springboot的测试
4.1导包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
4.2测试类
//springboot的测试
@RunWith(SpringRunner.class)
@SpringBootTest(classes= ApplicationConfig.class)
public class TempSpringBootTest {
@Test
public void testSpringBoot()throws Exception{
System.out.println("springboot测试!!!");
}
}
5打包
5.1导包
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
5.2代理及idea右侧的Maven project,在双击package,会在target生成相关文件
5.3进入生成文件所在的命令行(cmd),输入java -jar 输入生成文件的地址![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/29fa638e4ec9c789aabd0b0285edadb9.png)
6springBoot集成拦截器
6.1创建类MyHandlerInterceptor 实现HandlerInterceptor
@Component
public class MyHandlerInterceptor implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("拦截器开始启动了!!!");
return true;
}
}
6.2在主配置类中实现WebMvcConfigurer,重写addInterceptors方法,addPathPatterns拦截所有的地址,excludePathPatterns放行需要的路径
@SpringBootApplication
public class ApplicationConfig implements WebMvcConfigurer
{
@Autowired
private MyHandlerInterceptor myHandlerInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myHandlerInterceptor).addPathPatterns("/**").
excludePathPatterns("/login");
}
}