AnnotationConfigWebApplicationContext与AnnotationConfigApplicationContext

时间:2018-10-03 05:08:38

标签: spring spring-mvc

Spring 3.0提供了所有基于Java的经典XML配置,我将做同样的事情,但是在我进行研发的同时,我遇到了两种情况,其中AnnotationConfigApplicationContext在main()方法内部实现了,我不知道这种情况适合生产吗,

在其他情况下,我发现AnnotationConfigWebApplicationContext是在onStartup()的{​​{1}}内部实现的

为了更好地理解,请参考以下两种情况的片段

场景1

WebApplicationInitializer

场景2:

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext ctx = 
         new AnnotationConfigApplicationContext(HelloWorldConfig.class);

      HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
      helloWorld.setMessage("Hello World!");
      helloWorld.getMessage();
   }
}

我想知道这两种方法的优缺点,哪种方法最适合生产。请建议我。

0 个答案:

没有答案
相关问题