【发布时间】:2018-12-14 20:49:12
【问题描述】:
假设我有这样的代码结构:
public class NotificationService {
public void send(Notification notification) {
// call other services and send the notification
}
}
public class OrderNotification implements Notification {
@Autowired
public TranslationService translationService;
private String orderNumber;
public OrderNotification(String orderNumber) {
this.orderNumber = orderNumber;
}
public String getMessage() {
return translationService.trans('notification.order', new Object[]{orderNumber});
}
}
所以,我的目标是以这种方式使用NotificationService:
notificationService.send(new OrderNotification(orderNumber));
但我知道上面的代码不起作用,因为 translationService 将无法解析。
我的目标是将自定义参数传递给我的Notification 类,并能够在该类中使用服务。春天最好的方法是什么?
【问题讨论】:
-
看看这个答案,看看如何在手动创建的对象上初始化
Autowired字段:*.com/a/37463747/3876196 -
@AntoineB 我见过。它在春季被认为是一种好的做法吗?
-
既然这是唯一的方法,我相信是的。
-
还要检查那个*.com/a/6739606/8049667