I have a Webflux
app that restarts automatically after a few minutes of usage, sometimes I can use it for a couple of hours
then suddenly it restarts, when it restarts an error pops up saying duplicate EnhancerBySpringCGLIB
. I know this question has been asked before and I tried the suggested solutions but none worked. below is my log
2020-07-26 10:18:00.515 INFO 23080 --- [ restartedMain] u.s.w.NcitoAfricaApplication : Started NcitoAfricaApplication in 5.37 seconds (JVM running for 6.719)
2020-07-26 10:18:00.523 DEBUG 23080 --- [ restartedMain] o.s.b.d.r.Restarter : Creating new Restarter for thread Thread[main,5,main]
2020-07-26 10:18:00.523 DEBUG 23080 --- [ restartedMain] o.s.b.d.r.Restarter : Immediately restarting application
2020-07-26 10:18:00.523 DEBUG 23080 --- [ restartedMain] o.s.b.d.r.Restarter : Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@1cbd5ea1
2020-07-26 10:18:00.523 DEBUG 23080 --- [ restartedMain] o.s.b.d.r.Restarter : Starting application ubuntu.software.web.NcitoAfricaApplication with URLs [file:/C:/Users/alungu5/IdeaProjects/Tubombeko/]
2020-07-26 10:30:15.181 DEBUG 23080 --- [ File Watcher] o.s.b.d.r.Restarter : Restarting application
2020-07-26 10:30:15.185 DEBUG 23080 --- [ Thread-385] o.s.b.d.r.Restarter : Stopping application
2020-07-26 10:30:15.187 DEBUG 23080 --- [ Thread-385] onfigReactiveWebServerApplicationContext : Closing org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext@6de721d1, started on Sun Jul 26 10:17:55 SAST 2020
2020-07-26 10:30:15.195 DEBUG 23080 --- [ Thread-385] o.s.c.s.DefaultLifecycleProcessor : Stopping beans in phase 2147483647
2020-07-26 10:30:15.201 DEBUG 23080 --- [ Thread-385] o.s.c.s.DefaultLifecycleProcessor : Bean 'webServerGracefulShutdown' completed its stop procedure
2020-07-26 10:30:15.201 DEBUG 23080 --- [ Thread-385] o.s.c.s.DefaultLifecycleProcessor : Stopping beans in phase 2147483646
2020-07-26 10:30:15.217 DEBUG 23080 --- [ Thread-385] o.s.c.s.DefaultLifecycleProcessor : Bean 'webServerStartStop' completed its stop procedure
2020-07-26 10:30:15.217 DEBUG 23080 --- [ Thread-385] o.s.c.s.DefaultLifecycleProcessor : Stopping beans in phase 0
2020-07-26 10:30:19.433 DEBUG 23080 --- [ Thread-385] o.s.b.d.r.Restarter : Created RestartClassLoader org.springframework.boot.devtools.restart.classloader.RestartClassLoader@45d8b2bd
2020-07-26 10:30:19.433 DEBUG 23080 --- [ Thread-385] o.s.b.d.r.Restarter : Starting application ubuntu.software.web.NcitoAfricaApplication with URLs [file:/C:/Users/alungu5/IdeaProjects/Tubombeko/]
2020-07-26 10:30:19.571 DEBUG 23080 --- [ restartedMain] .c.l.ClasspathLoggingApplicationListener : Application started with classpath: [file:/C:/Users/alungu5/IdeaProjects/Tubombeko/]
. . . .
springframework.beans.factory.BeanCreationException: Error creating bean with name 'countryServiceImpl' defined in file [C:\Users\alungu5\IdeaProjects\Tubombeko\service\target\classes\ubuntu\software\service\country\CountryServiceImpl.class]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class ubuntu.software.service.country.CountryServiceImpl: Common causes of this problem include using a final class or a non-visible class; nested exception is org.springframework.cglib.core.CodeGenerationException: java.lang.LinkageError-->loader 'app' attempted duplicate class definition for ubuntu.software.service.country.CountryServiceImpl$$EnhancerBySpringCGLIB$$906ca3da. (ubuntu.software.service.country.CountryServiceImpl$$EnhancerBySpringCGLIB$$906ca3da is in module ubuntu.software.service of loader 'app')
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:603) ~[spring-beans-5.2.7.RELEASE.jar:?]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.7.RELEASE.jar:?]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.7.RELEASE.jar:?]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.7.RELEASE.jar:?]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.7.RELEASE.jar:?]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.7.RELEASE.jar:?]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:893) ~[spring-beans-5.2.7.RELEASE.jar:?]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.7.RELEASE.jar:?]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.7.RELEASE.jar:?]
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:62) ~[spring-boot-2.3.1.RELEASE.jar:?]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.1.RELEASE.jar:?]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.1.RELEASE.jar:?]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.3.1.RELEASE.jar:?]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.1.RELEASE.jar:?]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.1.RELEASE.jar:?]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.1.RELEASE.jar:?]
at ubuntu.software.web.NcitoAfricaApplication.main(NcitoAfricaApplication.java:21) [classes/:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:564) ~[?:?]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.3.1.RELEASE.jar:?]
Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class ubuntu.software.service.country.CountryServiceImpl: Common causes of this problem include using a final class or a non-visible class; nested exception is org.springframework.cglib.core.CodeGenerationException: java.lang.LinkageError-->loader 'app' attempted duplicate class definition for ubuntu.software.service.country.CountryServiceImpl$$EnhancerBySpringCGLIB$$906ca3da. (ubuntu.software.service.country.CountryServiceImpl$$EnhancerBySpringCGLIB$$906ca3da is in module ubuntu.software.service of loader 'app')
at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:208) ~[spring-aop-5.2.7.RELEASE.jar:?]
at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:110) ~[spring-aop-5.2.7.RELEASE.jar:?]
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:471) ~[spring-aop-5.2.7.RELEASE.jar:?]
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:350) ~[spring-aop-5.2.7.RELEASE.jar:?]
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:299) ~[spring-aop-5.2.7.RELEASE.jar:?]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:431) ~[spring-beans-5.2.7.RELEASE.jar:?]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.2.7.RELEASE.jar:?]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.7.RELEASE.jar:?]
... 21 more
Caused by: org.springframework.cglib.core.CodeGenerationException: java.lang.LinkageError-->loader 'app' attempted duplicate class definition for ubuntu.software.service.country.CountryServiceImpl$$EnhancerBySpringCGLIB$$906ca3da. (ubuntu.software.service.country.CountryServiceImpl$$EnhancerBySpringCGLIB$$906ca3da is in module ubuntu.software.service of loader 'app')
at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:558) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:363) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:585) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:110) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:108) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54) ~[spring-core-5.2.7.RELEASE.jar:?]
at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:134) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:319) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:572) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:419) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.aop.framework.ObjenesisCglibAopProxy.createProxyClassAndInstance(ObjenesisCglibAopProxy.java:57) ~[spring-aop-5.2.7.RELEASE.jar:?]
at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:205) ~[spring-aop-5.2.7.RELEASE.jar:?]
at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:110) ~[spring-aop-5.2.7.RELEASE.jar:?]
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:471) ~[spring-aop-5.2.7.RELEASE.jar:?]
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:350) ~[spring-aop-5.2.7.RELEASE.jar:?]
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:299) ~[spring-aop-5.2.7.RELEASE.jar:?]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:431) ~[spring-beans-5.2.7.RELEASE.jar:?]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.2.7.RELEASE.jar:?]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.7.RELEASE.jar:?]
... 21 more
Caused by: java.lang.LinkageError: loader 'app' attempted duplicate class definition for ubuntu.software.service.country.CountryServiceImpl$$EnhancerBySpringCGLIB$$906ca3da. (ubuntu.software.service.country.CountryServiceImpl$$EnhancerBySpringCGLIB$$906ca3da is in module ubuntu.software.service of loader 'app')
at java.lang.ClassLoader.defineClass1(Native Method) ~[?:?]
at java.lang.System$2.defineClass(System.java:2196) ~[?:?]
at java.lang.invoke.MethodHandles$Lookup.defineClass(MethodHandles.java:1648) ~[?:?]
at jdk.internal.reflect.GeneratedMethodAccessor43.invoke(Unknown Source) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:564) ~[?:?]
at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:555) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:363) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:585) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:110) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:108) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54) ~[spring-core-5.2.7.RELEASE.jar:?]
at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:134) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:319) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:572) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:419) ~[spring-core-5.2.7.RELEASE.jar:?]
at org.springframework.aop.framework.ObjenesisCglibAopProxy.createProxyClassAndInstance(ObjenesisCglibAopProxy.java:57) ~[spring-aop-5.2.7.RELEASE.jar:?]
at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:205) ~[spring-aop-5.2.7.RELEASE.jar:?]
at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:110) ~[spring-aop-5.2.7.RELEASE.jar:?]
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:471) ~[spring-aop-5.2.7.RELEASE.jar:?]
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:350) ~[spring-aop-5.2.7.RELEASE.jar:?]
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:299) ~[spring-aop-5.2.7.RELEASE.jar:?]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:431) ~[spring-beans-5.2.7.RELEASE.jar:?]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.2.7.RELEASE.jar:?]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.7.RELEASE.jar:?]
... 21 more
This is how my CountryServiceImpl
looks like
@Service
@Qualifier("CountryServiceImpl")
public class CountryServiceImpl extends BaseService<Country, CountryDto, CountryRepository> implements ICountryService {
@Lazy
@Autowired
private CountryRepository countryRepository;
@Lazy
@Autowired
private IJWTUtil jwtUtil;
@Override
public CountryRepository getRepository() {
return countryRepository;
}
@Override
public Mono<Country> createModel(CountryDto countryDto, Map<String, Claim> claims) {
Country country = getModelMapper().map(countryDto, Country.class);
country.setStatus(BaseModel.Status.ENABLED);
return Mono.just(country);
}
@Override
public Mono<Country> createUpdateModel(CountryDto countryDto, Map<String, Claim> claims) {
return getRepository().findById(countryDto.getId())
.flatMap(country -> Mono.just(country.toBuilder()
.name(countryDto.getName())
.build()));
}
@Override
public IJWTUtil getJwtUtil() {
return jwtUtil;
}
}
My ICountryService
interface
public interface ICountryService extends IBaseService<Country, CountryDto> {
}
My BaseService
Impl
@Transactional
public abstract class BaseService<T extends BaseModel, V extends BaseDto, E extends BaseRepository<T>> implements IBaseService<T, V> {
@Getter
private final ModelMapper modelMapper;
abstract public E getRepository();
public abstract Mono<T> createModel(V v, Map<String, Claim> claims);
public abstract Mono<T> createUpdateModel(V v, Map<String, Claim> claims);
public abstract IJWTUtil getJwtUtil();
public Mono<T> validate(Mono<T> t) {
return t;
}
public Flux<T> validate(Flux<T> t) {
return t;
}
public BaseService() {
modelMapper = new ModelMapper();
}
@Override
public Mono<ResponseEntity<?>> create(V v, Optional<String> authorizationHeader) {
Map<String, Claim> claims = authorizationHeader.map(mAuthorizationHeader -> getJwtUtil().getAllClaims(mAuthorizationHeader)).orElse(new HashMap<>());
Mono<T> model = this.createModel(v, claims);
return create(model, claims)
.flatMap(__ -> Mono.<ResponseEntity<?>>just(ResponseEntity
.status(HttpStatus.CREATED)
.body(Response.builder()
.payLoad(HttpStatus.CREATED)
.build())))
.switchIfEmpty(Mono.defer(() ->
Mono.just(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(Response.builder()
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.message("Unknown error occurred, try again")
.build()))));
}
@Override
public Mono<T> create(Mono<T> t, Map<String, Claim> claims) {
return validate(t)
.flatMap(k -> getRepository().save(k));
}
@Override
public Flux<T> createAll(List<T> t, Map<String, Claim> claims) {
return validate(Flux.fromIterable(t))
.thenMany(getRepository().saveAll(t));
}
}
And finally My IBaseService
Interface
public interface IBaseService<T extends BaseModel, V extends BaseDto> {
Mono<ResponseEntity<?>> create(V v, Optional<String> authorizationHeader);
Mono<T> create(Mono<T> t, Map<String, Claim> claims);
Flux<T> createAll(List<T> t, Map<String, Claim> claims);
}
After doing some reading I found out that CGLIB
issues are usually caused by the @Transactional
annotation as it generates proxies, a way around is to put it @Transactional
on the parent of which I have but the error still pops up after the app restarts.
Ok, so I found the culprit, turns out it was the Java 9
module-info.java
, I encountered an issue making RabbitMq
work, all files were imported correctly but it was pointing to say the error was coming from my module-info.java
so I removed it, after removing it I have not had this problem (and RabbitMq worked too), from my understanding, their are internal classes that were not put on the allow list that need to access my classes and because of that the app kept failing, the app has been working since then without any problems.