- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试将 mysql 数据库连接到 spring boot web 服务,但出现此错误,我不知道如何修复...
我的 Controller 是:
@RestController
@RequestMapping("/user")
public class UsersController {
@Autowired
UserService userService;
@RequestMapping("/test")
@ResponseBody
String home(){
return "Hello world";
}
@RequestMapping("/users")
String test(){
return "TEST ";
}
}
我的实体是:
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String username;
private String name;
private String firstname;
private String email;
private String adress;
private String password;
// Constructeur pour l'utilisation des JPA
protected User(){}
public User(String username, String name, String firstname, String email, String adress, String password) {
this.username = username;
this.name = name;
this.firstname = firstname;
this.email = email;
this.adress = adress;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
我的仓库文件:
public interface UserRepository extends CrudRepository<User, Integer>{
List<User> findUserByName(String name);
User findUserByUsername(String username);
}
我的服务:
@Service
@Component
public class UserService {
@Autowired
private UserRepository repoUser;
public List<User> getUser(String name){
return repoUser.findUserByName(name);
}
}
编辑:这是我的堆栈跟踪:
2016-08-05 20:48:39.025 WARN 1849 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'usersController': Unsatisfied dependency expressed through field 'userService': No qualifying bean of type [services.UserService] found for dependency [services.UserService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [services.UserService] found for dependency [services.UserService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2016-08-05 20:48:39.026 INFO 1849 --- [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2016-08-05 20:48:39.031 INFO 1849 --- [ main] o.apache.catalina.core.StandardService : Stopping service Tomcat
2016-08-05 20:48:39.053 INFO 1849 --- [ main] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report enable debug logging (start with --debug)
2016-08-05 20:48:39.073 ERROR 1849 --- [ main] o.s.boot.SpringApplication : Application startup failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'usersController': Unsatisfied dependency expressed through field 'userService': No qualifying bean of type [services.UserService] found for dependency [services.UserService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [services.UserService] found for dependency [services.UserService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:313) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174) [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE]
at com.example.JitBackofficeApplication.main(JitBackofficeApplication.java:13) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_101]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101]
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:na]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [services.UserService] found for dependency [services.UserService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1406) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1057) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
... 24 common frames omitted
我的应用程序属性:
spring.datasource.url=jdbc:mysql://localhost/jitdatabase
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.data.jpa.repositories.enabled=true
这是我的申请文件:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("controllers")
public class JitBackofficeApplication {
public static void main(String[] args) {
SpringApplication.run(JitBackofficeApplication.class, args);
}
}
here theres is my architecture
谁有解决办法?
问候
最佳答案
您的@ComponentScan
注释是错误的。您只需将 Controller 包添加为组件源。
您需要添加包含您的 Controller 和服务的包。
例如,如果您的包结构是:
com.mycompany.controllers
com.mycompany.services
然后将com.mycompany写入@ComponentScan
。
此外,您应该只在服务类上使用 @Service
而不是 @Component
。
关于java - org.springframework.beans.factory.UnsatisfiedDependencyException : Error creating bean with name userController : Unsatisfied dependency,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38752297/
我正在开发一个 spring data jpa 和 thymeleaf 项目,并且遇到了连接查询问题。 我有 3 个表(多对多关系): 1. 类(class) 2. 选项 3. 连接表:options
我正在使用 Spring boot 设置基本的 REST,但遇到一些我不知道如何修复的错误。 希望大家能帮帮我:) 我已经尝试了一些注释修复,但没有用...您可以在下面找到我的代码。也许依赖关系不正确
我正在编写一个 jpa 存储库示例,并且收到类型为 UnsatisfiedDependencyException 的运行时异常。 这是我的程序: @Configuration @EnableJpaRe
我有以下代码和@ComponentScan(basePackages = "com.project.shopping"),包结构为 com.project.shopping.Controller co
错误: Exception encountered during context initialization - cancelling refresh attempt: org.springfram
在我将@Service 添加到 Controller 后,我的单元测试失败了。该项目是 Spring-boot v 2.0.1.RELEASE。我花了很多时间试图找到答案,但没有运气。该测试在我添加
我是 Spring 的新手,我正在尝试制作一个简单的休息应用程序。当我将所有文件放在一个包中时,应用程序运行良好。由于我改变了我的项目组织,我无法构建我的项目。我收到此错误: 2017-09-30 2
它应该打印出在 spring.xml 文件中输入的三角形的坐标 (x, y)。 这是绘图应用程序。 java类 package spring_java_brains_07_injecting_obj
编辑:根据 user7294900 和 @skhussain 建议将 @Repository 和 @Service 添加到 UserDAOImpl 和 UserServiceImpl 类,但是我仍然得
我有一个 ApiRest 类,它有一个名为 myKey 的成员,我试图根据从 abc.properties 文件获取的值来设置该成员。当我使用 @Value 设置变量时,出现以下错误: Caused
我是使用微服务架构的新手,想要从另一个使用 camunda(只读业务流程工具(库))的模块访问 bean。错误未满足的依赖关系来自 camunda 接口(interface),但我在当前模块中不需要来
我在 SpringBoot 上遇到困难。我正在学习一门类(class),我的代码与我的教授完全相同,但我的代码给了我这个错误(我尝试使用许多解决方案来修复此问题,例如在另一个 .xml 中创建一个 b
我尝试使用 Spring 提供的默认 aop 代理,但出现执行错误。 这是我的代码:运行测试示例的第一个类。 @EnableAspectJAutoProxy() @ComponentScan(base
我创建了一个自定义存储库。在这个存储库中,我需要访问标准存储库(findAll)的一些方法,所以我添加了 我使用了 Spring Boot @EntityScan(basePackageClasses
我是 Swager 2 的新手,我一直在尝试进行一些测试,但没有成功,我希望我可以使用它。情况是这样的: 我正在使用 spring-boot 创建不同的休息服务,一切正常,但是当我尝试为我的服务创建单
我正在使用Spring Data MongoDB,以下是我的存储库+模型: public interface postJobRepository extends Repository { Li
当我在本地运行./gradlew build或./gradlew test时,它工作得很好。但是,当我在docker容器中运行这些命令时,出现错误org.springframework.beans.f
我正在尝试使用 Teradata 数据库创建一个简单的 Spring 应用程序。 它正在使用 mySql 数据库和驱动程序,但在更改为 Teradata 驱动程序/基于数据库后,我收到以下异常: or
我正在使用 SpringBoot 并且我有一个 DataJpaTest,但是当我运行测试类时,我得到这个 StackTrace: java.lang.IllegalStateException: F
我正在尝试使用 MySql 实现 Hibernate spring mvc 项目,但在使用 @Entity 映射我的 Book 类时遇到问题。 MySql 服务器与 Author.java 一起工作得
我是一名优秀的程序员,十分优秀!