I have defined a variable in Config.groovy as below
environments {
development {
training.defaultStartTime = "09:00"
}
production {
training.defaultStartTime = "09:00"
}
}
Now I want to Initialize the variable startTime in my domain class using grailsApplication.config .defaultStartTime . I am doing as follows
class Training {
def grailsApplication
String startTime = grailsApplication.config.training.defaultStartTime
}
But getting an error
"Caused by BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.InstantiationException: could not instantiate test objectcom.Training"
what is the best way to initialize the variables of domain class with the properties defined in config.groovy (grailsapplication)
The problem is due to the way injection works. The bean (in the case a domain class instance) is created using its default constructor, then the DI fields are set to the corresponding beans. But the startTime
field is initialized in the constructor - before DI happens - so grailsApplication
is null.
One workaround would be to create a custom constructor - see http://burtbeckwith.com/blog/?p=1003 for the details
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.