【问题标题】:Spring filter only in dev environemntSpring过滤器仅在开发环境中
【发布时间】:2016-08-10 05:29:45
【问题描述】:

我有一个 Spring Boot 应用程序,其中有一个这样定义的过滤器:

@Component
public class MyDevFilter extends GenericFilterBean {

@Override
public void doFilter(ServletRequest req, ServletResponse resp,
                     FilterChain chain) throws IOException,  ServletException {
.. filter implementation
}

我希望这个过滤器只在开发环境中注册, 通过弹簧配置文件或 jvm 参数(无所谓)。 我怎样才能做到这一点?

【问题讨论】:

  • 添加@Profile("your-profile")。重启。

标签: java spring filter


【解决方案1】:

使用 @Profile("profile-name") 注释您的 bean,只有在该配置文件处于活动状态时才会创建它。

【讨论】:

    【解决方案2】:

    只需在其上添加一个@Profile("dev") 注释,其名称对应于您的开发环境(这里假设为“dev”)。

    然后您可以在 application.properties 文件中激活它:

    spring.profiles.active=dev
    

    或者你可以在命令行中使用:

    --spring.profiles.active=dev
    

    【讨论】:

    • 不敢相信它这么简单。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-16
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多