Spring 启动 webflux 应用程序重新启动并导致重复的 EnhancerBySpringCGLIB

Spring boot webflux app restarts and causes duplicate EnhancerBySpringCGLIB

我有一个 Webflux 应用程序,在使用几分钟后会自动重启,sometimes I can use it for a couple of hours 然后突然重启,当它重启时会弹出一个错误,提示重复 EnhancerBySpringCGLIB。我知道之前有人问过这个问题,我尝试了建议的解决方案,但 none 奏效了。下面是我的日志

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$6ca3da. (ubuntu.software.service.country.CountryServiceImpl$$EnhancerBySpringCGLIB$6ca3da 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[=12=](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$6ca3da. (ubuntu.software.service.country.CountryServiceImpl$$EnhancerBySpringCGLIB$6ca3da 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$6ca3da. (ubuntu.software.service.country.CountryServiceImpl$$EnhancerBySpringCGLIB$6ca3da 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.apply(AbstractClassGenerator.java:110) ~[spring-core-5.2.7.RELEASE.jar:?]
        at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.apply(AbstractClassGenerator.java:108) ~[spring-core-5.2.7.RELEASE.jar:?]
        at org.springframework.cglib.core.internal.LoadingCache.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$6ca3da. (ubuntu.software.service.country.CountryServiceImpl$$EnhancerBySpringCGLIB$6ca3da is in module ubuntu.software.service of loader 'app')
        at java.lang.ClassLoader.defineClass1(Native Method) ~[?:?]
        at java.lang.System.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.apply(AbstractClassGenerator.java:110) ~[spring-core-5.2.7.RELEASE.jar:?]
        at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.apply(AbstractClassGenerator.java:108) ~[spring-core-5.2.7.RELEASE.jar:?]
        at org.springframework.cglib.core.internal.LoadingCache.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

这就是我的 CountryServiceImpl 的样子

@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;
    }
}

我的ICountryService界面

public interface ICountryService extends IBaseService<Country, CountryDto> {
}

我的BaseService实现

@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));
    }
}

最后是我的 IBaseService 界面

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);
}

阅读后我发现 CGLIB 问题通常是由 @Transactional 注释引起的,因为它会生成代理,解决方法是将它 @Transactional 放在父级上其中我有,但应用程序重新启动后仍然弹出错误。

好的,所以我找到了罪魁祸首,原来是 Java 9 module-info.java,我遇到了使 RabbitMq 工作的问题,所有文件都已正确导入,但它指向说错误来自我的 module-info.java 所以我删除了它,在删除它之后我没有遇到这个问题(并且 RabbitMq 也工作过),根据我的理解,它们是内部的 类 没有被放置在需要访问我的 类 的允许列表上,并且由于该应用程序一直失败,该应用程序从那时起一直在运行,没有任何问题。