如何在EntityListener类中使用@Autowired?

时间:2019-05-20 17:12:48

标签: java spring hibernate jpa

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

EntityListener:

@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;
}

1 个答案:

答案 0 :(得分:0)

据我所知,您不能将Spring托管的bean注入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