spring-boot - 如何设置 Spring/Gradle 项目类路径以获取配置 XML 文件
问题描述
我有一个简单的基于 Spring、Gradle 的 Web 服务项目,其结构如下:
UserSettingController
src/main/java
com.ui.usersetting.datamodel
com.ui.usersetting.restinterface
com.ui.usersetting.UserSettingController
resources
resources_deployment1
spring-resource1.xml
resources_deployment2
...
有一个 @Autowired JdbcTemplate 变量,com.ui.usersetting.datamodel
其定义出现在resource/resources_deployment1/spring-resource1.xml
. @Configuration 文件中也com.ui.usersetting.datamodel
包含
@ImportResource("classpath:spring-resource1.xml")
.
这可以构建和部署,但是当我点击 中定义的端点之一时
com.ui.usersetting.restinterface
,我从 Spring 收到以下错误:
class path resource [spring-resource1.xml] cannot be opened because it does not exist
我假设这意味着UserSettingController/resources/resources_deployment1/
包含spring-resource1.xml
的 不会出现在类路径上。我试图通过在 Eclipse 中设置类路径来解决此问题,但由于我使用 Gradle 命令行命令而不是 Eclipse 构建,因此此修复不起作用。
我需要维护上面的结构,而不是移动spring-resource1.xml
到src/main/resources
,以与我们组中的其他项目保持一致。那么,我怎样才能让 Spring 在正确的地方寻找spring-resourc1.xml
呢?
解决方案
只需将额外的resources
文件夹添加到您的sourceSets
. 假设您有例如 3 个resources_deployment
文件夹:
sourceSets {
main {
resources {
(1..3).each {
srcDir "resources/resources_deployment$it"
}
}
}
}
推荐阅读
- django - Django 理解嵌套模型的 uniq 约束
- sql - 一起使用 MIN() 和 MAX() 函数
- ios - 在 DispatchGroup 完成之前关闭 vc 会导致应用程序崩溃吗?
- javascript - Javascript setInterval 在本地工作但在服务器上
- c# - twitter typeahead jquery 数据源
- python - pandas 多个独立索引(不是多索引)
- javascript - 如何在 mvc 应用程序中为 @Html.EditorFor 编写 javascript onfocusout/onblue 事件
- arrays - 如何从八度数组中读取一些固定数量的元素?
- r - R read.table() 不包括第一列?
- typescript - 使用 Mithril.js,如何将异步函数给出的选项添加到先前添加的选择元素