2010-04-15 56 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是一个灾难。