Được cố gắng để tìm ra một giải pháp tốt đẹp này bản thân mình rằng
- xoay quanh việc tạo ra một XSD cho tập tin cấu hình - vì trong tâm trí của tôi toàn bộ lợi ích của việc sử dụng XML là bạn có thể mạnh mẽ gõ tệp cấu hình, về kiểu dữ liệu và trường nào là bắt buộc/tùy chọn
- Sẽ xác thực XML đối với XSD, vì vậy nếu giá trị bị thiếu, nó sẽ ném lỗi ra ngoài thay vì bean của bạn được chèn bằng 'null '
- Không dựa vào chú thích mùa xuân (như @Value - trong tâm trí của tôi, đó là cung cấp kiến thức về hạt cà phê abo ut + mối quan hệ của họ với các hạt khác, do đó, phá vỡ IOC)
- Sẽ xác thực XML mùa xuân dựa vào XSD, vì vậy nếu bạn cố gắng tham chiếu một trường XML không có trong XSD, nó sẽ ném ra một lỗi quá
- Đậu không có kiến thức về giá trị tài sản của nó đang được tiêm từ XML (tức là Tôi muốn tiêm các thuộc tính riêng lẻ, và không phải toàn bộ đối tượng XML)
Điều tôi nghĩ ra là như sau, xin lỗi đây là một cuộn dài, nhưng tôi thích nó như một giải pháp vì tôi tin nó bao gồm mọi điều. Hy vọng rằng điều này có thể hữu ích cho ai đó. mảnh nhỏ đầu tiên:
bean Tôi muốn giá trị tài sản tiêm vào:
package com.ndg.xmlpropertyinjectionexample;
public final class MyBean
{
private String firstMessage;
private String secondMessage;
public final String getFirstMessage()
{
return firstMessage;
}
public final void setFirstMessage (String firstMessage)
{
this.firstMessage = firstMessage;
}
public final String getSecondMessage()
{
return secondMessage;
}
public final void setSecondMessage (String secondMessage)
{
this.secondMessage = secondMessage;
}
}
class Test để tạo ra đậu trên và đổ ra các giá trị tài sản đó có:
package com.ndg.xmlpropertyinjectionexample;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public final class Main
{
public final static void main (String [] args)
{
try
{
final ApplicationContext ctx = new ClassPathXmlApplicationContext ("spring-beans.xml");
final MyBean bean = (MyBean) ctx.getBean ("myBean");
System.out.println (bean.getFirstMessage());
System.out.println (bean.getSecondMessage());
}
catch (final Exception e)
{
e.printStackTrace();
}
}
}
MyConfig.xsd :
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:myconfig="http://ndg.com/xmlpropertyinjectionexample/config" targetNamespace="http://ndg.com/xmlpropertyinjectionexample/config">
<xsd:element name="myConfig">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="1" maxOccurs="1" name="someConfigValue" type="xsd:normalizedString" />
<xsd:element minOccurs="1" maxOccurs="1" name="someOtherConfigValue" type="xsd:normalizedString" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Tệp MyConfig.xml mẫu dựa trên XSD:
<?xml version="1.0" encoding="UTF-8"?>
<config:myConfig xmlns:config="http://ndg.com/xmlpropertyinjectionexample/config">
<someConfigValue>First value from XML file</someConfigValue>
<someOtherConfigValue>Second value from XML file</someOtherConfigValue>
</config:myConfig>
Snippet của tập tin pom.xml để chạy xsd2java (đã không có nhiều khác ở đây ngoài việc thiết để Java 1.6, và phụ thuộc vào mùa xuân-ngữ cảnh):
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<executions>
<execution>
<id>main-xjc-generate</id>
<phase>generate-sources</phase>
<goals><goal>generate</goal></goals>
</execution>
</executions>
</plugin>
Bây giờ XML mùa xuân riêng của mình. Điều này tạo ra một sơ đồ/validator, sau đó sử dụng JAXB để tạo ra một unmarshaller để tạo ra một POJO từ tập tin XML, sau đó sử dụng lò xo # chú thích để bơm giá trị tài sản bằng quering POJO:
<?xml version="1.0" encoding="UTF-8"?>
<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.2.xsd" >
<!-- Set up schema to validate the XML -->
<bean id="schemaFactory" class="javax.xml.validation.SchemaFactory" factory-method="newInstance">
<constructor-arg value="http://www.w3.org/2001/XMLSchema"/>
</bean>
<bean id="configSchema" class="javax.xml.validation.Schema" factory-bean="schemaFactory" factory-method="newSchema">
<constructor-arg value="MyConfig.xsd"/>
</bean>
<!-- Load config XML -->
<bean id="configJaxbContext" class="javax.xml.bind.JAXBContext" factory-method="newInstance">
<constructor-arg>
<list>
<value>com.ndg.xmlpropertyinjectionexample.config.MyConfig</value>
</list>
</constructor-arg>
</bean>
<bean id="configUnmarshaller" class="javax.xml.bind.Unmarshaller" factory-bean="configJaxbContext" factory-method="createUnmarshaller">
<property name="schema" ref="configSchema" />
</bean>
<bean id="myConfig" class="com.ndg.xmlpropertyinjectionexample.config.MyConfig" factory-bean="configUnmarshaller" factory-method="unmarshal">
<constructor-arg value="MyConfig.xml" />
</bean>
<!-- Example bean that we want config properties injected into -->
<bean id="myBean" class="com.ndg.xmlpropertyinjectionexample.MyBean">
<property name="firstMessage" value="#{myConfig.someConfigValue}" />
<property name="secondMessage" value="#{myConfig.someOtherConfigValue}" />
</bean>
</beans>
Đó là chắc chắn tiện dụng để biết, và sẽ là một bản sao lưu tiện dụng.Tôi đoán nó dễ dàng, đủ để ghi đè PropertiesPersister để thực hiện phân tích kiểu phân tích Apache Digester, thay vì định dạng xml thuộc tính tiêu chuẩn. – GaryF
nó sẽ làm gì để tạo thứ gì đó tải các tệp xml tùy chỉnh (không thuộc tính) bằng trình tải tài nguyên? –