Spring 快速开始 启动Spring

【启动Spring必须配置】

【web.xml部署描述符方式】

1.配置Servlet级别上下文

<servlet>

  <servlet-name>springDispatcher</servlet-name>

  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

  <init-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>/WEB-INF/servletContext.xml</param-value>

  </init-param>

  <load-on-startup>1</load-on-startup>

</servlet>

2.配置应用程序级别上下文,也叫根应用上下文。使用监听器是因为ContextLoaderListener实现了ServleContextListener所以会在应用程序启动时初始化。

<context-param>

  <param-name>contextConfigLocation</param-name>

  <param-value>/WEB-INF/rootContext.xml</param-value>

</context-param>

<listener>

  <listener-class>org.springframework.web.context.ContextLoaderListener</listen-class>

</listener>

通过ContextLoaderListener和DispatcherServlet将创建出XmlWebApplicationContext实例,

意为希望使用XML文件作为Spring配置。(AnnotationConfigWebApplicationContext是希望用类配置)

 

【初始化器使用编程方式启动Spring】

ServletContextListener 的缺点 contextInitialized方法可能在其他监听器方法之后。

JEE6 添加新接口 ServeltContainerInitializer 的onStartup方法在所有监听器启动之前调用。

一、1种不利的方式 使用Java服务提供系统声明实现了ServletContainerInitializer的一个或多个类,【在文件/META-INF/services/javax.servlet.ServletContainerInitializer列出这些类】

  com.wrox.config.ContainerInitializerOne

    com.wrox...不利在于文件不能直接存在WAR文件或Web程序的/META-INF/services,必须在JAR文件的/META-INF/services目录,然后把jar放到/WEB-INF/lib

二、SpringFramework提供桥接口,SpringServletContainerInitializer,桥的意思是在含有该类的JAR包中有一个服务提供文件列出该类名字。

扫描应用程序寻找WebApplicationInitializer接口实现,调用onStartup。我们就是通过WebApplicationInitializer实现类配置

(编程式配置监听器、Servlet、过滤器 )

public class Bootstrap implements WebApplicationInitializer{

  @Override

  public void onStartup(ServletContext container){

    XmlWebApplicationContext rootContext = new XmlWebApplicationContext();

    rootContext.setConfiguration("/WEB-INF/rootContext.xml");

    container.addListener(new ContextLoaderListener(rootContext));

    

    XmlWebApplicationContext servletContext = new XmlWebApplicationContext();

    servletContext.setConfiguration("/WEB-INF/servletContext.xml");

    ServletRegistration.Dynamic dispatcer = container.addServlet("springDispatcher",new DispatcherServlet(servletContext));

    dispatcher.setLoadOnStartup(1);

    dispatcher.addMapping("/");

  }

}

(纯Java配置启动Spring)

public class Bootstrap implements WebApplicationInitializer{

  @Override

  public void onStartup(ServletContext container){

    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();

    rootContext.register(com.wrox.config.RootContextConfiguration.class);

    container.addListener(new ContextLoaderListener(rootContext));

    

    AnnotationConfigWebApplicationContext servletContext = new AnnotationConfigWebApplicationContext();

    servletContext.register(com.wrox.config.ServletContextConfiguration.class);

    ServeltRegistration.Dynamic dispatcher = container.addServlet("springDispatcher",new DispatcherServlet(servletContext));

    dispatcher.setLoadOnStartup(1);

    dispatcher.addMapping("/");

  }

}

 

 【桌面程序、服务器守护进程】rootContext作为daemonContext和forkedProcessContext的父亲上下文。

(在web应用程序ContextLoaderListener和DispatcherServlet会自动设置父亲上下文,连start也不用我们调用,

而独立应用程序需要自己start自己stop,手动stop有点坑爹用resgisterShutdownHook回调达到jvm退出自动停止应用上下文。)

public class Bootstrap{

  public static void main(String... args){

    ClassPathXmlApplicationContext rootContext = new ClassPathXmlApplicationContext("com/wrox/config/rootContext.xml");

    FileSystemXmlApplicationContext daemonContext = new FileSystemXmlApplicationContext(new String[]{"file:/path/to/daemonContext.xml"},rootContext);

    AnnotationConfigApplicationContext forkedProcessContext = new AnnotationConfigApplicationContext(com.wrox.config.ProcessContextConfiguration.class);

    forkedProcessContext.setParent(rootContext);

    rootContext.start();

    rootContext.registerShutdownHook();

    daemonContext.start();

    daemonContext.registerShutdownHook();

    foredProcessContext.start();

    foredProcessContext.registerShutdownHook();

  }

}

 

【多年以来的mistake之一】

映射到/* 带星号的意思是 Servlet容器把JSP请求发送到DispatcherServlet,这不是我们想要的。(为什么会这么不智能?容器处理映射优先级先处理我们自定义的再到它自己的。)

假如我们要映射到应用程序的根 是【用一个/ 就足以响应所有URL】,并且Servlet容器的JSP机制仍然可以处理JSP请求。

【多年以来的mistake之二】

如果计划将DispatcherServlet映射到应用程序根,注意统计需要使用的静态资源类型,一些在线教程演示使用SpringFramework提供静态资源,这样做是mistake。

因为更具体的URL-Pattern总是会覆盖单单一个/,所以【允许Servlet容器提供静态资源】不仅简单又做对了,只需要将这些静态资源映射到名为default的Servlet上(Servlet容器提供的!)。

<servlet-mapping>

  <servlet-name>default</servlet-name>

  <url-pattern>/resources/*</url-pattern>

  <url-pattern>*.css</url-pattern>

  <url-pattern>*.js</url-pattern>

  <url-pattern>*.png</url-pattern>

  <url-pattern>*.gif</url-pattern>

</servlet-mapping>

或者 servletContext.getServletRegistration("default").addMapping("/resources/*","*.css","*.js","*.png",".gif",".jpg");

 

………………………………………………………………………………………………………………

 

posted @ 2018-04-01 19:07  chenhui7373  阅读(232)  评论(0编辑  收藏  举报