0

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

herman wu
  • 1
  • 1

2 Answers2

0

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?

Nathan Russell
  • 3,428
  • 5
  • 30
  • 51
  • Also, see section 4.1 of this: http://www.baeldung.com/spring-mvc-static-resources where it says "In fact, if no ResourceResolver is added to the ResourceChainRegistration, this is the default resolver." – Nathan Russell Dec 14 '16 at 09:28
  • Yes, you are right! It really adds a default PathResourceResolver and a CachingResourceResolver. But I do not know how to only use the resolvers that I have set and avoid to appear the default resolvers? It seems that spring adds default resolvers alwalys – herman wu Dec 14 '16 at 11:49
  • I have just find there exist note in the spring-mvc-4.3.xsd: – herman wu Dec 14 '16 at 12:10
  • To override the standard/default beans (including resolvers), simply define them as you have done, but make sure you give them the same id that spring would set on its default one – Nathan Russell Dec 14 '16 at 12:13
0

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>
herman wu
  • 1
  • 1