Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在SpringWeb应用程序中加载外部应用程序配置_Java_Xml_Spring_Spring Mvc_Jaxb - Fatal编程技术网

Java 在SpringWeb应用程序中加载外部应用程序配置

Java 在SpringWeb应用程序中加载外部应用程序配置,java,xml,spring,spring-mvc,jaxb,Java,Xml,Spring,Spring Mvc,Jaxb,在我的多模块springmvcweb应用程序中,我添加了一个模块,它是一种不是spring管理的api。API有一个配置文件,即config.xml位于/main/java/resource中。我将API作为依赖项注入到spring管理的模块中 <bean id="conf" class="a.b.c.ConfLoader" factory-method="load"> <constructor-arg type="java.lang.String" value="c

在我的多模块springmvcweb应用程序中,我添加了一个模块,它是一种不是spring管理的api。API有一个配置文件,即config.xml位于/main/java/resource中。我将API作为依赖项注入到spring管理的模块中

<bean id="conf" class="a.b.c.ConfLoader" factory-method="load">
    <constructor-arg  type="java.lang.String" value="config.xml" />
</bean>

<bean id="builder" class="a.b.c.Builder">
    <constructor-arg ref="conf" />
</bean>
使用JAXB解组,将XML转换为对象

public static Configuration load(String configXML) {
    JAXBElement<Configuration> unmarshalledObject = null;
    try {
        JAXBContext jaxbContext = JAXBContext
                .newInstance(ObjectFactory.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        unmarshalledObject = (JAXBElement<Configuration>) unmarshaller
                .unmarshal(Thread.currentThread().getContextClassLoader().getResourceAsStream(configXML));

    } catch (JAXBException e) {
        LOGGER.error(e.getLocalizedMessage(), e);
    }
    Configuration config = new Configuration();
    if (unmarshalledObject != null) {
        config = unmarshalledObject.getValue();
    }
    return config;
}
只要config.xml位于应用程序的sAPI资源文件夹/main/java/resource下,这就可以正常工作

我需要将config.xml保留在应用程序之外,因为它具有特定于环境的配置

我试图将config.xml保存到tomcat的conf目录中,但没有加载它。我需要从应用程序以外的其他位置加载它

任何方向和帮助都是值得赞赏的

方法unmarchal将File作为参数,因此您可以执行以下操作:

 unmarshalledObject = (JAXBElement<Configuration>) unmarshaller
                .unmarshal(new File(pathtofile));

您可以在启动时通过命令行将其添加到类路径中吗?我想这应该放在setenv.bat/.sh中