文章目录
1. 属性注入
1.1 Spring版本
在application.properties
添加配置的属性
book.id=99
book.name=三国演义
book.author=罗贯中
创建一个Book
类
@Component
public class Book {
@Value("${book.id}")
private Integer id;
@Value("${book.name}")
private String name;
@Value("${book.author}")
private String author;
// 省略getter/setter
// 省略toString()
}
在测试类中执行打印.
@SpringBootTest
class Createdemo01ApplicationTests {
@Autowired
Book book;
@Test
void contextLoads() {
System.out.println(book);
}
}
输出如下:
注意:若不在默认的application.properties
添加配置的属性,比如在book.properties
:只需用在book.java
上添加@PropertySource("classpath:book.properties")
即可。
1.2 类型安全的属性注入
在book.java
上添加@ConfigurationProperties(prefix = "book")
即可
@Component
@PropertySource("classpath:book.properties")
@ConfigurationProperties(prefix = "book")
public class Book {
// @Value("${book.id}")
private Integer id;
// @Value("${book.name}")
private String name;
// @Value("${book.author}")
private String author;
// 省略getter/setter
// 省略toString()
}
执行后结果一致。
2. YAML
2.1 与properties区别
- YAML配置是有序的,而properties配置是无序的
- 自定义的YMAL目前暂时不支持使用注解直接注入到Spring Boot中
2.2 实验
新建application.yaml
文件
数据格式如下:
server:
port: 8081
servlet:
context-path: /sky
redis:
port: 6379
hosts:
- 192.168.1.123
- 192.168.1.124
- 192.168.1.125
- 192.168.1.126
创建测试类RedisCluster
@Component
@ConfigurationProperties("redis")
public class RedisCluster {
private Integer port;
private List<String> hosts;
// getter / setter
// toString
}
2.3 如果是对象列表
举例:
创建Redis.java
public class Redis {
private Integer port;
private String host;
//getter/setter
//toString()
}
修改RedisCluster.java
,添加private List<Redis> redisList;
,增加对应的get/set方法,修改toString()方法。
重点:注意对象列表写法:
server:
port: 8081
servlet:
context-path: /sky
redis:
port: 6379
hosts:
- 192.168.1.123
- 192.168.1.124
- 192.168.1.125
- 192.168.1.126
redisList:
- port: 6379
host: 192.168.2.110
- port: 6379
host: 192.168.2.111
3. Profile
针对生产环境、测试环境等有不同的配置,所以需要不同的配置参数。
注意:必须是application-{profile}.properties
格式
最后,在application.properties
中填入spring.profiles.active=dev
进行激活。
4. 整合freemarker举例
- 创建新的Spring Boot工程,注意要选择以下两个依赖
- 创建bean
public class User {
private String name;
private int age;
private String address;
//省略set/get
}
- 创建
controller
@Controller
public class UserController {
@GetMapping("/user")
public String user(Model model){
List<User> users = new ArrayList<>();
for (int i = 0; i < 10; i++) {
User u = new User();
u.setName("user_"+i);
u.setAge(i);
u.setAddress("china");
users.add(u);
}
model.addAttribute("users", users);
return "us";
}
}
- 添加
.ftlh
文件
内容如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<tr>
<td>姓名</td>
<td>年龄</td>
<td>地址</td>
</tr>
<#list users as u>
<tr>
<td>${u.name}</td>
<td>${u.age}</td>
<td>${u.address}</td>
</tr>
</#list>
</table>
</body>
</html>
运行,访问http://localhost:8080/user
,显示如下:
附录:
application.properties
中常用的对Freemarker的配置
# 自定义的Freemarker模板位置,默认在classpath下面的templates
spring.freemarker.template-loader-path=classpath:/sky
# 自定义模板的编码格式,默认是UTF-8
spring.freemarker.charset=UTF-8
# 定义
spring.freemarker.content-type=text/html
# 是否开启Freemarker缓存
spring.freemarker.cache=false
# 配置模板后缀
spring.freemarker.suffix=.ftlh