Java编程启动Spring的优势
上一篇文章讲解了使用web.xml启动Spring,本文讲解使用java编程启动Spring。那么,后者有什么优势呢?因为是使用java编程,最明显的优势莫过于容易调式了。若是使用web.xml的方式启动Spring,出现了问题,无法直接调试xml文件,但是,可以通过Spring的源代码间接调式,只是这种调试方式难度非常大,需要程序员了解Spring的工作原理。比如说,程序员需要知道,JavaEE应用程序容器初始化Spring应用上下文时,执行了哪一块代码。接着,在这块代码打断点进行调试。从狭义上讲,启动Spring,就是启动一个容器,就是初始化应用上下文。
java编程方式启动Spring
应用上下文的配置方式是xml文件
package com.gxz.util;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class LaunchSpring implements WebApplicationInitializer{
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.getServletRegistration("default").addMapping("/res/*");
XmlWebApplicationContext rootContext = new XmlWebApplicationContext();
rootContext.setConfigLocation("/WEB-INF/rootContext.xml");
servletContext.addListener((new ContextLoaderListener(rootContext)));
XmlWebApplicationContext dispatcherServletContext = new XmlWebApplicationContext();
dispatcherServletContext.setConfigLocation("/WEB-INF/servletContext.xml");
ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("springDispatcher", new DispatcherServlet(dispatcherServletContext));
dispatcherServlet.setLoadOnStartup(1);
dispatcherServlet.addMapping("/");
}
}
启动类需要实现接口org.springframework.web.WebApplicationInitializer,重写接口方法onStartup。实现这个接口的目的是为了让启动类在所有listener和servlet启动之前,它的onStartup方法被调用,Spring的启动需要在JavaEE应用程序启动时在最早时间被启动。
以下的java代码和xml配置是等价的。
servletContext.getServletRegistration("default").addMapping("/res/*");
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/res/*</url-pattern>
</servlet-mapping>
以下的java代码和xml配置是等价的。
XmlWebApplicationContext rootContext = new XmlWebApplicationContext();
rootContext.setConfigLocation("/WEB-INF/rootContext.xml");
servletContext.addListener((new ContextLoaderListener(rootContext)));
<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</listener-class>
</listener>
以下的java代码和xml配置是等价的。
XmlWebApplicationContext dispatcherServletContext = new XmlWebApplicationContext();
dispatcherServletContext.setConfigLocation("/WEB-INF/servletContext.xml");
ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet("springDispatcher", new DispatcherServlet(dispatcherServletContext));
dispatcherServlet.setLoadOnStartup(1);
dispatcherServlet.addMapping("/");
<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>
<servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
我们可以看出,对于根应用上下文或是DispatcherServlet应用上下文,若是使用xml文件配置的话,创建后得到的是一个XmlWebApplicationContext。若是使用java编程配置的话,创建后得到的是一个org.springframework.web.context.support.AnnotationConfigWebApplicationContext,下面的章节将会演示这一点。
应用上下文的配置方式是java编程
package com.gxz.util;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import com.gxz.config.DispatcherServletContextConfig;
import com.gxz.config.RootContextConfig;
public class LaunchSpring implements WebApplicationInitializer{
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.getServletRegistration("default").addMapping("/res/*");
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(RootContextConfig.class);
servletContext.addListener(new ContextLoaderListener(rootContext));
AnnotationConfigWebApplicationContext dispatcherServletContext = new AnnotationConfigWebApplicationContext();
dispatcherServletContext.register(DispatcherServletContextConfig.class);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("springDispatcher", new DispatcherServlet(dispatcherServletContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
以下的java代码和xml配置是等价的。
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(RootContextConfig.class);
servletContext.addListener(new ContextLoaderListener(rootContext));
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.gxz.config.RootContextConfig</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
以下的java代码和xml配置是等价的。
AnnotationConfigWebApplicationContext dispatcherServletContext = new AnnotationConfigWebApplicationContext();
dispatcherServletContext.register(DispatcherServletContextConfig.class);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("springDispatcher", new DispatcherServlet(dispatcherServletContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
<servlet>
<servlet-name>springDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.gxz.config.DispatcherServletContextConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>