当前,我正在application.yaml
文件中设置数据库连接信息。对于生产部署,我需要从文件中读取数据库连接字符串。
在Gitter中,我获得了使用micronaut.config.files
值的提示,但是它不起作用。
我在dbinfo.properties
文件中定义了数据库连接值,然后将build.gradle
文件更新为这样。
run {
environment('micronaut.environments','dev')
environment 'micronaut.config.files', 'C:\\shared\\application.dev.properties'
}
在application.yaml
dataSource:
url: ${DBURL}
dbCreate: create-update
pooled: true
jmxExport: true
driverClassName: com.mysql.cj.jdbc.Driver
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
username: ${DBUSER}
password: ${DBPASSWORD}
当我运行下面给出的错误消息时。
Caused by: io.micronaut.context.exceptions.BeanInstantiationException: Error instantiating bean of type [org.grails.orm.hibernate.HibernateDatastore]: Could not resolve placeholder ${DBURL} in value: ${DBURL}
是否有示例代码可从文件Micronaut Groovy代码中读取数据库连接字符串?
谢谢
SR
更新:19/2/26
我更新了build.gradle
文件,仍然是同样的错误。
run {
systemProperty('micronaut.environments','dev')
systemProperty('micronaut.config.files', 'C:\\shared\\application.dev.properties')
}
来自Application.grooy
文件
class Application {
static void main(String[] args) {
String filename=System.properties['micronaut.config.files']
File f= new File(filename)
if( f.exists()){
println "File found: $filename"
println f.text
} else{
println "file not found : $filename"
}
Micronaut.run(Application)
}
}
错误消息
19:31:49.925 [main] INFO i.m.context.env.DefaultEnvironment - Established active environments: [dev]
19:31:50.700 [main] INFO i.m.c.h.g.HibernateDatastoreFactory - Starting GORM for Hibernate
19:31:50.925 [main] ERROR io.micronaut.runtime.Micronaut - Error starting Micronaut server: Bean definition [org.grails.orm.hibernate.HibernateDatastore] could not be loaded: Error instantiating bean of type [org.grails.orm.hibernate.HibernateDatastore]: Could not resolve placeholder ${DBURL} in value: ${DBURL}
io.micronaut.context.exceptions.BeanInstantiationException: Bean definition [org.grails.orm.hibernate.HibernateDatastore] could not be loaded: Error instantiating bean of type [org.grails.orm.hibernate.HibernateDatastore]: Could not resolve placeholder ${DBURL} in value: ${DBURL}
at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1040)
at io.micronaut.context.DefaultApplicationContext.initializeContext(DefaultApplicationContext.java:201)
at io.micronaut.context.DefaultBeanContext.readAllBeanDefinitionClasses(DefaultBeanContext.java:2034)
at io.micronaut.context.DefaultBeanContext.start(DefaultBeanContext.java:156)
at io.micronaut.context.DefaultApplicationContext.start(DefaultApplicationContext.java:143)
at io.micronaut.runtime.Micronaut.start(Micronaut.java:67)
at io.micronaut.runtime.Micronaut.run(Micronaut.java:274)
at io.micronaut.runtime.Micronaut.run(Micronaut.java:260)
at webapp.Application.main(Application.groovy:30)
Caused by: io.micronaut.context.exceptions.BeanInstantiationException: Error instantiating bean of type [org.grails.orm.hibernate.HibernateDatastore]: Could not resolve placeholder ${DBURL} in value: ${DBURL}
at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:1335)
at io.micronaut.context.DefaultBeanContext.createAndRegisterSingleton(DefaultBeanContext.java:1902)
at io.micronaut.context.DefaultBeanContext.loadContextScopeBean(DefaultBeanContext.java:1543)
at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1038)
... 8 common frames omitted
Caused by: io.micronaut.context.exceptions.ConfigurationException: Could not resolve placeholder ${DBURL} in value: ${DBURL}
答案 0 :(得分:0)
您正在将环境变量与系统属性混淆
run {
systemProperty('micronaut.environments','dev')
systemProperty('micronaut.config.files', 'C:\\shared\\application.dev.properties')
}
编辑:上面的操作将导致文件成为应用程序中的属性源,但是不会提供DBURL之类的环境变量。尝试使用配置文件来提供这些值没有任何意义。
要么直接为这些值提供环境变量,要么从config中删除这些值,并允许导入的配置文件提供它们。
例如,从dataSource.url
中删除application.yml
,然后在dataSource.url=someurl
中设置C:\\shared\\application.dev.properties