我在XML配置文件,它看起來像:JAXB/XJC:生成子元素類並加載它們基於類的類型
<configuration>
<database>
<host></host>
<port></port>
</database>
<queue>
<host></host>
<port></port>
<type></type>
</queue>
</configuration>
我想用JAXB/XJC生成Java類,但是我想生成這些類並將這些級別解組到樹中。我不想接收Configuration.java,而是想要一個Database.java和Queue.java(爲了能夠將這些分別注入Guice託管的應用程序中)。我(目前)沒有看到這樣做的任何方式,但可能正在尋找錯誤的東西。
一些實驗後,我已經找到了解決產生這些類和能填充並返回這些基於類:
首先,我添加了一個bindings.xjb文件,該文件將生成包含類(數據庫和隊列在該示例中)
<jaxb:bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jaxb:globalBindings localScoping="toplevel"/>
</jaxb:bindings>
然而,JAXB不能解組使用數據庫或Queue類,僅配置類(這是我可能已經錯過的東西)。我可以做
JAXBContext context = JAXBContext.newInstance(Configuration.class);
Unmarshaller um = context.createUnmarshaller();
Configuration conf = (Configuration) um.unmarhal(xmlFile);
但不
JAXBContext context = JAXBContext.newInstance(Database.class);
Unmarshaller um = context.createUnmarshaller();
Database db = (Database) um.unmarhal(xmlFile);
但是,因爲我可以通過配置對象的實例調用getDatabase()獲取數據庫對象,這也可以被泛型使用反射(在適當的地方進行這項代碼緩存結果是另一種運動):
T item = null;
try {
JAXBContext context = JAXBContext.newInstance(Configuration.class);
Unmarshaller um = context.createUnmarshaller();
Configuration conf = (Configuration) um.unmarshal(xmlFile);
Method[] allMethods = Configuration.class.getDeclaredMethods();
for (Method method : allMethods)
{
if (method.getReturnType().equals(clazz))
{
item = (T) method.invoke(conf);
break;
}
}
} catch (JAXBException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new ConfigException("Failure detected while loading configuration", e);
}
return item;
我不知道這是最好的解決方案(我只開始與JAXB昨日)的工作,但似乎到f我的目標。
既然你知道你在解組Configuration'的'實例不需要要像反思一樣動態。你知道什麼屬性和他們的類型已經。 –
此外,如果我將Database.class或Queue.class傳遞給此方法,則可以擁有此方法提供的那些副本,但不限於在任何需要使用它的位置注入應用程序配置的完整副本。 –