ResourceHttpRequestHandler上的Spring AOP

时间:2015-01-13 23:23:12

标签: java spring-mvc aop

我需要为每个产品(例如每个产品都有自己的静态css,javascript等)提供<mvc:resources mapping="/id1/resources/**" location="/id1/resources/static" /><mvc:resources mapping="/id2/resources/**" location="/id2/resources/static" />等资源映射。因为产品将在运行时创建,所以我不能简单地对每个资源映射声明进行硬编码。所以我能想到的方法是使用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),其中我没有{{3}}。似乎有机会重新写下这个位置。

我相信一定有我遗失的东西。有人会帮助找到在运行期间重写位置的方式(不需要使用AOP)吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

回答我自己的问题。

根据我的需要,动态创建静态资源的资源映射,此解决方案适用于我,Setting resource paths programatically in spring MVC