问题描述
我正在尝试使用 Spring Boot 配置数据源,但出现 org.springframework.beans.factory.BeanCreationException:创建名为authenticationServiceImpl"的 bean 时出错:自动装配依赖项的注入失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.mycompany.myapp.repository.UserRepository com.mycompany.myapp.service.AuthenticationServiceImpl.userRepository;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到类型为 [com.mycompany.myapp.repository.UserRepository] 的合格 bean 依赖项:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者.依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
I'm trying to configure a datasource with Spring Boot but I'm getting org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'authenticationServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mycompany.myapp.repository.UserRepository com.mycompany.myapp.service.AuthenticationServiceImpl.userRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.mycompany.myapp.repository.UserRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
这是我的项目结构:
主类:
package com.mycompany.myapp;
@ComponentScan(basePackageClasses = {com.mycompany.myapp.domain.user.User.class,
com.mycompany.myapp.repository.UserRepository.class,
com.mycompany.myapp.service.AuthenticationServiceImpl.class})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
域类:
package com.mycompany.myapp.domain.user
@Entity
public class User {
@Id
@GeneratedValue
private long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String lastName;
@Column(nullable = false)
private String password;
@Column(nullable = false)
private String email;
public User() {}
public User(String email, String password){
this.email = email;
this.password = password;
}
}
存储库:
package com.mycompany.myapp.repository;
public interface UserRepository extends CrudRepository<User, Long> {
List<User> findByLastName(String lastName);
}
控制器
package com.mycompany.myapp.service;
@RestController
public class AuthenticationServiceImpl implements AuthenticationService {
@Autowired
private UserRepository userRepository;
@RequestMapping("/add")
public User add(){
User user = new User();
user.setName("Juan");
user.setLastName("Sarpe");
user.setEmail("email@gmail.com");
userRepository.save(user);
return user;
}
}
application.properties
application.properties
spring.datasource.url:jdbc:mysql://localhost:3306/mydb
spring.datasource.username=user
spring.datasource.password=pass
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
我猜我的应用程序没有检测到我在 UserRepository 上的 @Repository 注释.据我所知,Spring Boot 会自动将我的类设置为 @Repository,因为它扩展了 CrudRepository.我做错了什么?(请注意,我的 Application 类位于包层次结构的顶部).
I guess my app is not detecting my @Repository annotation on UserRepository. As far as I know, Spring Boot will automatically set my class as @Repository because it is extending CrudRepository. What am I doing wrong? (Notice that my Application class is on top of packages hierarchy).
推荐答案
在 spring boot 的主类中,你必须使用下面的注释:
@SpringBootApplication
@ComponentScan(basePackages = "basepackage")
@EntityScan(basePackages ="basepackage")
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages = "basepackage")
在 Repository 层使用下面的注释:
导入 org.springframework.stereotype.Repository;
import org.springframework.stereotype.Repository;
导入 org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Transactional;
@Transactional
@Repositor
如果您有任何服务层,则在实现类中使用以下注释:
import org.springframework.stereotype.Service;
@Service
这篇关于Spring Boot 不自动装配 @Repository的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!