2011-08-04 57 views
5

我正在嘗試編寫一個簡單的Spring AOP應用程序,但我遇到了xml配置問題。Spring xml問題

我的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" 
    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"> 

<bean id="audience" class="springaop.Audience"> 
</bean> 

<bean id="sam" class="springaop.Singer"> 
    <property name="id" value="1"></property> 
</bean> 

<aop:config> 
    <aop:aspect ref="audience"> 

     <aop:before pointcut="* springaop.Singer.perform(..)" 
     method="takeSeats"></aop:before> 

    </aop:aspect> 
</aop:config> 

</beans> 

我得到這樣的警告和異常:

 
WARNING: Ignored XML validation warning 
    org.xml.sax.SAXParseException: SchemaLocation: schemaLocation value = 'http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop' must have even number of URI's. 

Exception: Line 18 in XML document from class path resource [aop-conf.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'aop:config'. 

而且,我無法理解的xmlns

回答

16

變化提供XSD的位置在XML的頂部<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"> 

你只是添加此「HTTP: //www.springframework.org/schema/aop/spring-aop-3.0.xsd」。 xsi:schemaLocation屬性只是一堆對。每對中的第一個是模式URI,第二個是可以找到模式的URL。您可以將其視爲地圖:鑰匙,然後是價值。

6

xsi:schemaLocation屬性必須有偶數的URI。每對將名稱空間URI與XSD的位置相關聯。您的xsi:schemaLocation包含三個URI,因此它是無效的。這就是信息告訴你的。

您需要爲http://www.springframework.org/schema/aop