我需要像<mvc:resources mapping="/id1/resources/**" location="/id1/resources/static" />
,<mvc:resources mapping="/id2/resources/**" location="/id2/resources/static" />
这样的资源映射为每个产品(每个产品都可以有自己的静态css,javascript ...)。由于产品将在运行时创建,因此我不能简单地对每个资源映射声明进行硬编码。所以我能想到的方法是使用AOP为每个资源请求重写资源位置。ResourceHttpRequestHandler上的Spring AOP
首先,我得看点类
public class ResAudience {
public void anyMethodBefore() {
System.out.println("any method before ...");
}
public void anyMethodAfter() {
System.out.println("any method return ...");
}
}
我发现资源请求被org.springframework.web.servlet.resource.ResourceHttpRequestHandler处理。所以AOP配置
<aop:config>
<aop:aspect ref="resAspect">
<aop:pointcut id="resHandler" expression="execution(* org.springframework.web.servlet.resource.ResourceHttpRequestHandler..*(..))" />
<aop:before pointcut-ref="resHandler" method="anyMethodBefore"/>
<aop:after pointcut-ref="resHandler" method="anyMethodAfter"/>
</aop:aspect>
</aop:config>
我holping基于该请求,如ResourceHttpRequestHandler#setLocations(List locations)通过AOP重新编写的位置,但被调用的唯一功能在应用程序生命周期是ResourceHttpRequestHandler#handleRequest(HttpServletRequest request, HttpServletResponse response),我好像在那里不有机会重新写入位置。
我相信肯定会有一些我想念的东西。有人会帮助找到在运行期间重写该位置的方式(不需要使用AOP)吗?提前致谢。