简介:
目录
目录
简介:
基础篇:
1.快速起步
2.启动依赖原理
3.配置文件
4.yaml语法
5.yaml数据格式及小结
6.从配置文件中读取值
7.profile配置方式
8.profile激活方式
9.内部配置加载顺序
10.外部配置加载顺序
基础篇:
1.快速起步
使用IDEA快速起步的具体步骤:
- New module
- Select "Spring initializr"
- Maven, java, jar
- 选择依赖(web-spring web)
会自动生成pom文件,引用parent, 规定java version,引入了starter-web& starter-test
然后加上controller就可以运行了:
- New java class
- Nane: ex "hello controller"
@RestController
public class HelloController {
public String hello();{
return "hello Spring boot 222";
}
}
访问 http://localhost:8080/hello
2.启动依赖原理
- Spring-boot-starter-parent:
定义了各种技术版本信息:
Dependency management: 版本锁定
在父工程里定义了版本信息后,将来继承这个的工程就不需要再定义版本信息了。
2. sping-boot-starter-web:
总结:
1.parent定义版本信息,防止版本冲突。
2.定义了完成该功能需要的坐标合计,其中大部分来自于父工程。
3.我们的工程继承parent,引入starter后,可以通过依赖传递方便的获取jar包。
3.配置文件
Spring boot是基于约定的很多配置都有默认值,但是如果要替换默认配置的话,就可以使用 application.properties 或 application.yml(yaml)进行配置。
properties:
server.port=8081
yml:
server:
prot: 8082
配置文件之间有优先级,配置同样的的时候
Properties > yml > yaml
(注:不同的配置依然可以识别)
4.yaml语法
5.yaml数据格式及小结
6.从配置文件中读取值
一共有三种方法:
1.@Value
2.Environment
3.@ConfigrationProperites
先给出yml文件
server:
port: 8082
name: abc
#对象
person:
name: ${name}
age: 20
address:
- bejing
- shanghai
#对象行内写法
person2: {name: zhangsan,age: 20}
#数组
address:
- bejing
- shanghai
#数组行内写法
address2: [bejing,shanghai]
#纯量
msg1: 'hello \n world'
msg2: "hello \n world"
@Value
@RestController
public class HelloController {
@Value("${name}")
private String name;
@Value("${person.name}")
private String name2;
@Value("${person.age}")
private int age;
@Value("${address[0]}")
private String address1;
@Value("${msg1}")
private String msg1;
@Value("${msg2}")
private String msg2;
}
return "hello string boot 222";
}
@RequestMapping("/hello")
public String hello(){
return "hello string boot 222";
}
}
打印:
System.out.println(name);
System.out.println(name2);
System.out.println(age);
System.out.println(address1);
System.out.println(msg1);
System.out.println(msg2);
输出:
abc
abc
20
bejing
hello \n world
hello
world
2. Environment
@Autowired
private Environment env;
就两行,很简单。
打印:
System.out.println(env.getProperty("person.name"));
System.out.println(env.getProperty("address[1]"));
结果:
abc
shanghai
3.@ConfigrationProperties
1.先new java class "Person"
2.定义3个对象
private String name;
private int age;
private String[] address;
3.添加get set 方法(快捷键 alt+insert)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String[] getAddress() {
return address;
}
public void setAddress(String[] address) {
this.address = address;
}
4.+toString 方便打印(address就不用了)
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
5.顶端加两个注解
@Component
@ConfigurationProperties(prefix = "person")
perfix用来指定前缀来访问person里的name, age, address, 而不是别的地方的
6.注入对象
@Autowired
private Person person;
7.打印
System.out.println(person);
String[] address = person.getAddress();
for (String s : address) {
System.out.println(s);
}
8.输出结果
Person{name='abc', age=20}
bejing
shanghai
全部代码总览
HelloContoller:
package com.ithema.springbootinit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Value("${name}")
private String name;
@Value("${person.name}")
private String name2;
@Value("${person.age}")
private int age;
@Value("${address[0]}")
private String address1;
@Value("${msg1}")
private String msg1;
@Value("${msg2}")
private String msg2;
@Autowired
private Environment env;
@Autowired
private Person person;
@RequestMapping("/hello2")
public String hello2(){
System.out.println(name);
System.out.println(name2);
System.out.println(age);
System.out.println(address1);
System.out.println(msg1);
System.out.println(msg2);
System.out.println("----------------------------");
System.out.println(env.getProperty("person.name"));
System.out.println(env.getProperty("address[1]"));
System.out.println("----------------------------");
System.out.println(person);
String[] address = person.getAddress();
for (String s : address) {
System.out.println(s);
}
return "hello string boot 222";
}
@RequestMapping("/hello")
public String hello(){
return "hello string boot 222";
}
}
Person:
package com.ithema.springbootinit;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private int age;
private String[] address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String[] getAddress() {
return address;
}
public void setAddress(String[] address) {
this.address = address;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
7.profile配置方式
1) 多profile文件方式
a).复制3个properties文件
写法是固定的:applicatoin-XXX.properties
dev = development
pro = product
test
b) 每一个里面写上不同的配置(ex: server.port=8082)
c) 在application.properties里来决定激活(active)谁
2.yml 多文档配置
---
server:
port: 8081
spring:
config:
activate:
on-profile: dev
---
server:
port: 8082
spring:
config:
activate:
on-profile: pro
---
server:
port: 8083
spring:
config:
activate:
on-profile: test
---将每个区域划分,然后命名每个区域
最后来激活(同一文件)
---
spring:
profiles:
active: dev
8.profile激活方式
方法一:虚拟机参数方式
1).点击右上角的“Edit Configrations...”
2. 在 VM Option 里设置激活谁。
方法二:命令行参数
可以通过内部设置:
也可以通过外部:
先通过jar打包:
位置:
然后去根目录,shift右键打开shell。后面来设置激活谁
9.内部配置加载顺序
虽然不会加载值,但是会加载文件
10.外部配置加载顺序
11.Springboot整合Redis
先创建一个模块
选择springboot redis
创建好之后可以在pom文件里看到redis的起步依赖
注入一个Template,加两个测试方法就好了
这里的redis是默认的本机ip,6379端口
如果需要修改,则需要去配置文件修改
(创建一个yml)