Spring Boot Maven禁用派生项目中的特定RestController [英] Spring Boot, Maven, disable specific RestController in the derived project

查看:357
本文介绍了Spring Boot Maven禁用派生项目中的特定RestController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Maven Spring Boot 2项目中,我有一个名为api1的Maven模块.我在那里声明了许多@RestController.

In my Maven, Spring Boot 2 project I have Maven module called api1. I have declared a number of @RestControllers there.

为了扩展api1模块的逻辑,我实现了另一个名为api2的Maven模块,并将api1放置为Maven依赖项.

In order to extend the logic of the api1 module, I have implemented another Maven module called api2 and placed api1 there as Maven dependency.

现在,api1项目中的所有@RestController都在api2中初始化,因为它们都存在于api2类路径中.

Right now all of the @RestControllers from api1 project are initialized in the api2 because all of them are present on the api2 classpath.

如何在api2项目中禁用某个@RestController?

How to disable a certain @RestController in api2 project?

推荐答案

我认为这里要理解的关键事实是Spring仅在运行时起作用,而Maven在 build 中很重要

I think the crucial fact here to understand is that Spring works at runtime only, while maven matters in build time.

因此,maven认为api2依赖于api1,因此它了解到两个模块都必须包含在工件中(在spring boot的情况下,它是一个装有所有模块的大罐子).

So maven sees that api2 depends on api1 so it understands that both modules have to be included in the artifact (in the case of spring boot its a big jar with all modules inside).

现在,当spring开始时-所有模块都是可访问的,这是理所当然的,并且根据spring配置,它仅定义要加载和处理的bean,所有其余的控制器当然都在这些bean中.

Now, when spring starts - it "takes for granted" that all modules are accessible, and depending on spring configurations it just defines beans to be loaded and processed, all rest controllers are among these beans of course.

所以我想,您不介意工件(以及类路径)中有两个模块.

So I assume, you don't mind having two modules in the artifact (and in classpath).

在这种情况下,您根本不应该触摸Maven部分,但是当Spring Boot应用程序启动时,必须以某种方式指示"它,必须排除一些其余的控制器.关键是,不应以模块(嘿,春天,此控制器属于模块api2,因此必须将其排除在外")来完成,而应以业务行话"来完成.例如,api1包含所有管理员"功能,而api2包含所有应用程序"内容.因此,例如,如果您使用Java配置,则可以执行以下操作:

In this case, you shouldn't touch the maven part at all, but when the spring boot application starts it has to be "instructed" somehow that some rest controllers have to be excluded. The point is that it should be done not in terms of modules ("hey, spring, this controller belongs to module api2, so it has to be excluded"), but in terms of business "jargon". For example, api1 contains all "admin" functionality and api2 contains all "applicative" stuff. So, if you work with Java configurations, for example, you can do the following:

内部模块api1:

@Configuration
@ConditionalOnProperty(name = "admin.enabled", havingValue=true)
public class AdminControllersConfiguration {

   @Bean 
   public AdminControllerFromModuleApi1 adminController() {
      return new AdminControllerFromModuleApi1();
   }
}

}

在api2模块中,您仅以类似的方式定义了rest控制器,但是没有"@ConditionalOnProperty"注释.

In module api2 you just define your rest controllers in a similar way but without "@ConditionalOnProperty" annotation.

带有此批注的事情是,它允许关闭" bean或整个配置,例如在我的示例中.

The thing with this annotation is that it allows to "switch off" beans or entire configurations like in my example.

因此,当您启动api2时,只需在"application.properties"中定义以下内容:

So, when you start api2, you just define in "application.properties" or something the following:

admin.enabled=false

尽管物理上文件肯定在类路径中,但您的控制器不会在春季之前部署".

And your controllers won't be "deployed" by spring although physically the files are certainly in the classpath.

当然,由于spring允许使用不同类型的配置,因此该方法可能不适用于您的项目,但是思路仍然相同.

Of course, since spring allows different types of configurations, this method might not be applicable to your project, but the idea is still the same.

这篇关于Spring Boot Maven禁用派生项目中的特定RestController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆