在JavaSpring中动态解析依赖关系的最佳方法?
假设我有这样的代码结构:在JavaSpring中动态解析依赖关系的最佳方法?,java,spring,Java,Spring,假设我有这样的代码结构: public class NotificationService { public void send(Notification notification) { // call other services and send the notification } } public class OrderNotification implements Notification { @Autowired public
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.send(new OrderNotification(orderNumber));
但我知道上面的代码不起作用,因为translationService
无法解析
我的目标是将自定义参数传递给我的
通知
类,并能够使用该类中的服务。春天最好的方式是什么 您几乎没有可用的选项:
new
关键字创建的对象上的Spring注释。这些文件对此进行了解释OrderNotification
声明为原型作用域bean,并使用方法从上下文中获取每个实例
@Autowired
并使用普通构造函数注入
public OrderNotification(TranslationService translationService, String orderNumber) {
this.translationService = Objects.requireNonNull(translationService);
this.orderNumber = Objects.requireNonNull(orderNumber);
}
如果您只需要简单的
@Autowired
,我会选择选项3。这是最简单的方法,它使编写单元测试更容易,因为您不必依赖Spring。您几乎没有可用的选项:
new
关键字创建的对象上的Spring注释。这些文件对此进行了解释OrderNotification
声明为原型作用域bean,并使用方法从上下文中获取每个实例
@Autowired
并使用普通构造函数注入
public OrderNotification(TranslationService translationService, String orderNumber) {
this.translationService = Objects.requireNonNull(translationService);
this.orderNumber = Objects.requireNonNull(orderNumber);
}
如果您只需要简单的
@Autowired
,我会选择选项3。这是最简单的方法,它使编写单元测试变得更容易,因为您不必依赖Spring。我知道下面不是您问题的正确答案。然而,将实体
和服务
结合起来是一种糟糕的设计模式。实体
只应包含有关对象的信息,而不应包含业务逻辑。服务
包含所有业务逻辑
public class OrderNotification implements Notification {
private String orderNumber;
public OrderNotification(String orderNumber) {
this.orderNumber = orderNumber;
}
public String getMessage() {
return "Order number: " + orderNumber;
}
//Getter & Setters
...
}
@Service
public class NotificationService {
@Autowired
public TranslationService translationService;
public void send(Notification notification) {
//I do not know what trans accepts, so I assume it can accept Notification
translationService.trans(notification.getMessage());
}
}
您需要将服务
与实体
分开
OrderNotification看起来像一个常规实体。实体不应包含业务逻辑。您需要为业务逻辑提供特定的服务
public class OrderNotification implements Notification {
private String orderNumber;
public OrderNotification(String orderNumber) {
this.orderNumber = orderNumber;
}
public String getMessage() {
return "Order number: " + orderNumber;
}
//Getter & Setters
...
}
@Service
public class NotificationService {
@Autowired
public TranslationService translationService;
public void send(Notification notification) {
//I do not know what trans accepts, so I assume it can accept Notification
translationService.trans(notification.getMessage());
}
}
如果您真的需要将实体和服务结合起来,那么我推荐这种方法:
@Service
public class Master{
@Autowired
NotificationService notificationService
public void testMethod(){
Notification notification = notificationService.createOrder("order1");
notificationService.send(notification);
}
}
@Service
public class NotificationService {
@Autowired
public TranslationService translationService;
public Notification createOrder(String orderNumber){
return new OrderNotification(orderNumber, translationService);
}
public void send(Notification notification) {
// call other services and send the notification
notification.getMessage();
}
}
public class OrderNotification implements Notification {
private TranslationService translationService;
private String orderNumber;
//I have changed this constructor to accept TranslationService.
public OrderNotification(String orderNumber, TranslationService translationService) {
this.orderNumber = orderNumber;
this.translationService = translationService;
}
public String getMessage() {
return translationService.trans('notification.order', new Object[]{orderNumber});
}
}
我知道下面不是你问题的正确答案。然而,将
实体
和服务
结合起来是一种糟糕的设计模式。实体
只应包含有关对象的信息,而不应包含业务逻辑。服务
包含所有业务逻辑
public class OrderNotification implements Notification {
private String orderNumber;
public OrderNotification(String orderNumber) {
this.orderNumber = orderNumber;
}
public String getMessage() {
return "Order number: " + orderNumber;
}
//Getter & Setters
...
}
@Service
public class NotificationService {
@Autowired
public TranslationService translationService;
public void send(Notification notification) {
//I do not know what trans accepts, so I assume it can accept Notification
translationService.trans(notification.getMessage());
}
}
您需要将服务
与实体
分开
OrderNotification看起来像一个常规实体。实体不应包含业务逻辑。您需要为业务逻辑提供特定的服务
public class OrderNotification implements Notification {
private String orderNumber;
public OrderNotification(String orderNumber) {
this.orderNumber = orderNumber;
}
public String getMessage() {
return "Order number: " + orderNumber;
}
//Getter & Setters
...
}
@Service
public class NotificationService {
@Autowired
public TranslationService translationService;
public void send(Notification notification) {
//I do not know what trans accepts, so I assume it can accept Notification
translationService.trans(notification.getMessage());
}
}
如果您真的需要将实体和服务结合起来,那么我推荐这种方法:
@Service
public class Master{
@Autowired
NotificationService notificationService
public void testMethod(){
Notification notification = notificationService.createOrder("order1");
notificationService.send(notification);
}
}
@Service
public class NotificationService {
@Autowired
public TranslationService translationService;
public Notification createOrder(String orderNumber){
return new OrderNotification(orderNumber, translationService);
}
public void send(Notification notification) {
// call other services and send the notification
notification.getMessage();
}
}
public class OrderNotification implements Notification {
private TranslationService translationService;
private String orderNumber;
//I have changed this constructor to accept TranslationService.
public OrderNotification(String orderNumber, TranslationService translationService) {
this.orderNumber = orderNumber;
this.translationService = translationService;
}
public String getMessage() {
return translationService.trans('notification.order', new Object[]{orderNumber});
}
}
看看这个答案,看看如何在手动创建的对象上初始化
自动连接的字段:@AntoineB我看到了。在春天,这被认为是一种好的做法吗?因为这是唯一的方法,我相信是的。还可以检查一下这个答案,看看如何在手动创建的对象上初始化自动连接的字段:@AntoineB我看到了。在春天,这被认为是一种好的做法吗?因为这是唯一的方法,我相信是的。同时检查一个人是否完全同意。它不应该以这种方式使用<在这种情况下,代码>订单通知
类应为POJO。完全同意。它不应该以这种方式使用OrderNotification
类在这种情况下应该是POJO。3选项看起来不错,但是将TranslationService
注入到我想要使用通知的每个类中非常烦人。3选项看起来不错,但是将TranslationService
注入到我想要使用通知的每个类中非常烦人。