how to understand that SpringMVC resource-chain includes resolvers that I have not set

1.9k views Asked by At

My dispatcher.xml sets resource chain like below:

<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>

when I debug the codes ,I find that the resource chains has two PathResourceResolver, why ?Thank you! ResourceHttpRequestHanndler state

2

There are 2 answers

4
Nathan Russell On

I suspect Spring is adding a default PathResourceResolver bean with the default id pathResourceResolver. You are explicitly declaring one but without an id, so you are ending up with 2.
The Spring Framework does this a lot - ie. it will create default beans for you unless you specify a bean with the same id.

You can test this theory by:
1) comment your PathResourceResolver out of your config? Do you end up with 1?
2) use your PathResourceResolver in your config, but give it the id pathResourceResolver? Do you end up with 1?

0
herman wu On

I have just find there exist note in the 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>