春季启动战争期间{$ 44catalina.home}的静态资源

时间:2019-01-11 04:09:21

标签: java spring-boot

在我的Spring MVC项目中,

<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/, file:${catalina.home}/myresources/" />

配置为从多个位置提供静态资源。 但是,在Spring Boot中,

@Configuration
public class ResourceConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/", "file:${catalina.home}/myresources/");
    }
}

无法正常工作。请帮忙。

2 个答案:

答案 0 :(得分:0)

Spring不会直接在字符串中读取此属性$ {Catalina.home}。但是您可以通过以下方式使用@Value:

@Configuration
public class ResourceConfig implements 
WebMvcConfigurer {
    @Value("${catalina.home}" )
    private String catalinaHome;
    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/", "file:" + catalinaHome + "/myresources/");
    }
}

答案 1 :(得分:0)

    @Configuration
public class ResourceConfig implements WebMvcConfigurer {

    private static final Log LOG = LogFactory.getLog(ResourceConfig.class);

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        String catalinaHome;
        try {
            catalinaHome = System.getenv("CATALINA_HOME");
        } catch (Exception e) {
            LOG.error(e);
            catalinaHome = "";
        }
        registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/", "file:" + catalinaHome + "/myresources/");    
    }
}