我的项目基础是 SpringBoot2.3.1。pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
</dependencies>
项目中只包含三个类文件。
@Import(WebMvcAutoConfigurationAdapter.class)
@Configuration
@Order(2)
public class CustomEnableWebMvcConfiguration extends EnableWebMvcConfiguration {
public CustomEnableWebMvcConfiguration(ResourceProperties resourceProperties,
ObjectProvider<WebMvcProperties> mvcPropertiesProvider,
ObjectProvider<WebMvcRegistrations> mvcRegistrationsProvider, ListableBeanFactory beanFactory) {
super(resourceProperties, mvcPropertiesProvider, mvcRegistrationsProvider, beanFactory);
}
}
@Configuration
@Order(1)
public class CustomEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(final ConfigurableEnvironment environment, final SpringApplication application) {
application.setAllowBeanDefinitionOverriding(true);
}
}
@SpringBootApplication
public class Application {
public static void main(final String[] args) {
SpringApplication.run(Application.class, args);
}
}
spring.factories:
org.springframework.boot.env.EnvironmentPostProcessor=\
com.qiji.testelasticjob.CustomEnvironmentPostProcessor
调试项目会抛出异常:</p>
org.springframework.beans.factory.BeanCreationException:创建类路径资源[com/qiji/testelasticjob/CustomEnableWebMvcConfiguration.class]中定义的名称为“welcomePageHandlerMapping”的bean时出错:通过工厂方法进行的bean实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.boot.autoconfigure.web.servlet.WelcomePageHandlerMapping]:工厂方法“welcomePageHandlerMapping”抛出异常;嵌套异常是 java.lang.IllegalAccessError: 试图从类 com.qiji.testelasticjob.CustomEnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$b59ce73a 访问类 org.springframework.boot.autoconfigure.web.servlet.WelcomePageHandlerMapping 在 org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] 在 org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:635) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:893) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE] 在 org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] 在 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] 在 com.qiji.testelasticjob.Application.main(Application.java:17) [classes/:na] 原因:org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.boot.autoconfigure.web.servlet.WelcomePageHandlerMapping]:工厂方法'welcomePageHandlerMapping'抛出异常;嵌套异常是 java.lang.IllegalAccessError: 试图从类 com.qiji.testelasticjob.CustomEnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$b59ce73a 访问类 org.springframework.boot.autoconfigure.web.servlet.WelcomePageHandlerMapping 在 org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] 在 org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:650) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] ...省略了20个常用框架 引起:java.lang.IllegalAccessError:试图从类 com.qiji.testelasticjob.CustomEnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$b59ce73a 访问类 org.springframework.boot.autoconfigure.web.servlet.WelcomePageHandlerMapping 在 com.qiji.testelasticjob.CustomEnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$b59ce73a.welcomePageHandlerMapping() ~[classes/:na] 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121] 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121] 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121] 在 java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121] 在 org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] ...省略了21个常用框架
请帮帮我!</p>