1

The concept is we can create a spring application without having XML files so please point to some solution of this using java config file

I am working on creating a sample web mvc application as a proof of concept I am having issue where once the form is submitted I cannot submit again using the form . It gives this error

GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once

I can see where my issue is but I cant seem to find a work around

Controller.java

 AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();

//@TODO hibernate broke validity
//@TODO disable submit until data is valid
@RequestMapping(value="form", method=RequestMethod.POST)
public String submitForm(@ModelAttribute @Valid Subscriber subscriber,BindingResult result, Model m) {
    m.addAttribute("message", "Successfully saved person: " + subscriber.toString());
    ctx.register(WebConfiguration.class);
    //@TODO fix refresher only once issue
    ctx.refresh();
    SubscriberDao sao = ctx.getBean(SubscriberDao.class);
    sao.savePerson(subscriber);
    return "formPage";
}

If you look here ctx.refresh gets called everytime I go to the URL. I tried putting it in a constructor but that did not work.Can anyone help me find a solution/Better alternative if this is wrong.

Praveen
  • 101
  • 14

1 Answers1

0

This is a 2 part answer. I have found to one part which comprises of the problem when we try to refresh context in RequestMapping. This is however not true solution.The real solution is to declare a WebApplicationInitializer which I still need to work on.

I digress.

Solution:

Replace AnnotationConfigApplicationContext with AnnotationConfigWebApplicationContext and that should get the above code working.

But it is advisable to put more time and understand how WebApplicationInitializer is implemented in long run

Praveen
  • 101
  • 14