Spring MavenデフォルトXMLファイルの説明


Maven Project



検証項目を作成したら、サーブレット-context.xmlまたはweb.xmlまたはroot-contextxmlなどの基本生成が見られます.

web.xml


web.xmlの基本コードは以下の通りです.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>
	
	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- Processes application requests -->
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
		
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

1.DispatcherServiceletの設定

	<!-- Processes application requests -->
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
		
	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
<servlet mapping>を見るとurl-patternは「/」、つまりこの「/」に遭遇すればappServletという名前のサーブレットを見つけることができ、上の<servlet>を見ればappServletを見つけることができる.このappServiceletはispatcherServiceletを生成しています(init-parama生成、このDispatcherServiceletが行うべきことはservlet-context.xmlに格納されます).
1.設定しない<init-param>:<init-param><servlet-name>-servlet.xmlファイルからアプリケーションContext情報をロード
2.Spring Container:プロファイルの内容を読み、ApplicationContextオブジェクトを生成する
3.<url-pattern>:DispatcherServicelet処理のURLマッピングを定義するPattern
4.<load-on-startup>1</load-on-startup>:設定時にWAS起動時に初期化操作を行う
**DispatcherServiceletはサーブレットなので、1つ以上のDispatcherServiceletを設定できます!

2.イベント発生

	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>
	
	<!-- Creates the Spring Container shared by all Servlets and Filters -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
  • <listener>:<listener>大兄弟みたいなやつです.常に監視し、イベントが発生すると<context-param>のルートコンテキストが発生する.xmlを取得します.
  • servlet-context.xmlとroot-context。xml


    上に示すデフォルトのweb.xmlではdispatcherでservlet-contextを使用します.xmlを検索し、リスナがroot-contextから検索します.xmlを読み込むコードを確認しました.
    では、この2つのxmlファイルは何ですか?
    開始する前にservlet-context.xmlは「Webに関連付けられたコンテキスト」を設定するxmlであり、root-contextである.xmlは、「Webに関係のないコンテキスト」(ex:dao)を設定するxmlです.

    servlet-context.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/mvc"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:beans="http://www.springframework.org/schema/beans"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
    		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    
    	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    	
    	<!-- Enables the Spring MVC @Controller programming model -->
    	<annotation-driven />
    
    	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    	<resources mapping="/resources/**" location="/resources/" />
    
    	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<beans:property name="prefix" value="/WEB-INF/views/" />
    		<beans:property name="suffix" value=".jsp" />
    	</beans:bean>
    	
    	<context:component-scan base-package="com.study.mvc" />
    	
    	
    	
    </beans:beans>
    
  • <resources mapping>:resroce関連処理
  • view resolverview解析器はjspのパス要素です.
    次のコードでは、prefixは/WEB-INF/views/suffixを含む.jspを貼り付けています.
    たとえば、参照するアドレスがloginの場合、loginの前に接頭辞を付け、後に接尾辞を付け、/WEB-INF/views/loginを付けます.jspという名前のアドレスを生成します.
    したがって,loginを設定するだけでアドレスが自動的に生成される.
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<beans:property name="prefix" value="/WEB-INF/views/" />
    		<beans:property name="suffix" value=".jsp" />
    	</beans:bean>
  • <context:component-scan ... >:基本パッケージのコンポーネントを自動的に検索します.
  • root-context.xml

    
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
    	
    	<!-- Root Context: defines shared resources visible to all other web components -->
    		
    </beans>
    
  • root-context.xmlには他の要素はありませんが、設定できる領域が表示されます.
  • ウェブページ以外に設置されている領域
    Ex:ここでDataSourceなどのDBを設定します.