【发布时间】:2015-01-15 14:59:13
【问题描述】:
我需要每个产品的资源映射,例如<mvc:resources mapping="/id1/resources/**" location="/id1/resources/static" />、<mvc:resources mapping="/id2/resources/**" location="/id2/resources/static" />(每个产品都可以有自己的静态 css、javascript...)。因为产品将在运行时创建,所以我不能简单地对每个资源映射声明进行硬编码。所以我能想到的办法就是使用AOP来重写每个资源请求的资源位置。
首先,我有 Aspect 类
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>
我本来想根据ResourceHttpRequestHandler#setLocations(List locations)等请求通过AOP重写位置,但在应用程序生命周期中调用的唯一函数是ResourceHttpRequestHandler#handleRequest(HttpServletRequest request, HttpServletResponse response),我似乎没有机会重写位置。
我确信一定有我遗漏的东西。有人会帮助找到在运行时重写位置的方式(不需要使用 AOP)吗?提前致谢。
【问题讨论】:
标签: java spring-mvc aop