java AnnotationConfigApplicationContext 尚未刷新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25842512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AnnotationConfigApplicationContext has not been refreshed yet
提问by KarthickN
I am developing a spring MVC application. When I try to use AnnotationConfigApplicationContext in my controller class I am getting the following error. I have no idea what this statement exactly means.
我正在开发一个 spring MVC 应用程序。当我尝试在控制器类中使用 AnnotationConfigApplicationContext 时,出现以下错误。我不知道这句话到底是什么意思。
@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.springframework.context.annotation.AnnotationConfigApplicationContext@116b3c0 has not been refreshed yet
Can someone explain me what went wrong here ? I am using Spring 4.0.1.. I am new to spring mvc.
有人可以解释我这里出了什么问题吗?我正在使用 Spring 4.0.1 .. 我是 spring mvc 的新手。
回答by M. Deinum
When you are creating a new instance of an ApplicationContext
(regardless which type) you are basically creating new instances of each and every bean configured in that ApplicationContext
. That is nice the first time, it might work the second and depending on the amount of beans, the type of beans will crash after that. As the context will never be destroy (until the app crashed and is restarted) you will run into possible memory issues, performance issues, strange transactional problems etc.
当您创建一个ApplicationContext
(无论哪种类型)的新实例时,您基本上是在为该ApplicationContext
. 第一次很好,第二次可能会起作用,根据豆子的数量,豆子的类型在那之后会崩溃。由于上下文永远不会被破坏(直到应用程序崩溃并重新启动),您将遇到可能的内存问题、性能问题、奇怪的事务问题等。
A general rule of thumb is to neverconstruct a new instance of an ApplicationContext
but to use dependency injection instead.
一般的经验法则是永远不要构造 an 的新实例,ApplicationContext
而是使用依赖注入。
If you really want access to the ApplicationContext
put a field of that type in your controller and put @Autowired
on it.
如果您真的想访问ApplicationContext
控制器中该类型的字段并将其放入@Autowired
。
@Controller
public class MyController {
@Autowired
private ApplicationContext ctx;
….
}
Then you can do a lookup for the bean you need in the method. This can be handy if you use the ApplicationContext
as a factory for your beans. If all the beans you need are singletons it is better to simply inject the bean you need.
然后,您可以在方法中查找所需的 bean。如果您将ApplicationContext
用作 bean 的工厂,这会很方便。如果您需要的所有 bean 都是单例,最好简单地注入您需要的 bean。
@Controller
public class MyController {
@Autowired
private MappingFileGenerator mfg ;
….
}
Now Spring will inject the MappingFileGenerator
and it is available for use in your methods. No need to create a new instance of an ApplicationContext
.
现在 Spring 将注入MappingFileGenerator
并且它可以在您的方法中使用。无需创建ApplicationContext
.
More information is in the Spring Reference Guide.
更多信息在Spring 参考指南中。
回答by drew moore
@M.Deinum's comment will get quite a few more upvotes.
@M.Deinum 的评论将获得更多的支持。
Think of creating a new ApplicationContext
as instantiating a new (instance of an) application. Do you want to do that every time this (or any other method insaid application) is called? No, you don't.
将创建一个new ApplicationContext
视为实例化一个新的(一个)应用程序的实例。你想这样做,每到这个时候(或任何其他方法在该应用程序)被称为?不,你没有。
I'm guessing you thinkyou do because you need access to your ApplicationContext
in this method. To do that - i.e. to get access to the runningapplication context (rather than creating a new one), you want to do
我猜你认为你这样做是因为你需要ApplicationContext
在这个方法中访问你的。要做到这一点 - 即访问正在运行的应用程序上下文(而不是创建一个新的),你想要做
@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 here is the Autowired
annotation, which tells Spring to injectthe annotated object as a dependency.
这里的关键是Autowired
注解,它告诉 Spring将注解的对象作为依赖项注入。
I highly suggest following the links I've included (for starters), as what you're doing here suggests pretty strongly that you haven't wrapped your head around what DI is and does for you, and until you do, using it is likely to be counterproductive toward it's own ends for you.
我强烈建议遵循我提供的链接(对于初学者),因为您在这里所做的非常强烈地表明您还没有围绕 DI 是什么以及为您做什么,并且在您这样做之前,使用它是可能会适得其反。
回答by John doe
In case it helps someone, i was having this issue on a new URL Mapping added to a gradle project, i was missing the first slash of the url and that causing this "illegalstate not refreshed yet" on my tests
如果它对某人有帮助,我在添加到 gradle 项目的新 URL 映射上遇到了这个问题,我错过了 url 的第一个斜杠,这导致我的测试中出现了这个“尚未刷新的非法状态”