0

我的 dispatcher.xml 设置资源链如下:

<mvc:resources location="/static/" mapping="/static/**" cache-period="31536000">
        <mvc:resource-chain resource-cache="true">
            <mvc:resolvers>
                <!--<bean class="org.springframework.web.servlet.resource.GzipResourceResolver"/>-->
                <bean class="org.springframework.web.servlet.resource.CachingResourceResolver">
                    <constructor-arg ref="staticResourceCache"/>
                </bean>
                <bean class="org.springframework.web.servlet.resource.VersionResourceResolver">
                    <property name="strategyMap">
                        <map>
                            <entry key="/**">
                                <bean class="org.springframework.web.servlet.resource.ContentVersionStrategy"/>
                            </entry>
                        </map>
                    </property>
                </bean>
                <bean class="org.springframework.web.servlet.resource.PathResourceResolver"/>
            </mvc:resolvers>
            <mvc:transformers>
                <bean class="org.springframework.web.servlet.resource.CachingResourceTransformer">
                    <constructor-arg ref="staticResourceCache"/>
                </bean>
                <bean class="org.springframework.web.servlet.resource.CssLinkResourceTransformer">
                    <!--<property name="allowedLocations" value="/static"> </property>-->
                </bean>
            </mvc:transformers>
        </mvc:resource-chain>
    </mvc:resources>

当我调试代码时,我发现资源链有两个PathResourceResolver,为什么?谢谢! ResourceHttpRequestHandler 状态

4

2 回答 2

0

我刚刚发现 spring-mvc-4.3.xsd 中存在注释:

<xsd:documentation source="org.springframework.web.servlet.config.annotation.ResourceChainRegistration"><![CDATA[
Assists with the registration of resource resolvers and transformers.
Unless set to "false", the auto-registration adds default Resolvers (a PathResourceResolver) and Transformers
(CssLinkResourceTransformer, if a VersionResourceResolver has been manually registered).
The resource-cache attribute sets whether to cache the result of resource resolution/transformation;
setting this to "true" is recommended for production (and "false" for development).
A custom Cache can be configured if a CacheManager is provided as a bean reference
in the "cache-manager" attribute, and the cache name provided in the "cache-name" attribute.
        ]]></xsd:documentation>
于 2016-12-14T12:13:16.827 回答
0

我怀疑 Spring 正在添加一个PathResourceResolver具有默认 id 的默认 bean pathResourceResolver。您明确声明了一个但没有 id,因此您最终得到了
2。Spring 框架经常这样做 - 即。除非您指定具有相同 id 的 bean,否则它将为您创建默认 bean。

您可以通过以下方式测试该理论:
1)将PathResourceResolver您的配置注释掉?你最后是1吗?
2)PathResourceResolver在你的配置中使用你的,但给它 id pathResourceResolver?你最后是1吗?

于 2016-12-14T09:26:52.653 回答