繁体   English   中英

Spring Boot无法自动装配@Repository

[英]Spring Boot not autowiring @Repository

我正在尝试使用Spring Boot配置数据源,但是却遇到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)} 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

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类位于包层次结构的顶部)。

在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

您是否在配置中尝试过@EnableJpaRepositories

配置的标准方法是创建一个基本标记存储库接口,并在组件扫描中提供该接口。

public interface RepositoryPackage {
}

将接口RepositoryPackage与UserRepository放在同一软件包中。

@ComponentScan(basePackageClasses= "RepositoryPackage.class")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM