问题
I have a Spring boot camel application whose directory structure is like this
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
application.properties
CamelContext.xml
sql.properties
I have the flexibility to hardcode the path of the file location. Can anyone help me out how do I achieve this?
回答1:
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();
}
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();
}
}
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>
Appendix -
- http://www.java2novice.com/spring-boot/load-external-configuration-files/
- https://howtodoinjava.com/spring/spring-core/how-to-load-external-resources-files-into-spring-context/
来源:https://stackoverflow.com/questions/50637665/having-configuration-files-outside-jar-file-in-spring-boot