【问题标题】:Having configuration files outside jar file in Spring Boot在 Spring Boot 中具有 jar 文件之外的配置文件
【发布时间】:2018-11-11 06:08:19
【问题描述】:

我有一个Spring Boot Camel应用,目录结构是这样的

我想将此项目转换为 jar 文件。但是我想要 3 个文件在我的 jar 之外,这样我就不需要在配置更改时一次又一次地重新部署我的应用程序。 这三个文件是

  1. application.properties

  2. CamelContext.xml

  3. sql.properties

    我可以灵活地硬编码文件位置的路径。谁能帮助我如何实现这一目标?

【问题讨论】:

  • 1.支持开箱即用,2 和 3 提供文件路径或配置加载路径的能力。
  • 能否请您详细说明 2) 和 3)
  • 尝试检查this

标签: java spring spring-boot jar apache-camel


【解决方案1】:

既然我已经解决了这个问题,我会将解决方案发布给任何试图实现相同目标的人。

@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
    /*To load CamelContext.xml file */
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

    CustomResourceLoader customResourceLoader = (CustomResourceLoader) context.getBean("customResourceLoader");

    customResourceLoader.showResourceData();

/*To load the properties file*/ 

    ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(Application.class)
            .properties("spring.config.name:application.properties,sql",
                    "spring.config.location=D:/external/application.properties,D:/external/sql.properties")
            .build().run(args);

    ConfigurableEnvironment environment = applicationContext.getEnvironment();


}

在与主类相同的包中创建一个类CustomResourceLoader.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;


public class CustomResourceLoader implements ResourceLoaderAware {

    private ResourceLoader resourceLoader;

    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    public void showResourceData() throws IOException
    {
        //This line will be changed for all versions of other examples
        Resource banner = resourceLoader.getResource("file:D:/external/CamelContext.xml");
        InputStream in = banner.getInputStream();

        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        while (true) {
            String line = reader.readLine();
            if (line == null)
                break;
            System.out.println(line);
        }
        reader.close();
    }




}

另外,在 src/main/resources 中创建一个 applicationContext.xml 文件

<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.xsd
       http://camel.apache.org/schema/spring 
       http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean id="customResourceLoader" class="main.CustomResourceLoader"></bean>

</beans>

附录 -

【讨论】:

    猜你喜欢
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多