使用Spring重写Context.xml文件中的选项

时间:2013-05-11 06:10:32

标签: java xml spring

我的项目中有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>


我的Java代码重写了 context.xml

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

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

只要大声思考,可能需要用上述代码重写xml文件。

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