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

【已解决】java的spring boot程序报错:Parameter 0 of constructor in required a bean of type long that could not be found

Java crifan 5934浏览 0评论
折腾:
【未解决】VSCode中搭建Spring Boot的示例程序
期间,虽然
mvn spring-boot:run
可以继续运行了,但是最后出错了:
2020-01-18 20:48:06.237  WARN 19665 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'greeting': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'long' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2020-01-18 20:48:06.240  INFO 19665 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-01-18 20:48:06.256  INFO 19665 --- [  restartedMain] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-01-18 20:48:06.430 ERROR 19665 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   :

***************************
APPLICATION FAILED TO START
***************************

Description:
Parameter 0 of constructor in com.crifan.xxx.Greeting required a bean of type 'long' that could not be found.

Action:

Consider defining a bean of type 'long' in your configuration.
Parameter 0 of constructor in Greeting required a bean of type long that could not be found
spring boot – Parameter 0 of constructor in required a bean of type ‘java.lang.String’ that could not be found – Stack Overflow
java – Parameter 0 of constructor required a bean that could not be found – Stack Overflow
java – Parameter 0 of constructor in ….. Spring Boot – Stack Overflow
java – Spring – Error Parameter 0 of constructor in Service required a bean of type Configuration that could not be found – Stack Overflow
Guide to @ConfigurationProperties in Spring Boot | Baeldung
去看看此处的
com.crifan.xxx.Greeting
没太看懂,需要怎么改
尝试加:
@Configuration
@ConfigurationProperties("id")
结果会报错:
Configuration cannot be resolved to a type Java(16777218)
去快速修复
然后去导入
import org.springframework.context.annotation.Configuration;
即可。
同理,对于:
ConfigurationProperties cannot be resolved to a typeJava(16777218)
去导入
import org.springframework.boot.context.properties.ConfigurationProperties;
是不是这样就意味着:
可以自动给id这个属性,去Configuration配置了?
再去运行
mvn spring-boot:run
看看是否能解决问题。
结果错误有其他变化了:
✗ mvn spring-boot:run
[INFO] Scanning for projects...
。。。
[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:11)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.122 s
[INFO] Finished at: 2020-01-18T21:13:33+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
现在从错误
Parameter 0 of constructor in com.crifan.xxx.Greeting required a bean of type 'long' that could not be found.
对应的代码中
    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }
可以看出大概意思:
Greeting的构造函数中,第一个参数是long的,但是此处缺少了
所以问题变成:
搞清楚,此处运行
mvn spring-boot:run
内部运行的机制,为何没有传递参数给Greeting
然后去试试:
【未解决】spring boot尝试添加@ConstructorBinding以解决错误Parameter 0 of constructor in required a bean of type
恢复到最基本的
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Greeting {


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


    private final long id;
    private final String content;


    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }


    public long getId() {
        return id;
    }


    public String getContent() {
        return content;
    }


}
结果又是最开始的错误
***************************
APPLICATION FAILED TO START
***************************


Description:


Parameter 0 of constructor in com.crifan.xxx.Greeting required a bean of type 'long' that could not be found.


Action:


Consider defining a bean of type 'long' in your configuration.

【总结】
此处之前是参考了多个教程:
Getting Started · Building a RESTful Web Service
Getting Started · Serving Web Content with Spring MVC
然后给代码:
src/server/xxx/xxx/src/main/java/com/crifan/xxx/Greeting.java
public class Greeting {
加上了:
@SpringBootApplication
public class Greeting {
然后之后就是此处错误
Parameter 0 of constructor in com.crifan.xxx.Greeting required a bean of type 'long' that could not be found.
了,而后续有试了多种其他annotation:
  • @ConstructorBinding
  • @EnableConfigurationProperties
  • @ConfigurationPropertiesScan
  • @ConfigurationProperties(“id”)
导致更多其他错误。
最后是参考
Getting Started · Serving Web Content with Spring MVC
才搞懂:
此处Greeting中,是不应该加上@SpringBootApplication的
应该是普通的纯的class,即:
src/server/xxx/xxx/src/main/java/com/crifan/xxx/Greeting.java
去掉额外的annotation,只保留普通的类的变量:
package com.crifan.xxx;


// import org.springframework.boot.SpringApplication;
// import org.springframework.boot.autoconfigure.SpringBootApplication;
// import org.springframework.boot.context.properties.ConfigurationProperties;
// import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
// import org.springframework.boot.context.properties.ConstructorBinding;
// import org.springframework.boot.context.properties.EnableConfigurationProperties;
// import org.springframework.context.annotation.Configuration;


// @EnableConfigurationProperties(Greeting::class)
// @Configuration
// @ConfigurationProperties("id")
// @SpringBootApplication
// @ConstructorBinding
// @ConfigurationPropertiesScan
public class Greeting {


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


    private final long id;
    private final String content;


    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }


    public long getId() {
        return id;
    }


    public String getContent() {
        return content;
    }


}


同时,之前已有GreetingController:
src/server/xxx/xxx/src/main/java/com/crifan/xxx/GreetingController.java
package com.crifan.xxx;


import java.util.concurrent.atomic.AtomicLong;


import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;


@RestController
public class GreetingController {


    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();


    @GetMapping("/greeting")
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }
}
然后再去加上:
src/server/xxx/xxx/src/main/java/com/crifan/xxx/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);
    }


}
用于启动服务的SpringBootApplication的SpringApplication.run
即可正常运行
mvn spring-boot:run
消除当前错误了。

转载请注明:在路上 » 【已解决】java的spring boot程序报错:Parameter 0 of constructor in required a bean of type long that could not be found

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.172 seconds, using 22.18MB memory