Issue
I am trying to register a SpringMvc configuration WebMvcConfigurationSupport
class at runtime with my context. Typically, if I didn't have the requirement to do have it registered at runtime I would use the @Configuration
annotation on the class, but I need to register a particular instance to control some of the overrides at runtime (without using statics).
Looking at the documentation here: http://docs.spring.io/spring/docs/4.0.5.RELEASE/javadoc-api/org/springframework/context/annotation/Configuration.html it would seem like I might be able to use a ConfigurationClassPostProcessor
via the context.addBeanFactoryPostProcessor()
method to add my instance of the WebMvcConfigurationSupport
to the context at runtime, but I can't for the life of my get it to work.
Is what I want to do actually possible, and does anyone have an example?
EDIT
So I could do something like this:
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
public static List<HttpMessageConverter<?>> converters;
@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.addAll(this.converters);
}
}
WebMvcConfig.converters = //...
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(WebMvcConfig.class);
But I don't like the use of the statics there and it means I can't use multiple instances of the WebMvcConfigurationSupport
class at once. (Ie the same code creating multiple web servers)
What I would actually like is something like this:
public class WebMvcConfig extends WebMvcConfigurationSupport {
private List<HttpMessageConverter<?>> converters;
public WebMvcConfig setConverters(List<HttpMessageConverter<?>> converters) {
this.converters = converters;
return this;
}
@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.addAll(this.converters);
}
}
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register((new WebMvcConfig()).setConverters(...));
The ultimate idea is that I am building a sort of "web server" builder factory that will quickly throw up a server for me where I only have to give it a few parameters and some controllers (config is taken care of by the factory)....as I find that I am using the same code in alot of projects.
Solution
I've not found the answer, but I've found a workaround that I am happy with now:
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Autowired
private WebMvcConfigDetails details;
@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.addAll(details.converters);
}
}
The the WebMvcConfigDetails
need to be added to the context by this:
context.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
beanFactory.registerSingleton("webMvcConfigDetails", webMvcConfigDetails);
}});
Answered By - Cheetah