如何在 Java Spring Boot 中获取 Distributable()?

How to getDistributable() in Java Spring Boot?

我配置了

<distributable/>

在 web.xml 中。现在我需要检查 Web 应用程序是否可分发。

我该怎么做?

您可以从 Tomcat context 中获取此信息。您可以将嵌入式 Tomcat 容器定义为 @Bean 并从那里获取它。

import org.apache.catalina.Context;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {

    private static boolean distributable;

    public static boolean getDistributable() {
        return distributable;
    }

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

    @Bean
    public TomcatEmbeddedServletContainerFactory tomcatFactory() {

        return new TomcatEmbeddedServletContainerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                Application.distributable = context.getDistributable();
                System.out.println("distributable is :"+distributable);
            }
        };
    }   
}

您可能需要在工厂中以编程方式 setDistributable(true/false) 才能使其准确工作。