0

我正在研究spring spring-3.2.2。我在eclipse中创建了两个java项目。

  1. 弹簧测试
  2. 测试类路径

SpringTest 项目具有以下 beans.xml,其中定义了一个 bean。

<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-3.0.xsd">

   <bean id="helloWorld" class="com.spring.HelloWorld" init-method="testUpdate" scope="prototype">
       <property name="message" value="Hello World!"/>
   </bean>
</beans>

我已经创建了项目 SpringTest 的 jar springtest.jar,并将其添加到项目 Testclasspath 的类路径中。Testclasspath 项目的 Bean 配置文件是 Talentacquisition.xml,它正在导入 Springtest 项目的 beans.xml 文件。请找到以下人才获取.xml 内容

<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-3.0.xsd">

    <import resource="Beans.xml"/>
    <bean id="juggler" class="com.springaction.springidol.Juggler" />

 </beans>

我对 Talentacquisition.xml 中导入标记的行为感到困惑它如何能够找到存在于类路径中的 jar (springtest.jar) 中的 Beans.xml 并能够加载 bean?为什么 spring 没有给出任何错误?难道我必须将 Talentacqusition.xml 中的导入标签修改为以下

<import resource="classpath:Beans.xml"/>

如果 import 能够找到文件 Beans.xml ,那么我们应该什么时候使用 classpath:classpath* :

4

1 回答 1

1

ResourceLoaders 负责 Spring 如何加载资源。从参考手册

提供给 ApplicationContext 构造函数的一个或多个位置路径实际上是资源字符串,并且以简单的形式被适当地处理为特定的上下文实现。ClassPathXmlApplicationContext 将简单的位置路径视为类路径位置。您还可以使用带有特殊前缀的位置路径(资源字符串)来强制从类路径或 URL 加载定义,而不管实际的上下文类型如何。

您正在实例化的 ClassPathXmlApplicationContext “将简单的位置路径视为类路径位置”,即它将“Beans.xml”视为“类路径:Beans.xml”。同样,FileSystemXmlApplicationContext 会将“Beans.xml”视为“file:Beans.xml”。手册的第 6.7 节也有更多细节。

于 2013-07-24T07:59:41.853 回答