先声明本人并不是标题党,如果看了本篇文章并且认为没有得到任何收获,请您随便留言骂我,本人绝不还口,已经对springboot了如指掌大大神,求放过!

  不BB了,直接上代码,请各位在自己的springboot项目随便一个包下复制进去如下类(不要修改什么东西),如果你的springboot还能站起来算我输!

@Component public class Environment { }

  运行springboot的启动类会报如下错误,然后你删除这个类,你的springboot又能健步如飞了,你可能就会怀疑人生了,这代码有毒。先说明我的springboot是2.1.7.RELEASE,我也试了最新的2.2,报错基本一致!

复制代码
2019-11-02 00:42:46.181  INFO 13568 --- [           main] com.rdpaas.platform.demo.RunApplication  : Starting RunApplication on DESKTOP-9KL4U5L with PID 13568 (E:\project2018\platform\demo\target\classes started by 49519 in E:\project2018\platform) 2019-11-02 00:42:46.183  INFO 13568 --- [           main] com.rdpaas.platform.demo.RunApplication  : No active profile set, falling back to default profiles: default 2019-11-02 00:42:48.490  WARN 13568 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'methodValidationPostProcessor' defined in class path resource [org/springframework/boot/autoconfigure/validation/ValidationAutoConfiguration.class]: Unsatisfied dependency expressed through method 'methodValidationPostProcessor' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.core.env.Environment' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} 2019-11-02 00:42:48.499  INFO 13568 --- [           main] ConditionEvaluationReportLoggingListener :   Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2019-11-02 00:42:48.615 ERROR 13568 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   :   *************************** APPLICATION FAILED TO START ***************************  Description:  Parameter 0 of method methodValidationPostProcessor in org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration required a bean of type 'org.springframework.core.env.Environment' that could not be found.   Action:  Consider defining a bean of type 'org.springframework.core.env.Environment' in your configuration.
复制代码

  如上很普通,谁都可能加上的类,就这样一个简单的类,居然可以直接导致springboot站不起来了,如果你认命了,其实也很好解决,你可能会换个名字试试,或者你压根就不会用到这个类,或者是你给@Component("env"),加个别名,可能就碰巧解决了这个问题,那么这时候你可能会当成springboot已经规定了你不能使用关键字environment作为bean的名称,那么这个问题就变得一文不值了,因为你已经认命了,不让我用我不用就行了,以后一辈子都不用这个类名就好了。眼不见心不烦,我用简称Env还来的省事点。不过我个人认为我们遇到难题应该迎难而上,不能随便认命,我们都是骄傲的程序员。应该抱着希望遇到难题的心态,积极去面对难题,多解决一些疑难杂症,用知识和经验武装自己,努力成长,迎娶白富美,走上人生巅峰!如果看到这里觉得不认命的请跟着我一起看看这个问题到底为啥会出现吧!

  接下来我们一步步来找到问题的根源,为啥用了这个类,springboot就不举了?首先我们从启动的错误提示中找到唯一的关键信息:method methodValidationPostProcessor in org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration required a bean of type 'org.springframework.core.env.Environment' that could not be found。从这句话可以看出来在:org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration这个类中的这个方法:methodValidationPostProcessor 中需要一个:org.springframework.core.env.Environment类的对象作为参数,但是他找不到,看这个名字和我们自己定义的一样,先在idea中找到如上类的methodValidationPostProcessor方法的源码所在:

   为了验证图中的猜想,由于我这不是源码编译的,所以只能自己模仿这个类同样使用@Bean修饰一个方法看看是不是里面的参数都是完全按照参数名称注入的(可以先注释掉之前的Environment类排除那个类的影响),如下

复制代码
package com.rdpaas.platform.demo.env; import org.springframework.stereotype.Component; /**  * 用作测试的bean  * @author: rongdi  * @date: 2019-11-2 0:12  */ @Component public class TestBean { }