Spring在方法上的@Profile是一个好习惯吗 [英] Is Spring's @Profile on methods a good practice
本文介绍了Spring在方法上的@Profile是一个好习惯吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Spring Boot Web应用程序,公开了休息服务.
I have a Spring Boot Web application exposing rest services.
我在问自己如何正确管理过滤器"上的配置文件. 实际上,我的应用程序有2个配置文件:dev和prod(您猜它代表什么...)
I'm asking myself how to correctly manage profiles on Filters. Actually, my app has 2 profiles: dev and prod (you guess what it stands for...)
在生产模式下,我要激活的过滤器比在开发模式下要多.
In prod mode, i have more filters to activate than in dev mode.
我的过滤器"配置类如下:
My Filters configuration class is the following:
@Configuration
public class FiltersConfig {
@Bean
public FilterRegistrationBean filterRegistrationBean(CompositeFilter compositeFilter){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setDispatcherTypes(EnumSet.allOf(DispatcherType.class));
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.setFilter(compositeFilter);
return filterRegistrationBean;
}
@Bean
@Profile("dev")
public CompositeFilter devCompositeFilter(){
CompositeFilter compositeFilter = new CompositeFilter();
List<Filter> filtersList = new ArrayList<>();
//filtersList.add(filter1());
compositeFilter.setFilters(filtersList);
return compositeFilter;
}
@Bean
@Profile("prod")
public CompositeFilter prodCompositeFilter(){
CompositeFilter compositeFilter = new CompositeFilter();
List<Filter> filtersList = new ArrayList<>();
//filtersList.add(filter1());
compositeFilter.setFilters(filtersList);
return compositeFilter;
}
}
我的问题是:
- 在方法上添加@Profile是一种好习惯吗?
- 是否有一种方法可以强制编译器排除类,方法等,这些类,方法等使用与当前配置文件不同的配置文件进行注释? (我不希望我的生产jar/war充满不必要的代码!)
- 弹簧靴是否提供了一种更清晰的方式来组织档案?
- Is it a good practice to add @Profile on method?
- Is there a way to force the compiler to exclude classes, methods, etc. annotated with a diferent profiles than the one set as current? (I don't want my production jar/war populated with unnecessary code!)
- Does spring boot provide a clearer way to organize profiles?
thx.
推荐答案
我认为最好在不同的程序包中为不同环境配置.您不想混合您的配置. 结构可能看起来像这样:
I think it would be better to have configurations for different environments in distinct packages. You don't want to mix your configuration. The structure might look like this:
config
- Config1.java
- Config2.java
dev
- WebConfig.java
- DataConfig.java
prod
- WebConfig.java
- DataConfig.java
这篇关于Spring在方法上的@Profile是一个好习惯吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文