gpt4 book ai didi

java - 如何在EntityListener类中使用@Autowired?

转载 作者:行者123 更新时间:2023-12-02 05:26:49 29 4
gpt4 key购买 nike

我有一个 DatabaseInitializer 类,它通过 crudrepositories 将一些数据插入到我的数据库中。现在,我添加了一个 EntityListener,如果表 2 中不存在该实体的日期,它应该更新另一个表(表 2)中的数字。为此,我尝试使用该实体的 crudrepository @Autowired。但存储库未正确自动连接,它始终为空。

实体监听器:

@Component
public class OrderDayIdListener {

@Autowired
private static OrderRepository orderRepository;

@Autowired
private OrderDayIdRepository orderDayIdRepository;

@PrePersist
private void incrementOrderIdInTable(Order order) {
LocalDate date = order.getDate();

OrderDayId orderDayIdObject = orderDayIdRepository.findByDate(date);

if(orderDayIdObject == null){
orderDayIdObject = new OrderDayId(1L, date);
} else {
orderDayIdObject.incrementId();
}

Long orderDayId = orderDayIdObject.getId();
order.setOrderDayId(orderDayId);

orderDayIdRepository.save(orderDayIdObject);
orderRepository.save(order);
}
}

实体:

@EntityListeners(OrderDayIdListener.class)
@Data
@Entity
public class Order {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;

@Column(name ="date")
private LocalDate date;
}

最佳答案

据我所知,您无法将 Spring 托管 bean 注入(inject)到 JPA EntityListener 中。我发现创建辅助类来完成这项工作:

public final class AutowireHelper implements ApplicationContextAware {

private static final AutowireHelper INSTANCE = new AutowireHelper();
private static ApplicationContext applicationContext;

private AutowireHelper() {
}

/**
* Tries to autowire the specified instance of the class if one of the specified beans which need to be autowired
* are null.
*
* @param classToAutowire the instance of the class which holds @Autowire annotations
* @param beansToAutowireInClass the beans which have the @Autowire annotation in the specified {#classToAutowire}
*/
public static void autowire(Object classToAutowire, Object... beansToAutowireInClass) {
for (Object bean : beansToAutowireInClass) {
if (bean == null) {
applicationContext.getAutowireCapableBeanFactory().autowireBean(classToAutowire);
return;
}
}
}

@Override
public void setApplicationContext(final ApplicationContext applicationContext) {
AutowireHelper.applicationContext = applicationContext;
}

/**
* @return the singleton instance.
*/
public static AutowireHelper getInstance() {
return INSTANCE;
}}

不仅仅是这样做:

public class OrderDayIdListener {

@Autowired
private OrderRepository orderRepository;

@Autowired
private OrderDayIdRepository orderDayIdRepository;

@PrePersist
public void incrementOrderIdInTable(Order order) {
AutowireHelper.autowire(this, this.orderRepository);
AutowireHelper.autowire(this, this.orderDayIdRepository);

LocalDate date = order.getDate();

OrderDayId orderDayIdObject = orderDayIdRepository.findByDate(date);

if(orderDayIdObject == null){
orderDayIdObject = new OrderDayId(1L, date);
} else {
orderDayIdObject.incrementId();
}

Long orderDayId = orderDayIdObject.getId();
order.setOrderDayId(orderDayId);

orderDayIdRepository.save(orderDayIdObject);
orderRepository.save(order);
}}

完整说明here

关于java - 如何在EntityListener类中使用@Autowired?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56225449/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com