2013-05-11 9 views
9

Ho la configurazione Spring nel mio progetto. In questo context.xml viene riscritto dinamicamente da me in Java. La mia domanda è, perché l'URL dello spazio dei nomi dei bean non viene dopo che il file è stato riscritto?Riscrivere le opzioni nel file Context.xml utilizzando Spring

mio context.xml file prima di riscrittura:

<?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> 


codice mio Java per riscrivere il 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); 
    } 
} 


Dopo contesto. xml viene riscritta:

<?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> 


Qui il context.xml di file riscritto manca il namespace XML

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

Perchè questo xmlns manca durante la riscrittura?

risposta

3

Era molto tempo fa quando ho giocato con DOM, ma provare docFactory1.setNamespaceAware(true)false per impostazione predefinita) o setAttributeNS("http://www.springframework.org/schema/beans", "xmlns").

Btw per ottenere aiuto, cerca di ridurre il tuo problema al minimo. Il tuo problema qui è con l'utilizzo del framework Java DOM, non ha nulla a che fare con Spring. Potresti aver fatto la domanda in 3 righe senza quel rumore.

+0

Grazie per l'aiuto. La prossima volta in corsia seguirò i tuoi commenti – MadTech

+0

Non mi giudicare male, amico, senza offesa :-) Per noi è semplicemente più facile capire un problema con 3 linee di codice che lo stesso con 100 linee;] E benvenuti a SO - A proposito, queste soluzioni hanno aiutato? – rlegendi

+0

@MadTech: più una persona che ti ha aiutato, per favore. – ron

1

Pensando ad alta voce, quale potrebbe essere un requisito in cui si riscriverebbe il file xml con il codice precedente.

Guardando il tuo codice, sembra che tu voglia aggiornare una proprietà di un bean. Non puoi semplicemente ottenere il bean dal contesto, aggiornare la sua proprietà in base alla tua logica di business e fare un aggiornamento del contesto! Ciò dovrebbe essere semplice e salvarti dalla cosa complessa che stai facendo.

Problemi correlati