Spring通过Java配置集成Tomcat的方法

时间:2022-02-16 02:22:58

添加Tomcat依赖

  1. <!-- 自己编译的版本-->
  2. <dependency>
  3. <groupId>org.apache</groupId>
  4. <artifactId>apache-tomcat-9.0.36-src</artifactId>
  5. <version>1.0-SNAPSHOT</version>
  6. </dependency>

首先需要一个tomcat启动类

  1. public class TomcatRun {
  2.  
  3. private static final int PORT = 8080;
  4. private static final String CONTEXT_PATH = "/com/yu";
  5.  
  6. public static void main(String[] args) {
  7. Tomcat tomcat = new Tomcat();
  8. // 设置监听端口
  9. tomcat.setPort(PORT);
  10. tomcat.getHost().setAppBase(".");
  11. // 这里 Connector 不存在,自动创建一个 Connector,并将 tomcat 的端口赋值给 Connector
  12. tomcat.getConnector();
  13. tomcat.addWebapp(CONTEXT_PATH, new File("src/main/webapp").getAbsolutePath());
  14. try {
  15. tomcat.start();
  16. } catch (LifecycleException e) {
  17. e.printStackTrace();
  18. }
  19. tomcat.getServer().await();
  20. }
  21. }

也可以自己创建一个Connector指定端口

  1. // 手动创建 connector
  2. // Connector connector = new Connector();
  3. // connector.setPort(PORT);
  4. // tomcat.getService().addConnector(connector);

创建Spring配置

  1. public class MyWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  2. @Override
  3. protected Class<?>[] getRootConfigClasses() {
  4. return new Class[]{RootConfig.class};
  5. }
  6.  
  7. @Override
  8. protected Class<?>[] getServletConfigClasses() {
  9. return new Class[]{AppConfig.class};
  10. }
  11.  
  12. @Override
  13. protected String[] getServletMappings() {
  14. return new String[]{"/*"};
  15. }
  16.  
  17. @Override
  18. protected Filter[] getServletFilters() {
  19. return new Filter[]{
  20. //
  21. new CharacterEncodingFilter(StandardCharsets.UTF_8.name())
  22. };
  23. }
  24. }

这里就结束了。。。



问题来了Tomcat是怎么启动Sping的?

 

实现了Servlet3.0的容器(例如Tomcat)会扫描classpath*下面的META-INF/services/javax.servlet.ServletContainerInitializer文件,里面指定ServletContainerInitializer的实现,另外还有一个注解HandlesTypes表达对某个类感兴趣,在调用onStartup方法时会将HandlesTypes指定接口的实现类传递进来。

例如:Spring中的ServletContainerInitializer实现类SpringServletContainerInitializer会调用WebApplicationInitializeronStartup方法,也就是上面定义的MyWebApplicationInitializer父类的onStartup,这里就会相继完成AnnotationConfigWebApplicationContextDispatcherServlet的初始化

  1. @HandlesTypes(WebApplicationInitializer.class)
  2. public class SpringServletContainerInitializer implements ServletContainerInitializer {
  3.  
  4. @Override
  5. public void onStartup(@Nullable Set<Class<?>> webAppInitializerClasses, ServletContext servletContext)
  6. throws ServletException {
  7.  
  8. List<WebApplicationInitializer> initializers = new LinkedList<>();
  9.  
  10. if (webAppInitializerClasses != null) {
  11. for (Class<?> waiClass : webAppInitializerClasses) {
  12. // Be defensive: Some servlet containers provide us with invalid classes,
  13. // no matter what @HandlesTypes says...
  14. if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) &&
  15. WebApplicationInitializer.class.isAssignableFrom(waiClass)) {
  16. try {
  17. initializers.add((WebApplicationInitializer)
  18. ReflectionUtils.accessibleConstructor(waiClass).newInstance());
  19. }
  20. catch (Throwable ex) {
  21. throw new ServletException("Failed to instantiate WebApplicationInitializer class", ex);
  22. }
  23. }
  24. }
  25. }
  26.  
  27. if (initializers.isEmpty()) {
  28. servletContext.log("No Spring WebApplicationInitializer types detected on classpath");
  29. return;
  30. }
  31.  
  32. servletContext.log(initializers.size() + " Spring WebApplicationInitializers detected on classpath");
  33. AnnotationAwareOrderComparator.sort(initializers);
  34. for (WebApplicationInitializer initializer : initializers) {
  35. // 调用WebApplicationInitializer实现类的onStartup方法
  36. initializer.onStartup(servletContext);
  37. }
  38. }
  39.  
  40. }

Spring通过Java配置集成Tomcat的方法

到此这篇关于Spring通过Java配置集成Tomcat的文章就介绍到这了,更多相关Spring配置集成Tomcat内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_45387444/article/details/115682664