1

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

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

我写了两个相互冲突的自定义注释@Client,并@ServerClientApplication.javaServerApplication.java.

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

我想在没有扫描包的情况下运行 ClientApplication test.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();

4

2 回答 2

2

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

无论如何如何尝试这个:

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

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

于 2018-11-20T07:08:35.663 回答
2

对于服务器:

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

对于客户:

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

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

 @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = ServerApplication.class)
于 2018-11-20T07:08:56.177 回答