我正在尝试使用 Spring Boot 配置数据源,但出现org.springframework.beans.factory.BeanCreationException: 创建名称为“authenticationServiceImpl”的 bean 时出错: Autowiring 依赖项注入(inject)失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法 Autowiring 字段:私有(private) com.mycompany.myapp.repository.UserRepository com.mycompany.myapp.service.AuthenticationServiceImpl.userRepository;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到依赖项 [com.mycompany.myapp.repository.UserRepository] 类型的合格 bean:预计至少有 1 个有资格作为此依赖项的 Autowiring 候选者的 bean。依赖注释:{@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);
}
Controller
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("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="afcac2cec6c3efc8c2cec6c381ccc0c2" rel="noreferrer noopener nofollow">[email protected]</a>");
userRepository.save(user);
return user;
}
}
应用程序属性
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。我究竟做错了什么? (请注意,我的应用程序类位于包层次结构的顶部)。
最佳答案
在 Spring Boot 的主类中,您必须使用以下注释:
@SpringBootApplication
@ComponentScan(basePackages = "basepackage")
@EntityScan(basePackages ="basepackage")
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages = "basepackage")
在存储库层中使用以下注释:
导入org.springframework.stereotype.Repository;
导入org.springframework.transaction.annotation.Transactional;
@Transactional
@Repositor
如果您有任何服务层,请在实现类中使用以下注释:
import org.springframework.stereotype.Service;
@Service
关于Spring Boot 不 Autowiring @Repository,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37620503/