1. 程式人生 > >XML配置檔案頭部理解

XML配置檔案頭部理解

簡介

學習web開發兩年時間了,雖然以前經常使用如Hibernate、Spring等框架的xml配置檔案,但是對於他的頭部幾乎是一個小白,今天發現理解這些還是非常有用的,所以接下來做簡單的介紹。

原始碼示例

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

<aop:aspectj-autoproxy />

<bean id="customerBo" class="com.yiibai.customer.bo.impl.CustomerBoImpl" />

<!-- Aspect -->
<bean id="logAspect" class="com.yiibai.aspect.LoggingAspect" />

<aop:config>

  <aop:aspect id="aspectLoggging" ref="logAspect">

    <!-- @Before -->
    <aop:pointcut id="pointCutBefore"
      expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomer(..))" />

    <aop:before method="logBefore" pointcut-ref="pointCutBefore" />

    <!-- @After -->
    <aop:pointcut id="pointCutAfter"
       expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomer(..))" />

    <aop:after method="logAfter" pointcut-ref="pointCutAfter" />

    <!-- @AfterReturning -->
    <aop:pointcut id="pointCutAfterReturning"
       expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomerReturnValue(..))" />

    <aop:after-returning method="logAfterReturning"
      returning="result" pointcut-ref="pointCutAfterReturning" />

    <!-- @AfterThrowing -->
    <aop:pointcut id="pointCutAfterThrowing"
      expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomerThrowException(..))" />

    <aop:after-throwing method="logAfterThrowing"
      throwing="error" pointcut-ref="pointCutAfterThrowing" />

    <!-- @Around -->
    <aop:pointcut id="pointCutAround"
      expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomerAround(..))" />

    <aop:around method="logAround" pointcut-ref="pointCutAround" />

  </aop:aspect>

</aop:config>

</beans>
解釋
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

xmlns:xsi相當於定義了一個字首xsi,這個字首有一個識別符號是“http://www.w3.org/2001/XMLSchema-instance”,這個可以理解為單純的字串,而後邊的xsi:schemaLocation則是先指定剛才定義的識別符號“http://www.w3.org/2001/XMLSchema-instance”,然後給出這個識別符號表示的字首的定義文件所在的地方“http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ”,此時的這個URL不能理解為單純的字串,而是一個真實的網路地址,表示要從這裡去取xsi的定義檔案。

這個的理解可以類似於web.xml當中的Servlet的配置。先單獨提取出來頭部,第一行和第二行,一個是xmlns一個卻是xmlns:xsi,這兩個雖然看著不一樣,但是本質上是一樣的,第一個只是使用了當前文件的預設頭部,比如後邊的文件內容當中使用的<bean>,這個是沒有任何字首的,但是他的原理跟有字首的是一樣的,只是他是用了預設字首。

如果在文件內容中要使用aop所指定的元素,此時就必須帶上aop字首用來表示這個元素來自於AOP這個schema定義檔案(什麼是schema檔案去網上查)。