将休眠属性配置文件移出项目



是否可以将包含数据库连接详细信息的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"})吗?

来自PropertySource的JavaDocs

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

您只需要将前缀更改为file:

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

如果您使用的是 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。

最新更新