Spring Boot 不扫描命名包

SpringBoot do not scan named packages

我构建了一个基于SpringBoot的演示模块,包括服务器和客户端应用程序。 路径如:

├── test
│   ├── client
│   │   ├── DemoController.java
│   │   └── ClientApplication.java
│   ├── server
│   │   └── ServerApplication.java

我在 ClientApplication.javaServerApplication.java 上写了两个相互冲突的自定义注释 @Client@Server

当我运行客户端或服务器时,两个注解冲突。

我想运行不带扫描包的ClientApplicationtest.server,还有ServerApplication。

我尝试了一些但没有用(springBootVersion = '1.5.11.RELEASE'):

@Client
@SpringBootApplication
@ComponentScan(basePackages = "test.client", excludeFilters = {
        @ComponentScan.Filter(type = FilterType.REGEX, pattern = "test\.server\.*"),
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, pattern = ServerApplication.class)
})
public class ClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args).stop();
    }
}

我在ClientApplication.main中写错了代码:

SpringApplication.run(***ServerApplication***.class, args).stop();

看起来很奇怪,因为这两个应用程序不在同一个基础包中。即使没有明确排除,其他软件包的配置 class 也不应该被发现。

无论如何试试这个怎么样:

@ComponentScan(basePackages = "test.client", 
  excludeFilters = @Filter(type=FilterType.REGEX, pattern="test\.server\.*")) 

此外,您可以尝试使用 @Profile 注释将 classes 拆分为客户端和服务器配置文件。

对于服务器:

 @ComponentScan(basePackages = "test.server", excludeFilters = {
    @Filter(type = FilterType.REGEX, pattern = "test.client.*")})

对于客户:

 @ComponentScan(basePackages = "test.client", excludeFilters = {
    @Filter(type = FilterType.REGEX, pattern = "test.server.*")})

或使用特定过滤器排除 class:

 @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = ServerApplication.class)