Mysql Hibernate拦截器中的spring boot自动连线存储库不工作

Mysql Hibernate拦截器中的spring boot自动连线存储库不工作,mysql,hibernate,spring-boot,spring-data-jpa,Mysql,Hibernate,Spring Boot,Spring Data Jpa,我正在使用Hibernate拦截器处理更新,需要将存储库保存到数据库中 我自动连接了存储库,但它不工作。总是空的。这是我的密码: @Component public class AuditInterceptor extends EmptyInterceptor { @Autowired private LogsRepository repository; public boolean onFlushDirty(Object entity, Serializable id, Object[]

我正在使用Hibernate拦截器处理更新,需要将存储库保存到数据库中

我自动连接了存储库,但它不工作。总是空的。这是我的密码:

@Component
public class AuditInterceptor extends EmptyInterceptor {

@Autowired
private LogsRepository repository;

public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
        String[] propertyNames, Type[] types) {
    if(entity instanceof Personnel) {
        for (int i = 0; i < propertyNames.length; i++) {
            if(currentState[i] != previousState[i]) {
                Log log = new Log();
                log.setEntity("Personnel");
                log.setIdEntity(id.toString());
                log.setPropertyChanged(propertyNames[i] );
                log.setOldValue(previousState[i]  != null ? previousState[i].toString() : "");
                log.setNewValue(currentState[i] != null ? currentState[i].toString() : "");
                log.setTimestamp(new Date());
                System.out.println(log.toString());
                repository.save(log);
                return true;
            }
            System.out.println("Property names :"+propertyNames[i]);
            System.out.println("Old value :"+previousState[i]);
            System.out.println("New value :"+currentState[i]);
        }
    }

    return false;
}
@组件
公共类审核侦听器扩展了EmptyInterceptor{
@自动连线
私有日志存储库;
公共布尔值onFlushDirty(对象实体,可序列化id,对象[]当前状态,对象[]先前状态,
字符串[]属性名称,类型[]类型){
if(人员的实体实例){
for(int i=0;i
}


拦截器工作正常,但LogRepository总是空的。

我通过实现ApplicationContextAware解决了这个问题

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringContext implements ApplicationContextAware {

    private static ApplicationContext context;

    public static <T extends Object> T getBean(Class<T> beanClass) {
        return context.getBean(beanClass);
    }

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        SpringContext.context = context;
    }
}
LogsRepository repository = SpringContext.getBean(LogsRepository.class);