2010-04-15 69 views
1

在依賴AI有以下使用Spring配置文件:如何在Maven的依賴

<beans> 
    <bean 
     id="simplePersonBase" 
     class="com.paml.test.SimplePerson" 
     abstract="true"> 
     <property 
      name="firstName" 
      value="Joe" /> 
     <property 
      name="lastName" 
      value="Smith" /> 
    </bean> 
</beans> 

然後在項目B,我添加了一個作爲一個依賴,並具有以下配置:

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> 
    <bean 
     id="simplePersonAddress01" 
     parent="simplePersonBase"> 
     <property 
      name="firstName" 
      value="BillyBob" /> 
     <property 
      name="address" 
      value="1060 W. Addison St" /> 
     <property 
      name="city" 
      value="Chicago" /> 
     <property 
      name="state" 
      value="IL" /> 
     <property 
      name="zip" 
      value="60613" /> 
    </bean> 
</beans> 

當我使用的ClassPathXmlApplicationContext像這樣:

BeanFactory beanFactory = new ClassPathXmlApplicationContext(new String[] { 
     "./*.xml" }); 

    SimplePerson person = (SimplePerson)beanFactory.getBean("simplePersonAddress01"); 
    System.out.println(person.getFirstName()); 

春抱怨,因爲它不能解析的XML父。

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'simplePersonBase' is defined 

我確信有一種方法可以做到這一點,但是我沒有找到它。有誰知道如何?

回答

2

嘗試使用classpath*:前綴。

+0

的的src/main /資源我以前看到這一點,但另一個錯誤是誤導我。根據記錄,這是我有什麼改變時: \t \t Bean工廠的beanfactory =新的ClassPathXmlApplicationContext(新的String [] { \t \t \t 「的classpath *:* XML」}); – javamonkey79 2010-04-15 18:54:21

0

A.jave是否有相應的xml文件?在項目A中,你是否在src/main/resources中有xml?

+0

是,第一個XML片段是在項目A. – javamonkey79 2010-04-15 18:41:25

0

不是一個真正的問題的答案 - 但要注意這種方法的。

它可以是一個噩夢解決錯誤。想象一下,在啓動時出現彈簧錯誤 - 解決這個問題的唯一方法就是打開所有的罐子,找到其中的任何應用程序環境。

至少,把應用程序上下文文件在不同的包,並指定任何你想通過名稱來使用。

從類路徑的根A * .XML是一個災難。