2013-02-04 49 views
0

我正在嘗試創建一個bean,並且出現錯誤。我已經縮小到這個特定的bean,並在引用API documents之後,我不明白爲什麼每當我部署我的.war文件時它會被拋出。希望我缺少一些簡單的東西。創建時發生Spring bean拋出錯誤

這裏是APP-security.xml文件內容:

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    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.1.xsd 
     http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 

    <beans:bean id="contextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource"> 
     <constructor-arg value="ldap://localhost:389/dc=mydomain,dc=com" /> 
     <property name="userDn" value="cn=manager,dc=mydomain,dc=com" /> 
     <property name="password" value="password" /> 
    </beans:bean> 

    <context:annotation-config/> 
    <context:component-scan base-package="my.current.project"/> 

</beans:beans> 

這裏是我的Maven的pom.xml的一個片段:

<properties> 
    ... 
    <spring.security.version>3.1.1.RELEASE</spring.security.version> 
    ... 
</properties> 

<dependencies> 
    ... 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-core</artifactId> 
     <version>${spring.security.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-web</artifactId> 
     <version>${spring.security.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-config</artifactId> 
     <version>${spring.security.version}</version> 
    </dependency>  
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-ldap</artifactId> 
     <version>${spring.security.version}</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-taglibs</artifactId> 
     <version>${spring.security.version}</version> 
    </dependency> 
    ... 
</dependencies> 

,這裏是拋出的錯誤:

SEVERE: Context initialization failed 
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 88 in XML document from class path resource [app-security.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 88; columnNumber: 66; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'constructor-arg'. 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334) 
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209) 
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180) 
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125) 
    at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94) 
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131) 
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4135) 
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4630) 
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791) 
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771) 
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:546) 
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:905) 
    at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:740) 
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:500) 
    at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1345) 
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:303) 
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119) 
    at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1337) 
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1601) 
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1610) 
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1590) 
    at java.lang.Thread.run(Thread.java:679) 
+0

你可以嘗試使用URL的列表,而不是構造函數的參數?無法解釋爲什麼它不起作用,但可能會讓你過去你的問題。 – CodeChimp

回答

5

您必須完全限定所有XML元素:

<beans:bean id="contextSource" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource"> 
    <beans:constructor-arg value="ldap://localhost:389/dc=mydomain,dc=com" /> 
    <beans:property name="userDn" value="cn=manager,dc=mydomain,dc=com" /> 
    <beans:property name="password" value="password" /> 
</beans:bean> 

as beans不是默認名稱空間。既然你從來沒有在您的程式碼中使用默認的Spring Security命名空間,只是讓beans一個默認:

<beans xmlns="http://www.springframework.org/schema/beans" 

,並宣佈豆類如常。

+0

我在文件的其餘部分使用了Spring Security,但所有這些bean和configs都可以正常工作。這是我能夠縮小範圍的錯誤的根源。有沒有什麼辦法可以在不改變命名空間的情況下運行?或者我應該只是將其包含在常規的'spring.xml'文件中? – James

+0

@James:我的第一個代碼片段應該可以工作,而不需要對命名空間進行任何更改。或者將其移動到普通的Spring文件並導入它,就像你所建議的那樣。 –

+0

Doh,我只是注意到了我的錯誤。謝謝! – James

相關問題