在 Spring Boot 中具有 jar 文件之外的配置文件

在 Spring Boot 中具有 jar 文件之外的配置文件

问题描述:

我有一个 Spring boot camel 应用程序,它的目录结构是这样的

I have a Spring boot camel application whose directory structure is like this

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

I want to convert this project into a jar file. But I want 3 files outside my jar so that I don't need to redeploy my application again and again when the configuration is changed. those 3 files are

  1. application.properties

  1. application.properties

CamelContext.xml

CamelContext.xml

sql.properties

sql.properties

我可以灵活地硬编码文件位置的路径.任何人都可以帮助我如何实现这一目标?

I have the flexibility to hardcode the path of the file location. Can anyone help me out how do I achieve this?

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

Since I have resolved the issue I will post the solution for anyone who is trying to achieve the same thing.

@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

Create a class CustomResourceLoader.java in same package as that of main class

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 文件

also, create an applicationContext.xml file in src/main/resources

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

附录 -