Spring引导未自动连接@Repository
我正在尝试使用Spring Boot配置数据源,但我得到了Spring引导未自动连接@Repository,spring,spring-boot,spring-data,spring-data-jpa,Spring,Spring Boot,Spring Data,Spring Data Jpa,我正在尝试使用Spring Boot配置数据源,但我得到了org.springframework.beans.factory.BeanCreationException:创建名为“authenticationServiceImpl”的bean时出错:自动连线依赖项的注入失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:无法自动连接字段:private com.mycompany.myapp.repository.Use
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符合此依赖项的autowire候选项的条件。依赖项注释:{@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;
@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);
}
应用程序属性
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注释。据我所知,SpringBoot将自动将我的类设置为@Repository,因为它扩展了Crudepository。我做错了什么?(请注意,我的应用程序类位于包层次结构的顶部)。配置的标准方法是创建一个基本标记存储库接口,并在组件扫描中提供该接口
public interface RepositoryPackage {
}
将接口RepositoryPackage与UserRepository保持在同一个包中
@ComponentScan(basePackageClasses= "RepositoryPackage.class")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
您在配置中尝试过吗 在spring boot的主类中,您必须使用以下注释:
@SpringBootApplication
@ComponentScan(basePackages = "basepackage")
@EntityScan(basePackages ="basepackage")
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages = "basepackage")
import org.springframework.stereotype.Service;
@Service
在存储库层中使用以下注释:
@SpringBootApplication
@ComponentScan(basePackages = "basepackage")
@EntityScan(basePackages ="basepackage")
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages = "basepackage")
import org.springframework.stereotype.Service;
@Service
导入org.springframework.stereotype.Repository
导入org.springframework.transaction.annotation.Transactional
@Transactional
@Repositor
如果您有任何服务层,则在实现类中使用以下注释:
@SpringBootApplication
@ComponentScan(basePackages = "basepackage")
@EntityScan(basePackages ="basepackage")
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages = "basepackage")
import org.springframework.stereotype.Service;
@Service
我觉得还可以。这就是所有的例外吗?有嵌套的异常吗?检查具有相同名称的另一个类(UserRepository)。您是否具有正确的maven/gradle依赖项?请看本指南:
ar.com.prokarma.tim.repository.UserRepository
这是一个单独的类吗?抱歉,我已经复制了代码和异常并尝试运行它。我已经编辑了描述。我将在stacktrace之前向描述中添加异常。应用程序类中缺少@SpringBootApplication。请参阅,因为包结构非常重要。对不起,我已经尝试在我的主类上设置@SpringBootApplication
。但它不会识别我的@存储库
。这就是为什么我试着这样设置..我仍然得到异常。它没有解决依赖关系。