Spring 加载多个applicationContext-*.xml

本文介绍如何在Spring框架中加载多个配置文件,并通过示例展示了如何定义和使用这些配置文件来设置数据库连接和iBatis层。
 Spring 加载多个applicationContext

 

// java 类  MyDataSource 是 .DAO 类

public static void util(){
  
  ClassPathXmlApplicationContext resource = new ClassPathXmlApplicationContext(new String[]{"applicationContext-database.xml","applicationContext-ibatis.xml","applicationContext-beans.xml"});
  BeanFactory factory = resource;

  MyDataSource myData=(MyDataSource)factory.getBean("myDataSource");
  
   }

// 三个bean文件配置

 

applicationContext-database.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "spring-beans.dtd">
<beans>

   
   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
        <property name="url"><value>jdbc:oracle:thin:@172.16.3.67:1521:orcl</value></property>
        <property name="username"><value>zfoa</value></property>
        <property name="password"><value>zfoa</value></property>
    </bean>
</beans>

 

applicationContext-ibatis.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "spring-beans.dtd">
<beans>
                                                                                                                                                                                                                           
  <!-- SqlMap setup for iBATIS Database Layer -->                                                                                                                                                                                                                  
  <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">                                                                                                                                                                                                                                                                   
    <property name="configLocation" value="classpath:sql-map-config.xml"/>                                                                                                                                                                                                                                                                          
    <property name="dataSource"ref="dataSource"/>                                                                                                                                                                                                                         
  </bean> 
</beans>

applicationContext-beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "spring-beans.dtd">

<beans>
   
   <bean id="myDataSource" class="com.boco.dao.MyDataSource">                           
     <property name="sqlMapClient">                                         
       <ref bean="sqlMapClient"/>                           
     </property>                           
 </bean>  
</beans>

 

 

 

<think> 我们正在处理一个Spring配置异常:`org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException`,指出在`applicationContext-server.xml`文件的第19行有一个元素`http-conf:conduit`未被声明。根据错误信息,这是一个XML模式验证错误,因为Spring在解析配置文件时,发现了一个未在模式中定义的元素。 ### 问题分析 1. **异常类型**:`XmlBeanDefinitionStoreException` 表明在解析Spring XML配置文件时出错。 2. **错误位置**:`class path resource [applicationContext-server.xml] line 19`。 3. **根本原因**:嵌套异常`org.xml.sax.SAXParseException`,具体错误为`cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'http-conf:conduit'`。 - 这个错误意味着在XML配置中使用了`http-conf:conduit`元素,但是该元素在对应的XML Schema(模式)中没有被定义,或者模式文件没有被正确引入。 ### 解决方案 根据引用内容[1][2][3]以及错误信息,我们可以采取以下步骤: #### 1. **检查XML命名空间声明** -Spring配置文件中,我们需要确保正确声明了`http-conf`命名空间及其对应的模式位置。 - 根据引用[1]和[2]的示例,我们需要在根元素`<beans>`中声明`xmlns:http-conf`和对应的`schemaLocation`。 - 例如,如果`http-conf`属于Apache CXF的配置命名空间,则声明如下: ```xml xmlns:http-conf="http://cxf.apache.org/transports/http/configuration" ``` 并在`schemaLocation`中添加: ```xml http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd ``` - 完整的`<beans>`根元素示例: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:http-conf="http://cxf.apache.org/transports/http/configuration" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd"> ``` #### 2. **检查依赖库** - 引用[3]中提到,如果缺少相关的XML解析器库,也可能导致此类问题。确保项目中包含必要的XML处理库(如Xerces)。在Maven项目中,可以添加以下依赖: ```xml <dependency> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> <version>2.12.2</version> <!-- 使用最新稳定版本 --> </dependency> ``` 但注意,通常Spring项目已经包含了XML解析库,因此这一步骤仅在明确缺少相关库时进行。 #### 3. **验证XML配置** - 检查`applicationContext-server.xml`文件的第19行,确认`http-conf:conduit`元素的使用是否正确。 - 确保该元素的命名空间前缀`http-conf`已经在根元素中声明(如步骤1所示)。 - 如果该元素需要嵌套在特定的父元素中,请确保它处于正确的位置。 #### 4. **检查模式文件的可访问性** - 确保在运行时能够从指定的`schemaLocation`下载模式文件。如果应用运行在防火墙内,可能需要将模式文件放在本地,或者使用本地的模式文件副本(通过修改`schemaLocation`指向本地路径)。 ### 示例修复后的配置片段 假设我们是在配置Apache CXF的HTTP传输设置,修复后的配置可能如下: ```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:http-conf="http://cxf.apache.org/transports/http/configuration" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd"> <!-- 其他配置 --> <!-- 使用http-conf:conduit元素 --> <http-conf:conduit name="*.http-conduit"> <http-conf:client ConnectionTimeout="30000" ReceiveTimeout="30000"/> </http-conf:conduit> </beans> ``` ### 注意事项 - 如果使用了多个命名空间(如`context`、`mvc`等),请确保每个命名空间都有正确的声明和`schemaLocation`(如引用[2]所示)。 - 版本一致性:确保在`schemaLocation`中使用的模式版本与项目中的库版本一致(例如,Spring 4.3对应使用`spring-beans-4.3.xsd`,但通常使用不带版本号的URL可以自动匹配,如`spring-beans.xsd`)。 ### 总结 该错误的核心是XML模式验证失败,因为`http-conf:conduit`元素未被声明。通过正确声明命名空间和模式位置,并确保相关库存在,即可解决。 §§相关问题§§ 1. 如何解决Spring配置文件中命名空间声明导致的异常? 2.Spring项目中如何正确配置Apache CXF? 3. Spring XML配置中常见的模式验证错误有哪些? [^1]: 加上约束http://www.springframework.org/schema/context和http://www.springframework.org/schema/context/spring-context.xsd [^2]: 把springmvc-config.xml头文件 [^3]: 加入jar包:xerces xmlParserAPIs 和 xercesImpl </think> ### 解决 `org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException` 异常指南 您遇到的异常是由于 Spring XML 配置文件中未正确声明 `http-conf:conduit` 元素所需的命名空间和模式定义所致。以下是具体解决方案: #### 1. **添加缺失的命名空间声明** 在 `applicationContext-server.xml` 文件的根元素 `<beans>` 中添加 `http-conf` 命名空间声明: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:http-conf="http://cxf.apache.org/transports/http/configuration" <!-- 关键添加 --> xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/transports/http/configuration <!-- 添加模式位置 --> http://cxf.apache.org/schemas/configuration/http-conf.xsd"> <!-- 添加模式位置 --> ``` #### 2. **验证模式位置一致性** 确保 `schemaLocation` 中的 XSD 文件路径与您的 CXF 版本匹配: ```xml <!-- CXF 3.x+ 示例 --> http://cxf.apache.org/schemas/configuration/http-conf.xsd <!-- CXF 2.x 示例 --> http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd ``` #### 3. **添加必要的依赖(如未引入)** 在 Maven 项目中添加 Apache CXF 依赖: ```xml <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.5.5</version> <!-- 匹配您的CXF版本 --> </dependency> ``` #### 4. **验证元素使用正确性** 检查第 19 行的 `http-conf:conduit` 元素是否位于合法上下文: ```xml <!-- 正确用法示例 --> <http-conf:conduit name="*.http-conduit"> <http-conf:client ConnectionTimeout="10000" ReceiveTimeout="20000"/> </http-conf:conduit> ``` #### 5. **其他可能原因** - **XSD 访问问题**:如处离线环境,下载 XSD 到本地并修改路径: ```xml xsi:schemaLocation="... http://cxf.apache.org/schemas/configuration/http-conf.xsd file:/local/path/http-conf.xsd" ``` - **版本冲突**:检查 CXF 和 Spring 版本兼容性 - **XML 语法错误**:使用 IDE 的 XML 验证功能检查文件完整性 > **诊断要点**:`cvc-complex-type.2.4.c` 错误明确表示 XML 解析器在严格模式下找不到元素声明,核心解决方法是提供正确的命名空间和模式定义[^1][^2]。 ### 相关问题解决效果验证 修复后启动应用,以下日志表明配置加载成功: ``` INFO [main] org.springframework.context.support.ClassPathXmlApplicationContext - Loading XML bean definitions from class path resource [applicationContext-server.xml] ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值