无法在AuditInterceptor中自动装配服务,该服务扩展了EmptyInterceptor

时间:2016-06-10 05:12:20

标签: java hibernate spring-boot

我有我的AuditInterceptor类,它从org.hibernate扩展EmptyInterceptor。

我将拦截器作为bean添加到springboot主类中,如下所示。

@Bean
        public LocalContainerEntityManagerFactoryBean entityManagerFactory(
                EntityManagerFactoryBuilder factory, DataSource dataSource,
                JpaProperties properties) {
            Map<String, Object> jpaProperties = new HashMap<String, Object>();
            jpaProperties.putAll(properties.getHibernateProperties(dataSource));
            jpaProperties.put("hibernate.ejb.interceptor", hibernateInterceptor());
            return factory.dataSource(dataSource).packages("sample.data.jpa")
                    .properties((Map) jpaProperties).build();
        }

    public AuditInterceptor hibernateInterceptor() {
        AuditInterceptor auditInterceptor = new AuditInterceptor();
        return auditInterceptor;
    }

我有AuditInterceptor试图自动装配我的服务类。

@Component
public class AuditInterceptor extends EmptyInterceptor {

   private final Logger LOGGER = LogManager.getLogger(this.getClass());

   @Autowired
   private IAuditMstService auditMstService;

当我启动springboot应用程序时,我遇到错误。

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'entityManagerFactory': Requested bean is currently in creation: Is there an unresolvable circular reference?
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:347) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    ... 133 common frames omitted

有谁可以帮忙解决这个问题???

1 个答案:

答案 0 :(得分:0)

不要尝试新的AuditInterceptor(),也可以尝试自动装配AuditInterceptor。