描述
使用@Value映射properties文件属性到Java字段
重点
- 使用@PropertySource 注解指定*.properties文件位置;
- 使用@Value进行注入;
my.properties
book.author=ssslinppp
book.name=spring boot
Java类
package com.sssppp;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@PropertySource("classpath:my.properties")
public class Ch522 {
@Value("${book.author}")
private String bookAuthor;
@Value("${book.name}")
private String bookName;
@RequestMapping("/aa")
String index() {
return "book name is:" + bookName + " and book author is:" + bookAuthor;
}
}