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

标签 java spring hibernate jpa

我有一个 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/

相关文章:

java - 如何在 Java 中使用正则表达式拆分后删除空结果?

java - grails 渲染插件不兼容的返回类型问题

java - com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException : Cannot add or update a child row: a foreign key constraint fails

java - spring data Rest Hibernate 将所有表名小写并忽略 @Table ("Name")

java - 使用 Spring AOP 时是否代理了所有方法?

java - 未找到值或 ??当插入mysql时

java - 更新主方法中的变量?

java - 在 webMethods Java 服务中获取请求对象

spring - 每个请求模型的事件循环和线程之间的区别

java - Jackson 序列化一对多关系