9

我的项目中有Spring配置。在那个context.xml是由我用 Java 动态重写的。我的问题是,为什么在文件被重写后 bean 命名空间 URL 没有出现?

重写前 我的context.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:util="http://www.springframework.org/schema/util" 
    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-2.0.xsd 
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2..xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<!-- <context:annotation-config /> -->

<bean class="org.springframework.ws.client.core.WebServiceTemplate" id="webServiceTemplate">
    <constructor-arg ref="messageFactory"/>
    <property name="marshaller" ref="xmlbeansMarshaller"/>
    <property name="unmarshaller" ref="xmlbeansMarshaller"/>
    <property name="defaultUri">
     <value>https://google.com</value></property>
</bean></beans>


我重写context.xml的 Java 代码:

DocumentBuilderFactory docFactory1 = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder1 = docFactory1.newDocumentBuilder();
Document doc1 = docBuilder1.parse(afilePath);

Node incIncident1 = doc1.getElementsByTagName("beans").item(0);

NodeList beanList = incIncident1.getChildNodes();

NodeList beanlist1 = beanList.item(25).getChildNodes();
List <Map<String, String>> aunitDetails = be.extendedData.get("uicdsDetails");
if (aunitDetails != null) {
    for (int i = 0; i < aunitDetails.size(); i++) {
        Map<String, String> unitLogDetails = aunitDetails.get(i);
        NodeList beanList2= beanlist1.item(7).getChildNodes();
        if (unitLogDetails.get("uURL") != null) {
            beanList2.item(0).setTextContent(unitLogDetails.get("uicdsURL"));
        } else {
            beanList2.item(0).setTextContent("https://google.com");
        }
        TransformerFactory transformerFactory1 = TransformerFactory.newInstance();
        Transformer transformer1 = transformerFactory1.newTransformer();
        System.out.println(doc);
        DOMSource source1 = new DOMSource(doc1);
        StreamResult result1 = new StreamResult(new File(afilePath));
        transformer1.transform(source1, result1);
    }
}


重写 context.xml 后:

<?xml version="1.0" encoding="UTF-8"?>
    <beans  
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:util="http://www.springframework.org/schema/util" 
        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-2.0.xsd 
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2..xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
    <!-- <context:annotation-config /> -->

    <bean class="org.springframework.ws.client.core.WebServiceTemplate" id="webServiceTemplate">

        <constructor-arg ref="messageFactory"/>
        <property name="marshaller" ref="xmlbeansMarshaller"/>
        <property name="unmarshaller" ref="xmlbeansMarshaller"/>
        <property name="defaultUri">
         <value>https://google.com</value></property>
    </bean>

</beans>


这里重写的context.xml文件缺少 XML 命名空间

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

为什么xmlns在重写时会丢失?

4

2 回答 2

3

很久以前当我玩 DOM 时,但尝试docFactory1.setNamespaceAware(true)false默认情况下)或setAttributeNS("http://www.springframework.org/schema/beans", "xmlns").

顺便说一句,为了获得帮助,请尽量将您的问题减少到最低限度。您的问题是使用 Java DOM 框架,它与 Spring 无关。您可以在没有所有噪音的情况下用 3 行来问这个问题。

于 2013-05-13T06:27:36.087 回答
1

只是大声想出来,你可以用上面的代码重写 xml 文件的要求是什么。

查看您的代码,您似乎想要更新 bean 的属性。您不能简单地从上下文中获取 bean,根据您的业务逻辑更新其属性并进行上下文刷新!那应该保持简单,并使您免于做复杂的事情。

于 2013-05-19T14:46:39.797 回答