【问题标题】:Moving hibernate properties configuration file outside of project?将休眠属性配置文件移到项目之外?
【发布时间】:2016-09-20 10:16:36
【问题描述】:

是否可以将包含db连接详细信息的hibernate配置文件移到hibernate项目之外并连接到它?

我当前的配置如下,但我想将我的文件从项目外部移动并仍然连接到我的data sources。我该怎么做?

@Configuration
@EnableTransactionManagement
@PropertySource({"classpath:hibernate.properties"})
@EnableJpaRepositories(basePackages = "com.my.packages", entityManagerFactoryRef = "schemaOneManagerFactory", transactionManagerRef = "schemaOneTransactionManager")
public class SchemaOneDataSourceConfig
{

//configuration methods

}

我需要更改线路:@PropertySource({"classpath:hibernate.properties"}) 吗?

【问题讨论】:

    标签: java spring hibernate datasource spring-data-jpa


    【解决方案1】:

    来自PropertySource的JavaDocs

    指示要加载的属性文件的资源位置。例如,“classpath:/com/myco/app.properties”或“file:/path/to/file”。

    您只需将前缀更改为file:

    @PropertySource({"file:/path/to/hibernate.properties"})
    

    【讨论】:

    • 谢谢,是否需要双 \\ 字符来转义字符串中的字符?
    • @java123999 只需使用/ 而不是\\。
    • 当我这样做时,我得到“类路径资源无法打开,因为它不存在”?谢谢
    • @java123999 它应该可以工作。你删除classpath:吗?
    • 错误:类路径资源不存在无法打开?
    【解决方案2】:

    如果您使用 Spring Boot,您可以将属性放在 application.properties 中,并且可以将该文件放在 JAR 中,但也可以通过将其放在 config 子文件夹中来覆盖它(相对于您的运行目录)。

    请参阅http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-26
      • 2020-11-18
      • 2013-02-09
      • 2017-04-30
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      相关资源
      最近更新 更多