Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'jpaAuditingHandler' defined in null on application startup using spring boot

After upgrading to spring boot 2.1 from 2.0.5, I am getting following error at application start up:

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.

The error gets resolved if I specify following in application.yaml:

spring:
  main:
    allow-bean-definition-overriding: true

What will this allow-bean-definition-overriding do ?

Also, I don't want to do enable that. How can I configure 'jpaAuditingHandler' to get away with this error

like image 389
Learner Avatar asked Dec 14 '18 22:12

Learner


2 Answers

You will get this error if you have multiple @EnableJpaAuditing declarations in your Spring config files. Just declare it once.

like image 144
Julian Avatar answered Nov 01 '22 14:11

Julian


I developed a library, which required @EnableJpaAuditing. Users could have their own @Configuration classes, which could be declaring @EnableJpaAuditing. This would be leading to application context failure because of multiple @EnableJpaAuditing in one context.

So, we had to declare @EnableJpaAuditing only if the user context has not already declared it.

Fortunately, the Spring Boot allows conditions at context.

Next solution (in Kotlin) is the @Configuration, which is applied only if @EnableJpaAuditing was not applied.

@Configuration
@ConditionalOnMissingBean(name=["jpaAuditingHandler"])
@EnableJpaAuditing
class JpaAuditingNonConflictingDeclaration
like image 1
Vladimir Shefer Avatar answered Nov 01 '22 13:11

Vladimir Shefer