1. 程式人生 > >啟動Spring:java程式設計方式

啟動Spring:java程式設計方式

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>