Spring Boot Camel项目将配置文件置于Jar包外的实现方案
嘿,针对你这个Spring Boot Camel项目的需求——把application.properties、CamelContext.xml和sql.properties这三个配置文件放到Jar包外,不用重新打包就能修改配置,我给你梳理下硬编码路径的实现步骤,都是实际项目里常用的方法:
application.properties Spring Boot本身支持加载外部配置,硬编码路径的话有两种常用方式:
方式1:启动类里指定配置路径
直接在启动类的main方法里,给SpringApplication指定外部配置文件的路径:
@SpringBootApplication public class CamelBootApplication { public static void main(String[] args) { // 这里硬编码路径为Jar同级目录下的config文件夹,你可以改成任意绝对路径 String externalAppProps = "file:./config/application.properties"; SpringApplication app = new SpringApplication(CamelBootApplication.class); app.setDefaultProperties(Collections.singletonMap("spring.config.location", externalAppProps)); app.run(args); } }
方式2:用@PropertySource注解指定
在启动类或者配置类上添加注解,直接指向外部文件:
@SpringBootApplication @PropertySource(value = "file:./config/application.properties", ignoreResourceNotFound = false) public class CamelBootApplication { public static void main(String[] args) { SpringApplication.run(CamelBootApplication.class, args); } }
注意:ignoreResourceNotFound=false表示找不到这个文件就启动失败,确保配置文件必须存在,避免启动后出问题。
CamelContext.xml Camel的XML上下文文件默认从classpath加载,要加载外部文件,需要手动在配置类里初始化CamelContext:
@Configuration public class CamelCustomConfig { @Bean public CamelContext camelContext() throws Exception { // 硬编码外部XML文件路径 String camelXmlPath = "file:./config/CamelContext.xml"; DefaultCamelContext camelContext = new DefaultCamelContext(); // 加载外部XML里的路由和配置 XmlCamelContextLoader xmlLoader = new XmlCamelContextLoader(); try (InputStream inputStream = new FileInputStream(new File(camelXmlPath))) { xmlLoader.loadRoutes(camelContext, inputStream); } return camelContext; } }
如果用了Spring Boot Camel Starter,也可以在外部的application.properties里添加camel.springboot.xml-routes=file:./config/CamelContext.xml,不过这属于间接硬编码,你可以根据自己的习惯选择。
sql.properties 这个是自定义配置文件,两种加载方式:
方式1:@PropertySource注解
和Spring Boot配置类似,在配置类上添加注解:
@Configuration @PropertySource(value = "file:./config/sql.properties", ignoreResourceNotFound = false) public class SqlConfig { // 直接用@Value注入配置项 @Value("${sql.user.query}") private String userQuery; // 也可以把Properties注入进来 @Autowired private Environment env; // 业务方法里直接使用即可 }
方式2:手动加载Properties
如果需要更灵活的控制,可以手动加载:
@Component public class SqlPropertiesHolder { private Properties sqlProperties; @PostConstruct public void loadSqlProperties() throws IOException { String sqlPropsPath = "file:./config/sql.properties"; sqlProperties = new Properties(); try (InputStream is = new FileInputStream(new File(sqlPropsPath))) { sqlProperties.load(is); } } // 提供获取配置的方法 public String getSqlProperty(String key) { return sqlProperties.getProperty(key); } }
最后要确保这三个文件不会被打包到Jar里,需要在pom.xml的build节点里配置资源排除:
<build> <resources> <resource> <directory>src/main/resources</directory> <excludes> <exclude>application.properties</exclude> <exclude>CamelContext.xml</exclude> <exclude>sql.properties</exclude> </excludes> </resource> </resources> <!-- 保留Spring Boot打包插件的配置 --> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
打包完成后,把三个配置文件放到Jar所在目录的config文件夹下(对应我们硬编码的./config/路径),然后运行java -jar your-project-name.jar,应用就会加载外部的配置文件了。修改配置后,重启应用就能生效,完全不用重新打包。
内容的提问来源于stack exchange,提问作者Shubham Chopra

