AnnotationConfigApplicationContext has not yet been updated

I am developing a spring MVC application. When I try to use AnnotationConfigApplicationContext in my controller class, I get the following error. I don’t know what exactly this means.

@RequestMapping(value = "/generate", method = RequestMethod.POST) public ModelAndView generateMappingFile(@ModelAttribute Mapping mapping) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); MappingFileGenerator mfg = ctx.getBean(MappingFileGenerator.class); } 

Error message β†’

 java.lang.IllegalStateException:org.spring framework.context.annotation.AnnotationConfigApplicationContext@ 116b3c0 has not been refreshed yet 

Can someone explain to me what is wrong here? I am using spring 4.0.1. I am new to spring mvc.

+6
source share
2 answers

When you create a new instance of ApplicationContext (no matter what type), you basically create new instances of each bean configured in this ApplicationContext . This is good the first time, it may work the second, and depending on the number of beans, then the beans type will crash after that. Since the context will never be destroyed (until the application crashes and restarts), you will encounter possible memory problems, performance problems, strange transaction problems, etc.

The general rule is to never construct a new instance of ApplicationContext , but use dependency injection instead.

If you really want access to ApplicationContext put a field of this type in your controller and put @Autowired on it.

 @Controller public class MyController { @Autowired private ApplicationContext ctx; …. } 

Then you can search for the bean that you need in this method. This can be convenient if you use ApplicationContext as a factory for your beans. If you need all the beans, then you just need to enter a bean.

 @Controller public class MyController { @Autowired private MappingFileGenerator mfg ; …. } 

Spring will now introduce the MappingFileGenerator , and it is available for use in your methods. There is no need to create a new instance of ApplicationContext .

See the Spring Reference Guide for more information.

+7
source

@ M.Deinum comments will receive a few more deferrals.

Consider creating a new ApplicationContext as an instance of a new (instance) application. Do you want to do this every time this (or any other method in the specified application) is called? No no.

I assume that you think you are doing it because you need access to your ApplicationContext in this method. To do this, to access the context of the executable application (and not to create a new one), you want to do

 @Controller // or @Service / @Component / ... : tells Spring that this is a bean, and to inject the specified dependencies class YourClass { @Autowired // tells Spring that this object is a dependency should should be injected ApplicationContext ctx; @RequestMapping(value = "/generate", method = RequestMethod.POST) public ModelAndView generateMappingFile(@ModelAttribute Mapping mapping) { MappingFileGenerator mfg = ctx.getBean(MappingFileGenerator.class); } 

The key point is the Autowired annotation, which tells Spring inject the annotated object as a dependency .

I highly recommend following the links that I have included (for starters), because what you are doing here suggests pretty much that you have not wrapped your head in what DI is doing for you, and while you do, using it is more likely total, will be counterproductive in relation to its own goals for you.

+2
source

Source: https://habr.com/ru/post/975276/


All Articles