Charles
Charles

Reputation: 685

How to override ResourceHttpRequestHandler to implement a customized resource handler?

When I use spring mvc, I use <mvc:resources /> to map the location of the static resources to handler, and now I want to add some new functions to handler resource, is there anyone who can tell me how to override the ResourceHttpRequestHandler?

(Based on the following doc, the <mvc:resources /> use ResourceHttpRequestHandler to handle resources.)

Thanks in advance!

Upvotes: 2

Views: 2103

Answers (1)

Aravind A
Aravind A

Reputation: 9697

I haven't tried this but you could try extending the ResourceHttpRequestHandler and use a BeanFactoryPostProcessor to replace ResourceHttpRequestHandler class with your custom class . A similar solution is given here

Sample...

public class ResourceHttpRequestHandlerReplacer implements   BeanFactoryPostProcessor {
public void postProcessBeanFactory(ConfigurableListableBeanFactory factory)
        throws BeansException {

    String[] names = factory.getBeanNamesForType(ResourceHttpRequestHandler.class);

    for (String name: names) {
        BeanDefinition bd = factory.getBeanDefinition(name);
        bd.setBeanClassName("org.myProject.CustomResourceHttpRequestHandler");
    }            
  }       
}

Upvotes: 4

Related Questions