最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【无法解决】spring boot尝试添加@ConstructorBinding以解决错误Parameter 0 of constructor in required a bean of type

spring crifan 1458浏览 0评论
折腾:
【已解决】java的spring boot程序报错:Parameter 0 of constructor in required a bean of type long that could not be found
期间,看到log中的建议:
Action:
Consider defining a bean of type 'long' in your configuration.
Consider adding @ConstructorBinding to com.crifan.xxx.Greeting if you intended to use constructor-based configuration property binding.
去试试加上@ConstructorBinding,结果出错:
去修复
就可以了。
再去运行看看
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-01-31 10:22:59.045 ERROR 12387 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'greeting': @EnableConfigurationProperties or @ConfigurationPropertiesScan must be used to add @ConstructorBinding type com.crifan.xxx.Greeting
        at org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator.validate(ConfigurationPropertiesBeanDefinitionValidator.java:66) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator.postProcessBeanFactory(ConfigurationPropertiesBeanDefinitionValidator.java:45) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:286) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:174) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:706) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at com.crifan.xxx.Greeting.main(Greeting.java:16) [classes/:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_112]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_112]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_112]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_112]
        at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.2.2.RELEASE.jar:2.2.2.RELEASE]


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
@EnableConfigurationProperties or @ConfigurationPropertiesScan must be used to add @ConstructorBinding type
Misleading error message with immutable configuration properties and @Component · Issue #18646 · spring-projects/spring-boot
ConfigurationProperties with constructor binding cannot be mocked · Issue #18652 · spring-projects/spring-boot
想要试试
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@EnableConfigurationProperties(Greeting::class)
结果class报错
再去试试
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;

@ConfigurationPropertiesScan
结果:
变成别的错误了
✗ mvn spring-boot:run
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.crifan:xxx >-------------------
[INFO] Building xxx 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:2.2.2.RELEASE:run (default-cli) > test-compile @ xxx >>>
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ xxx ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ xxx ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ xxx ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/crifan/dev/dev_root/projects/xxx/src/server/xxx/xxx/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ xxx ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< spring-boot-maven-plugin:2.2.2.RELEASE:run (default-cli) < test-compile @ xxx <<<
[INFO]
[INFO]
[INFO] --- spring-boot-maven-plugin:2.2.2.RELEASE:run (default-cli) @ xxx ---
[INFO] Attaching agents: []
Exception in thread "main" java.lang.Error: Unresolved compilation problem:


        at com.crifan.xxx.Greeting.main(Greeting.java:17)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.462 s
[INFO] Finished at: 2020-01-31T10:39:06+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.2.2.RELEASE:run (default-cli) on project xxx: Application finished with exit code: 1 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] 
http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.2.2.RELEASE:run default-cli on project xxx: Application finished with exit code  1 Help 1
Guide to @EnableConfigurationProperties | Baeldung
Guide to @ConfigurationProperties in Spring Boot | Baeldung
说是Spring Boot 2.2,无需再加上
  • @Component
  • @Configuration
  • @EnableConfigurationProperties
所以去掉,变成:
// @Configuration
@ConfigurationProperties("id")
@SpringBootApplication
@ConstructorBinding
// @EnableConfigurationProperties(Greeting::class)
@ConfigurationPropertiesScan
public class Greeting {
结果:
出现之前的错误,所以还是改回去,加上@Configuration
但是又始终出现之前的错误:
2020-01-31 10:47:52.700 ERROR 13660 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'greeting': @EnableConfigurationProperties or @ConfigurationPropertiesScan must be used to add @ConstructorBinding type com.crifan.xxx.Greeting
        at org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator.validate(ConfigurationPropertiesBeanDefinitionValidator.java:66) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator.postProcessBeanFactory(ConfigurationPropertiesBeanDefinitionValidator.java:45) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:286) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:174) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:706) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
        at com.crifan.xxx.Greeting.main(Greeting.java:20) [classes/:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_112]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_112]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_112]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_112]
        at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.2.2.RELEASE.jar:2.2.2.RELEASE]


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
很是奇怪啊
感觉有缓存?
故意弄个语法错误:
@ConfigurationP roperties("id")
结果报错倒是和之前上面的错误一样了:
[INFO] --- spring-boot-maven-plugin:2.2.2.RELEASE:run (default-cli) @ xxx ---
[INFO] Attaching agents: []
Exception in thread "main" java.lang.Error: Unresolved compilation problem:


        at com.crifan.xxx.Greeting.main(Greeting.java:19)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.949 s
[INFO] Finished at: 2020-01-31T10:49:06+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.2.2.RELEASE:run (default-cli) on project xxx: Application finished with exit code: 1 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] 
http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
所以貌似上面这个错误,则表示是语法错误?
恢复
@ConfigurationProperties("id")
结果:
还是之前错误
2020-01-31 10:51:07.035 ERROR 13937 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'greeting': @EnableConfigurationProperties or @ConfigurationPropertiesScan must be used to add @ConstructorBinding type com.crifan.xxx.Greeting
即:加上了
@ConfigurationPropertiesScan
感觉没效果。
EnableConfigurationProperties or ConfigurationPropertiesScan must be used to add ConstructorBinding type
A Guide to Spring Boot ConfigurationProperties for Kotlin Data Class
spring-projects/spring-boot – Gitter
只保留
@ConfigurationProperties("id")
结果:
2020-01-31 10:55:27.650 ERROR 14226 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed


org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
ConfigurationProperties (Spring Boot Docs 2.2.4.RELEASE API)
“org.springframework.boot.context.properties
Annotation Type ConfigurationProperties
*
@Target(value={TYPE,METHOD})
@Retention(value=RUNTIME)
@Documented
public @interface ConfigurationProperties
Annotation for externalized configuration. Add this to a class definition or a @Bean method in a @Configuration class if you want to bind and validate some external Properties (e.g. from a .properties file).
Binding is either performed by calling setters on the annotated class or, if @ConstructorBinding is in use, by binding to the constructor parameters.
Note that contrary to @Value, SpEL expressions are not evaluated since property values are externalized.
Since:
1.0.0
Author:
Dave Syer
See Also:
ConfigurationPropertiesScan, ConstructorBinding, ConfigurationPropertiesBindingPostProcessor, EnableConfigurationProperties”
算了,回去参考之前官网教程
3.6. Using the @SpringBootApplication Annotation
@SpringBootApplication
= @EnableAutoConfiguration + @ComponentScan + @Configuration
->
  • @EnableAutoConfiguration
    • enable Spring Boot’s auto-configuration mechanism
  • @ComponentScan
    • enable @Component scan on the package where the application is located (see the best practices)
  • @Configuration
    • allow to register extra beans in the context or import additional configuration classes
【总结】
此处尝试通过添加
  • @ConstructorBinding
  • @EnableConfigurationProperties
  • @ConfigurationPropertiesScan
  • @ConfigurationProperties(“id”)
等写法,都还是没有解决问题。
最后是:
不该给此处的Greeting.java加@SpringBootApplication的,而是
另外单独新建一个java文件
ServingWebContentApplication.java
package com.crifan.xxx;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class ServingWebContentApplication {


    public static void main(String[] args) {
        SpringApplication.run(ServingWebContentApplication.class, args);
    }


}
用于启动服务。就可以了。
具体情况详见:
【已解决】java的spring boot程序报错:Parameter 0 of constructor in required a bean of type long that could not be found

转载请注明:在路上 » 【无法解决】spring boot尝试添加@ConstructorBinding以解决错误Parameter 0 of constructor in required a bean of type

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
80 queries in 0.167 seconds, using 22.20MB memory