Spring boot-扫描软件包

问题描述 投票:0回答:1

我开始在Spring Boot中编写应用程序,下面是我的包结构的外观:

com.practice.spring.project.helloworld.HelloworldApplication.java
com.practice.spring.project.repository.EmployeeRepository.java
com.practice.spring.project.model.Employee.java

下面是我如何成功启动应用程序,

@SpringBootApplication
@ComponentScan(basePackages = "com.practice.spring.project.DB", basePackageClasses = InitDatabase.class)
@EnableJpaRepositories(basePackages = "com.practice.spring.project.repository" , basePackageClasses = EmployeeRepository.class)
public class HelloworldApplication {

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

    @Bean
    public CommandLineRunner run(EmployeeRepository employeeRepository) throws Exception {
        return (args) -> {
            System.out.println("Calling it after the application context is all loaded up");
            employeeRepository.save(new Employee("Ashwin", "Architect"));
        };

    }
}

我的问题是,我是否必须为我添加的每个类都指定base-packages和baseClasses?如果有10个包具有10个不同的类,这将很困难。

确定应该有一种更简单的方法来扫描和实例化不同程序包中的类。

spring spring-boot
1个回答
0
投票
[找出一种方法-将basePackages设置为com.practice.spring.project。*

@ComponentScan(basePackages = "com.practice.spring.project.*")

© www.soinside.com 2019 - 2024. All rights reserved.