有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java<mvc:resources>我可以指向location=“http:path/to/s3/bucket”吗?

我正在从事一个Spring MVC项目,现在我需要将我的资源转移到s3 bucket,但我需要一种机制来方便地在本地和s3之间切换。 我的dispatcher servlet中有以下代码片段。xml

例如。 对于同一项目中的文件

<mvc:resources location="/app-res/" mapping="/app-res/**"/>

对于本地驱动器上的文件,我可以

<mvc:resources location="file:D:/app-res/" mapping="/app-res/**"/>

我也可以这样做吗

<mvc:resources location="http:path/to/s3/bucket" mapping="/app-res/**"/>

我也试过这么做,但没能找到解决办法


共 (1) 个答案

  1. # 1 楼答案

    使用弹簧配置文件是一种可能的解决方案:

    http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-definition-profiles-xml

    参数化外部URL:

    <mvc:resources location="${path.to.s3.bucket}" mapping="/app-res/**"/>
    

    创建一些配置文件定义:

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:jdbc="http://www.springframework.org/schema/jdbc"
        xmlns:jee="http://www.springframework.org/schema/jee"
        xsi:schemaLocation="...">
    
        <beans profile="dev">
            <bean id="applicationPropertiesPlaceholder"
                class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                <property name="locations">
                    <list>
                        <value>classpath:profiles/dev.profile.properties</value>
                    </list>
                </property>
            </bean>     
        </beans>
    
        <beans profile="production">
            <bean id="applicationPropertiesPlaceholder"
                class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                <property name="locations">
                    <list>
                        <value>classpath:profiles/production.profile.properties</value>
                    </list>
                </property>
            </bean>         
        </beans>
    
    </beans>
    

    创建属性文件,然后只需将-Dspring.profiles.active=xyz附加到服务器的JVM参数