Spring 不使用XML配置文件启动 (机器翻译)

发布于 2017-07-10  242 次阅读


Servlet 3 + Spring Without XML

Software should be simple but we keep putting up artifical barriers to our end goals. XML configuration is one of those barriers as it is annoying to maintain and refactor.

软件应该很简单,但是我们不断为我们的最终目标制造人为的障碍。XML配置是其中一个障碍,因为它是烦人的维护和重构。

The XML burden was reduced with Spring 2.5 with the addition of annotation based configuration, but you still needed to bootstrap Spring in XML.

通过添加基于注释的配置,Spring 2.5减少了XML负担,但是您仍然需要以XML引导Spring。

However in Servlet 3 and Spring 3.1 we can now drop XML completely and have 100% code based configuration. This includes the web.xml! This is all thanks to the Servlet 3 spec!

然而,在Servlet 3和Spring 3.1中,我们现在可以完全删除XML并具有100%的基于代码的配置。这包括web.xml!这都是感谢Servlet 3规范!

I wrote simple "Hello world" XML free Spring project, but instead of going over the code now, I thought I would explain from the bottom up. The bootstraping process of Spring can be a bit of magic, so hopefully this will actualize the process.

我写了简单的“Hello world” XML免费的Spring项目,但是现在不用再过代码,我以为我会从下到上解释。Spring的引导过程可能有点魔法,所以希望这将实现这个过程。

XML free bootstrap process:

  1. Servlet 3 container finds all classes implementing ServletContainerInitializer
  2. Container executes a Spring implementation SpringServletContainerInitializer
  3. SpringServletContainerInitializer finds all classes implementing WebApplicationInitializer
  4. Spring executes our root configuration implementation WebApp
  5. Our root configuration create a new Spring context AnnotationConfigWebApplicationContext
  6. The context finds all classes with the @Configuration annotation
  7. The context executes our controller configuration ControllerConfiguration
  8. Our controller configuration finds all classes with the @Controller annotation
  9. The configuration creates our controller RootController
  10. Finally the context we created in step 5 is fed into the Spring Servlet DispatcherServlet

XML免费引导过程:

  1. Servlet 3容器查找实现ServletContainerInitializer的所有类
  2. Container执行Spring实现SpringServletContainerInitializer
  3. SpringServletContainerInitializer查找实现WebApplicationInitializer的所有类
  4. Spring执行我们的根配置实现WebApp
  5. 我们的根配置创建一个新的Spring上下文AnnotationConfigWebApplicationContext
  6. 上下文查找具有@Configuration注释的所有类
  7. 上下文执行我们的控制器配置ControllerConfiguration
  8. 我们的控制器配置使用@Controller注释查找所有类
  9. 该配置创建我们的控制器RootController
  10. 最后,我们在步骤5中创建的上下文被馈送到Spring Servlet DispatcherServlet中

Now when we hit http://localhost:8080 the request goes to the DispatcherServlet which is configured to handle / with the RootController.

现在当我们点击http://localhost:8080 时,请求转到DispatcherServlet,该DispatcherServlet配置为处理/使用RootController。

There are just two caveats at the moment. First your application container must support Servlet 3 for this to work. I used the Jetty 8 Maven plugin for testing. Second Spring 3.1 is currently in RC1. You can start playing with it now, but just remember to upgrade to the release version when it comes out next month.

目前只有两个警告。首先,您的应用程序容器必须支持Servlet 3才能工作。我使用Jetty 8 Maven插件进行测试。第二弹3.1目前在RC1。您现在可以开始玩它,但是请记住在下个月出货时升级到发行版本。

With all the configuration in code it is so easy to refactor and navigate. No more back and forth with XML.

通过代码中的所有配置,它很容易重构和导航。没有更多的来回与XML。


梦想还是要有的,万一实现了呢