Spring 使用组件扫描配置 bean - service.UserService 中的字段 userRepository 需要类型为“repository”的 bean

Spring configuring bean with component scan - Field userRepository in service.UserService required a bean of type 'repository

我检查了很多例子,但它们通常是关于用 @Repository@Service 等标记 classes ...例如 它是关于要扫描的包的,我不确定我应该在哪里以及用什么方法进行扫描。

我有一个简单的应用程序和一个 User 模型,UserRepository 扩展 JPA 存储库和一项服务。当我尝试连接数据库时,默认设置是可以的,但是每当我想使用自定义配置时,即使我将其标记为@Repository,程序也找不到 UserRepository class .该程序仍然需要存储库class 配置bean。

这是我的服务方式:

@Service
public class UserService {
    @Autowired
    UserRepository userRepository;
    public User findById(Long id) {
        return userRepository.findById(id).get();
    }
}

型号是:

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Builder
@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String name;
    private Date birthday;
}

用户资料库class:

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}

这是我尝试实现的配置:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactory",
        transactionManagerRef = "transactionManager")
public class UserDbConfig {
    
    @Primary
    @Bean("dataSource")
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/testSchema");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        return dataSource;
    }
    
    @Primary
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
            EntityManagerFactoryBuilder builder,
            @Qualifier("dataSource") DataSource dataSource
    ) {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("com.practice.multiDatabase.model");
        factory.setDataSource(dataSource());
        return factory;
    }
    
    @Bean("transactionManager")
    public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {

        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory);
        return txManager;
    }
}

这是我的项目结构和主要方法(我没有添加任何特别的东西)

输出为:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field userRepository in com.practice.multiDatabase.service.UserService required a bean of type 'com.practice.multiDatabase.repository.UserRepository' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.practice.multiDatabase.repository.UserRepository' in your configuration.

补充说明: 当我将它添加到 main 方法时

@SpringBootApplication(scanBasePackages = {
        "com.practice.multiDatabase.repository",
        "com.practice.multiDatabase.controller",
        "com.practice.multiDatabase.service",
        //"com.practice.multiDatabase.config"
        //, "com.practice.multiDatabase.model"
})
public class MultiDatabaseApplication {

    public static void main(String[] args) {
        SpringApplication.run(MultiDatabaseApplication.class, args);
    }

}

程序正在运行,但可能它忽略了配置文件,因为我已经明确编写了控制器服务存储库,如果我也取消注释配置文件夹,那么仍然会出现同样的错误。

我的天啊。我找到了。我刚刚添加了

basePackages = {"com.practice.multiDatabase.repository"}

进行配置,现在可以使用了。

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages = {"com.practice.multiDatabase.repository"},
        entityManagerFactoryRef = "entityManagerFactory",
        transactionManagerRef = "transactionManager")
public class UserDbConfig {