spring boot 笔记

springboot不是对spring的增强, 而是提供快速使用spring的方式

spring缺点, 配置繁琐, 依赖繁琐

springboot功能, 自动配置, 起步依赖, 辅组功能

约定大于配置 = 配置都有默认格式+默认值

 

 

 

 

@SpringBootApplication   //这个注解不能忘
public class HelloApplication {

public static void main(String[] args){
SpringApplication.run(HelloApplication.class, args);
}
}

yaml:
server:
  port: 8080    //用缩进表示归属

配置文件优先级: properties > yml > yaml

 从程序中读取: 

1.

 

 

 

 2.

 

 

 

 比@value好的地方是, 可以直接get, 不需要声明java变量

 

3.

application.yaml

 

 

test.java

 

 

 

 

 

 

 

server:
port: 8082

name: zhangshan
name2: zhangshan

person:
name: ${name}
age: 30

person2: {name: '${name2}', age: 33}

adress: [beijing, shanghai]

adress2:
- dalian
- xiamen

msg1: 'hello \n world' #原样输出
msg2: "hello \n world"



虚拟机参数:

 

 

命令行参数:

 

 

配置文件加载顺序:

打成jar之后, project级别下的配置文件, 不会被打进jar包


 

 这个才是项目级别

          

 

 

 

 

 

 

 

与jar同级目录, 默认会被spring识别读取, 不需要指定--spring.config.location 

 

 

 

 

 

 

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootTestApplication.class)      //如果test目录下包结构与主目录一模一样,(classes = SpringbootTestApplication.class)可以省略
public class UserServiceTest {

@Autowired
private UserService us;

@Test
public void testAdd(){
us.add();
}
}


Redis

springboot本地配置redis, 默认连接的是本机ip, 6379


posted @ 2021-06-28 02:14  hippoppower  阅读(69)  评论(0)    收藏  举报