spring-boot 中的 catalina.base 系统属性覆盖?

Goi*_*nas 1 eclipse spring embedded-tomcat-7 spring-boot

我的项目使用 catalina.base 系统属性来获取与该属性的文件夹位置相关的资源。在 Eclipse 中工作时,我传递 -Dcatalina.base=。作为应用程序 Main 类的 VM 参数。

我的主类是这样实现的:

@ComponentScan
@EnableAutoConfiguration
public class Main {

    private static final Logger logger = LogManager.getLogger();

    public static void main(String[] args) {

        if ( logger.isDebugEnabled() )  {
            String debugCatalinaBase = System.getProperty("catalina.base");
            // here catalinaBase is set to "." as expected.
            logger.debug("catalinaBase: {}", debugCatalinaBase);
        }

        SpringApplication.run(Main.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

作为该项目的一部分,我有一个 ApplicationConfig 类,其实现如下:

@Configuration
public class ApplicationConfig {

    private static final Logger logger = LogManager.getLogger();

    @Value("${catalina.base}")
    private String catalinaBase;

    @Bean
    public Properties jndiProperties() {

        if ( logger.isDebugEnabled() )  {
            String debugCatalinaBase = System.getProperty("catalina.base");
            // here catalinaBase is set to "C:\Users\theUser\AppData\Local\Temp\tomcat.8558104871268204693.8081". NOT as expected!!!
            logger.debug("catalinaBase: {}", debugCatalinaBase);
        }

        Properties properties = new Properties();
        properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
            CnsContextFactory.class.getName());
        properties.setProperty(Context.PROVIDER_URL,
            "file:" + catalinaBase + "\\conf\\jndi.properties");
        return properties;
    }

    @Bean( name = "propertyConfigurer" )
    public static PropertySourcesPlaceholderConfigurer propertyConfigurer() {
        PropertySourcesPlaceholderConfigurer configurer = 
            new PropertySourcesPlaceholderConfigurer();
        return configurer;
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您在代码片段的注释中看到的,在 ApplicationConfig 中,catalina.base 系统属性已更改,我不确定为什么或在哪里发生这种情况?

有关信息,我的项目使用 spring-boot-starter-xxx:1.1.4.RELEASE jar 和 spring-xxx:4.0.6.RELEASE jar。

另外,我还有另一个项目,它遵循几乎相同的整体模式并使用相同的 jar,并且该项目总是看到 catalina.base 设置为“。” 正如预期的那样。所以我想知道问题是否与有问题的项目中加载弹簧配置的顺序有关,或者与这些问题有关?

预先感谢您对此的任何帮助。下午

Phi*_*ebb 6

使用临时目录作为源Tomcat.setBaseDir(...)进行字符串引导调用。TomcatEmbeddedServletContainerFactoryTomcat 实际上是在调用in 的方法catalina.base时设置该属性的。initBaseDir()org.apache.catalina.startup.Tomcat

如果您不希望 Spring Boot 使用临时文件夹,则需要添加server.tomcat.basedir到您的临时文件夹中。application.properties