Springboot开心学习(第三天)

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区别

  1. YAML配置是有序的,而properties配置是无序的
  2. 自定义的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举例

  1. 创建新的Spring Boot工程,注意要选择以下两个依赖
    在这里插入图片描述
  2. 创建bean
public class User {
    private String name;
    private int age;
    private String address;
	//省略set/get    
}
  1. 创建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";
    }
}

  1. 添加.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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值