发布于 4年前

报错 :The bean 'jpaAuditingHandler', defined in null

错误信息

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'jpaAuditingHandler', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

关键文件

JpaAuditorAware.java

package com.nasa.framework.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

import java.util.Optional;

@Configuration
@EnableJpaAuditing(auditorAwareRef = "jpaAuditorAware")
public class JpaAuditorAware implements AuditorAware<String> {
    @Override
    public Optional<String> getCurrentAuditor() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return Optional.of(loginUser.getUser().getUserId());
    }
}

Application.java

@EnableJpaAuditing
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class ,SecurityAutoConfiguration.class})
public class Application
{
    *** 略 ***
}

分析

错误描述是jpaAuditingHandler重复且无法覆盖,给出的建议是修改spring配置为允许覆盖:spring.main.allow-bean-definition-overriding=true。但设为允许覆盖后会带来其他麻烦,故不使用此设置。
google后发现@EnableJpaAuditing只需要加在启动类上就可以了,配置类JpaAuditorAware.java不需要再加了。

解决

JpaAuditorAware.java的注解@EnableJpaAuditing(auditorAwareRef = "jpaAuditorAware")去掉就行

参考链接

©2020 edoou.com   京ICP备16001874号-3