Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 在SpringBoot中,是否有主注释来指定用于扫描组件、存储库、实体等的基本包_Spring Boot_Annotations - Fatal编程技术网

Spring boot 在SpringBoot中,是否有主注释来指定用于扫描组件、存储库、实体等的基本包

Spring boot 在SpringBoot中,是否有主注释来指定用于扫描组件、存储库、实体等的基本包,spring-boot,annotations,Spring Boot,Annotations,问题: 我必须为不同类型的对象编写不同的注释,例如: @SpringBootApplication @EnableBatchProcessing @ComponentScan(basePackages = { "com.dbs.ntb.remittance" }) @EnableJpaRepositories(basePackages = { "com.dbs.ntb.remittance" }) @EntityScan(basePackages = { "com.dbs.ntb.remitta

问题:

我必须为不同类型的对象编写不同的注释,例如:

@SpringBootApplication
@EnableBatchProcessing
@ComponentScan(basePackages = { "com.dbs.ntb.remittance" })
@EnableJpaRepositories(basePackages = { "com.dbs.ntb.remittance" })
@EntityScan(basePackages = { "com.dbs.ntb.remittance" })
public class Application {
是否有主注释,类似于:

@Scan(basePackages = {"com.dbs.ntb.remittance"})
更新: 我看到@SpringBootApplication有这样的功能:

@SpringBootApplication(scanBasePackages = { "com.dbs.ntb.remittance" })
public class Application {

但它只是@ComponentScan的别名。它不会扫描存储库和实体。

是的,
@SpringBootApplication
将为您完成所有这些。如果将
@SpringBootApplication
放在根包中,它将为您查找JPA存储库、实体和组件

您可以制作一个元注释,以至少强制执行已设置的所有扫描

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@ComponentScan
@EntityScan()
public @interface Scan {

    @AliasFor(annotation = ComponentScan.class, attribute = "basePackages") String[] scanBasePackages();

    @AliasFor(annotation = EntityScan.class, attribute = "basePackages") String[] scanEntities();

}

我省略了@EnableJPARepositories,这至少会迫使您为每次扫描设置值,但我建议将启动应用程序放在包结构的根目录中。

刚刚要更新我的问题:@SpringBootApplication(scanBasePackages={“com.dbs.ntb.transmission”})只是@componentScan的别名。它不适用于存储库和实体。将用这个信息更新我的问题不,通常最好的做法是将SpringBoot应用程序放在包结构的根目录中,否则您将陷入配置多于约定的状态。在我的示例中,您可以创建一个元注释来稍微清理它,但是
AliasFor
注释是不可重复的,因此您不能从一个字段向多个注释提供值。确定。我现在明白了。是的,如果这个注释已经在基本包中,它当然会工作。问题是,当我们在根包alreadyYeah中没有启动启动类时,您别无选择,只能这样做,我添加了一个元注释的示例,您可以创建它来尝试并清理它。实际上,我将启动类移动到了基本包,如“com.dbs.ntb.com”。但是这个应用程序是特定于事件记录批处理的。所以我觉得将其放在“com.dbs.ntb.embigation.eventlog.batch”中是正确的。它指的是“com.dbs.ntb.migration.eventlog”中另一个模块中的一些实体。