java Spring Boot 项目中的 SonarQube “关闭这个 ConfigurableApplicationContext”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37071032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:10:02  来源:igfitidea点击:

SonarQube "Close this ConfigurableApplicationContext" in Spring Boot project

javaspringspring-bootsonarqube

提问by kimreik

I have blocker issue "Close this "ConfigurableApplicationContext"" in main method

我在 main 方法中有阻止程序问题“关闭此“ConfigurableApplicationContext””

public static void main(String[] args)
{
    SpringApplication.run(MyApplication.class, args);
}

I've tried code from SonarQube example

我已经尝试过 SonarQube 示例中的代码

public static void main(String[] args)
{
    ConfigurableApplicationContext context = null;
    try
    {
        context = SpringApplication.run(MyApplication.class, args);
    }
    finally
    {
        if (context != null) {
            context.close();
        }
    }
}

but it closes the context right after starting.

但它在启动后立即关闭上下文。

How to fix this issue?

如何解决这个问题?

回答by Andy Wilkinson

The issue that SonarQube is reporting is a false positive and should be ignored. SonarQube's FAQlists some options for removing false positives:

SonarQube 报告的问题是误报,应该被忽略。SonarQube 的常见问题解答列出了一些用于消除误报的选项:

False-Positive and Won't Fix

You can mark individual issues as False Positive or Won't Fix through the issues interface. However, this solution doesn't work across branches - you'll have to re-mark the issue False Positive for each branch under analysis. So an in-code approach may be preferable if multiple branches of a project are under analysis:

//NOSONAR

You can use the mechanism embedded in rules engine (//NOPMD...) or the generic mechanism implemented in SonarQube: put //NOSONAR at the end of the line of the issue. This will suppress the issue.

Switch Off Issues

You can review an issue to flag it as false positive directly from the user interface.

误报且无法修复

您可以通过问题界面将个别问题标记为误报或无法修复。但是,此解决方案不适用于跨分支 - 您必须为正在分析的每个分支重新标记问题 False Positive。因此,如果正在分析项目的多个分支,则代码内方法可能更可取:

//声纳

您可以使用规则引擎 (//NOPMD...) 中嵌入的机制或 SonarQube 中实现的通用机制:将 //NOSONAR 放在问题行的末尾。这将抑制问题。

关闭问题

您可以直接从用户界面查看问题以将其标记为误报。

回答by Assen Kolov

If you have a web application, the application context will be destroyed (I think by ContextLoaderListener, not sure), no explicit code is needed.

如果您有一个 Web 应用程序,应用程序上下文将被销毁(我认为是ContextLoaderListener,不确定),不需要显式代码。

In the case of a command line application, the context must be destroyed manually, otherwise beans will not be destroyed properly - @PreDestroy methods will not be called. E.g:

在命令行应用程序的情况下,必须手动销毁上下文,否则将无法正确销毁 bean - @PreDestroy 方法将不会被调用。例如:

@Bean
public ApplicationRunner applicationRunner() {
    return new ApplicationRunner() {
        public void run(ApplicationArguments args) throws Exception {

            try {
                doStuff();
            } finally {
                context.close();
            }
        }

I noticed this when a Cassandra session stayed open after my spring boot command line application has finished.

在 Spring Boot 命令行应用程序完成后,Cassandra 会话保持打开状态时,我注意到了这一点。

回答by Joergi

I was always thinking, that it is false/positive.

我一直在想,这是假的/肯定的。

But you can test this with a this few lines.

但是你可以用这几行来测试这个。

@RunWith(SpringRunner.class)
@SpringBootTest
public class YourApplicationTest {

    @Test
    public void shouldLoadApplicationContext() {
    }

    @Test
    public void applicationTest() {
        YourApplication.main(new String[] {});
    }

}

Now Sonar is saying, this is tested!
(Kudos goes out to: Robert @ https://stackoverflow.com/a/41775613/863403)

现在声纳说,这是经过测试的!
(荣誉去:罗伯特@https: //stackoverflow.com/a/41775613/863403