Java 尝试在eclipse中配置基本spring启动项目时发生未满足的PendencyException异常
我正试图在eclipse(使用STS)中配置一个基本的spring引导项目,在该项目中,我想定义一个控制器类和一个实体类,以便在控制器中自动连接,但我遇到了一个异常。我一直想把它修好,但现在我没有主意了,有人能帮我吗 一切正常(我可以在控制器中获取实体对象,http请求“”以正确的方式返回我的视图)。。。直到我在“Entidad”类中定义一个构造并得到异常 这是我的代码:Java 尝试在eclipse中配置基本spring启动项目时发生未满足的PendencyException异常,java,spring,eclipse,spring-boot,Java,Spring,Eclipse,Spring Boot,我正试图在eclipse(使用STS)中配置一个基本的spring引导项目,在该项目中,我想定义一个控制器类和一个实体类,以便在控制器中自动连接,但我遇到了一个异常。我一直想把它修好,但现在我没有主意了,有人能帮我吗 一切正常(我可以在控制器中获取实体对象,http请求“”以正确的方式返回我的视图)。。。直到我在“Entidad”类中定义一个构造并得到异常 这是我的代码: package com.example.springMVC_ejemplo; import org.springframe
package com.example.springMVC_ejemplo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringMvcBahiaProjectApplication {
public static void main(String[] args) {
SpringApplication.run(SpringMvcBahiaProjectApplication.class, args);
}
}
我得到的例外是:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.6.RELEASE)
2020-04-07 20:26:57.072 INFO 18236 --- [ main] c.e.s.SpringMvcBahiaProjectApplication : Starting SpringMvcBahiaProjectApplication on DESKTOP-40IFSQT with PID 18236 (C:\Users\ignac\OneDrive\Documentos\eclipse-workspace\springMVC_BahiaProject\target\classes started by ignac in C:\Users\ignac\OneDrive\Documentos\eclipse-workspace\springMVC_BahiaProject)
2020-04-07 20:26:57.075 INFO 18236 --- [ main] c.e.s.SpringMvcBahiaProjectApplication : No active profile set, falling back to default profiles: default
2020-04-07 20:26:57.682 INFO 18236 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-04-07 20:26:57.689 INFO 18236 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-04-07 20:26:57.689 INFO 18236 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-04-07 20:26:57.742 INFO 18236 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-04-07 20:26:57.743 INFO 18236 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 639 ms
2020-04-07 20:26:57.777 WARN 18236 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myController': Unsatisfied dependency expressed through field 'entidad'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entidad' defined in file [C:\Users\ignac\OneDrive\Documentos\eclipse-workspace\springMVC_BahiaProject\target\classes\com\example\springMVC_ejemplo\model\Entidad.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2020-04-07 20:26:57.779 INFO 18236 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2020-04-07 20:26:57.787 INFO 18236 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-04-07 20:26:57.870 ERROR 18236 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.example.springMVC_ejemplo.model.Entidad required a bean of type 'java.lang.String' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'java.lang.String' in your configuration.
提前谢谢 您的
实体不应是@服务
。(由于Spring不知道将哪些字符串传递到构造函数中,因此会出现异常)
删除注释,不要将其@Autowire连接到@Controller中。毕竟,它是一个简单的模型对象。您的实体
不应该是@服务
。(由于Spring不知道将哪些字符串传递到构造函数中,因此会出现异常)
删除注释,不要将其@Autowire连接到@Controller中。毕竟,它是一个简单的模型对象
从Entidad bean类中删除@Service李>
不向Entidad类添加参数构造函数,否则spring容器将无法实例化Entidad类或删除@Autowire
从Entidad bean类中删除@Service李>
不向Entidad类添加参数构造函数,否则spring容器将无法实例化Entidad类或删除@Autowire
所以,问题是我的方法是错误的,我不能在我的控制器中注入这种对象。。。谢谢。您通常会有存储库,然后从数据库中选择这些实体(只是一个示例)。因此,如果我最初的想法是在Entidad中创建一个方法来创建一个示例对象列表(不将这一责任交给控制器),那么最好只为此创建一个存储库,如果它只是“实践”的内存列表,您可以在\@控制器中创建它。或底层\@服务。但是,稍后在使用数据库时,您很可能会使用存储库/dao。因此,问题是我的方法是错误的,我无法将此类对象注入控制器中。。。谢谢。您通常会有存储库,然后从数据库中选择这些实体(只是一个示例)。因此,如果我最初的想法是在Entidad中创建一个方法来创建一个示例对象列表(不将这一责任交给控制器),那么最好只为此创建一个存储库,如果它只是“实践”的内存列表,您可以在\@控制器中创建它。或底层\@服务。但是,稍后在使用数据库时,很可能会使用存储库/dao。
package com.example.springMVC_ejemplo.model;
import org.springframework.stereotype.Service;
@Service
public class Entidad {
private String nombre;
private String atributo1;
private String atributo2;
private Entidad(String nombre, String atributo1, String atributo2) {
super();
this.nombre = nombre;
this.atributo1 = atributo1;
this.atributo2 = atributo2;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getAtributo1() {
return atributo1;
}
public void setAtributo1(String atributo1) {
this.atributo1 = atributo1;
}
public String getAtributo2() {
return atributo2;
}
public void setAtributo2(String atributo2) {
this.atributo2 = atributo2;
}
}
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.6.RELEASE)
2020-04-07 20:26:57.072 INFO 18236 --- [ main] c.e.s.SpringMvcBahiaProjectApplication : Starting SpringMvcBahiaProjectApplication on DESKTOP-40IFSQT with PID 18236 (C:\Users\ignac\OneDrive\Documentos\eclipse-workspace\springMVC_BahiaProject\target\classes started by ignac in C:\Users\ignac\OneDrive\Documentos\eclipse-workspace\springMVC_BahiaProject)
2020-04-07 20:26:57.075 INFO 18236 --- [ main] c.e.s.SpringMvcBahiaProjectApplication : No active profile set, falling back to default profiles: default
2020-04-07 20:26:57.682 INFO 18236 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-04-07 20:26:57.689 INFO 18236 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-04-07 20:26:57.689 INFO 18236 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-04-07 20:26:57.742 INFO 18236 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-04-07 20:26:57.743 INFO 18236 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 639 ms
2020-04-07 20:26:57.777 WARN 18236 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myController': Unsatisfied dependency expressed through field 'entidad'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entidad' defined in file [C:\Users\ignac\OneDrive\Documentos\eclipse-workspace\springMVC_BahiaProject\target\classes\com\example\springMVC_ejemplo\model\Entidad.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2020-04-07 20:26:57.779 INFO 18236 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2020-04-07 20:26:57.787 INFO 18236 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-04-07 20:26:57.870 ERROR 18236 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.example.springMVC_ejemplo.model.Entidad required a bean of type 'java.lang.String' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'java.lang.String' in your configuration.