春季启动扫描包



我开始在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"));
};
}
}

我的问题是,我是否应该指定基本包&我添加的每个类的基类?如果有10个包,有10个不同的班级,那就太难了。

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

找到了一种方法-将basePackages设置为com.practice.spring.project.*

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

最新更新