5.2 创建自己的配置属性
5.2 创建自己的配置属性
正如前面提到的,配置属性只不过是指定来接受
为了支持配置属性的属性注入,
为了演示
@GetMapping
public String ordersForUser(
@AuthenticationPrincipal User user, Model model) {
model.addAttribute("orders",
orderRepo.findByUserOrderByPlaceAtDesc(user));
return "orderList";
}
除此之外,还需要向
List<Order> findByUserOrderByPlaceAtDesc(User user);
请注意,此存储库方法是用
如前所述,在用户下了一些订单之后,这个控制器方法可能会很有用。但对于最狂热的
@GetMapping
public String ordersForUser(
@AuthenticationPrincipal User user, Model model) {
Pageable pageable = PageRequest.of(0, 20);
model.addAttribute("orders",
orderRepo.findByUserOrderByPlaceAtDesc(user));
return "orderList";
}
随着这个改变,
List<Order> findByUserOrderByPlaceAtDesc(User user, Pageable pageable);
这里,已经更改了
虽然这工作得非常好,但它让我感到有点不安,因为已经硬编码了页面大小。如果后来发现
可以使用自定义配置属性来设置页面大小,而不是硬编码页面大小。首先,需要向
@Controller
@RequestMapping("/orders")
@SessionAttributes("order")
@ConfigurationProperties(prefix="taco.orders")
public class OrderController {
private int pageSize = 20;
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
...
@GetMapping
public String ordersForUser(
@AuthenticationPrincipal User user, Model model) {
Pageable pageable = PageRequest.of(0, pageSize);
model.addAttribute("orders",
orderRepo.findByUserOrderByPlacedAtDesc(user, pageable));
return "orderList";
}
}
程序清单
新的
taco:
orders:
pageSize: 10
或者,如果需要在生产环境中进行快速更改,可以通过设置
$ export TACO_ORDERS_PAGESIZE=10
可以设置配置属性的任何方法,都可以用来调整最近订单页面的大小。接下来,我们将研究如何在属性持有者中设置配置数据。
5.2.1 定义配置属性持有者
这里没有说
对于
package tacos.web;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import lombok.Data;
@Component
@ConfigurationProperties(prefix="taco.orders")
@Data
public class OrderProps {
private int pageSize = 20;
}
正如在
它还带有
关于配置属性持有者,没有什么特别的。它们是从
@Controller
@RequestMapping("/orders")
@SessionAttributes("order")
public class OrderController {
private OrderRepository orderRepo;
private OrderProps props;
public OrderController(OrderRepository orderRepo,
OrderProps props) {
this.orderRepo = orderRepo;
this.props = props;
}
...
@GetMapping
public String ordersForUser(
@AuthenticationPrincipal User user, Model model) {
Pageable pageable = PageRequest.of(0, props.getPageSize());
model.addAttribute("orders",
orderRepo.findByUserOrderByPlacedAtDesc(user, pageable));
return "orderList";
}
...
}
现在
例如,假设在其他几个
package tacos.web;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import org.springframework.boot.context.properties.
ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import lombok.Data;
@Component
@ConfigurationProperties(prefix="taco.orders")
@Data
@Validated
public class OrderProps {
@Min(value=5, message="must be between 5 and 25")
@Max(value=25, message="must be between 5 and 25")
private int pageSize = 20;
}
//end::validated[]
尽管可以很容易地将
5.2.2 声明配置属性元数据
根据
图
配置属性元数据是完全可选的,并不会阻止配置属性的工作。但是元数据对于提供有关配置属性的最小文档非常有用,特别是在
例如,当我将鼠标悬停在
图
为了帮助那些可能使用你定义的配置属性(甚至可能是你自己定义的)的人,通常最好是围绕这些属性创建一些元数据,至少它消除了
要为自定义配置属性创建元数据,需要在
快速修复缺失的元数据。
如果正在使用
图
然后选择 Create Metadata for…
选项来为属性添加一些元数据(在
对于
{
"properties": [
{
"name": "taco.orders.page-size",
"type": "java.lang.String",
"description": "Sets the maximum number of orders to display in a list."
}
]
}
注意,元数据中引用的属性名是
有了这些元数据,警告就应该消失了。更重要的是,如果你悬停在
图
另外,如图
图
配置属性对于调整自动配置的组件和注入到应用程序